|
| 1 | +from flask import Blueprint, jsonify, request |
| 2 | +from app.models import Goal, GoalMilestone, db |
| 3 | +from app.auth import token_required |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | +bp = Blueprint("goals", __name__) |
| 7 | + |
| 8 | +@bp.route("", methods=["GET"]) |
| 9 | +@token_required |
| 10 | +def get_goals(current_user): |
| 11 | + goals = Goal.query.filter_by(user_id=current_user.id).all() |
| 12 | + result = [] |
| 13 | + for g in goals: |
| 14 | + milestones = GoalMilestone.query.filter_by(goal_id=g.id).all() |
| 15 | + result.append({ |
| 16 | + "id": g.id, |
| 17 | + "name": g.name, |
| 18 | + "target_amount": float(g.target_amount), |
| 19 | + "current_amount": float(g.current_amount), |
| 20 | + "currency": g.currency, |
| 21 | + "deadline": g.deadline.isoformat() if g.deadline else None, |
| 22 | + "created_at": g.created_at.isoformat(), |
| 23 | + "milestones": [{ |
| 24 | + "id": m.id, |
| 25 | + "name": m.name, |
| 26 | + "target_amount": float(m.target_amount), |
| 27 | + "achieved": m.achieved |
| 28 | + } for m in milestones] |
| 29 | + }) |
| 30 | + return jsonify(result), 200 |
| 31 | + |
| 32 | +@bp.route("", methods=["POST"]) |
| 33 | +@token_required |
| 34 | +def create_goal(current_user): |
| 35 | + data = request.json |
| 36 | + try: |
| 37 | + deadline = datetime.strptime(data["deadline"], "%Y-%m-%d").date() if data.get("deadline") else None |
| 38 | + except ValueError: |
| 39 | + return jsonify({"error": "Invalid date format, use YYYY-MM-DD"}), 400 |
| 40 | + |
| 41 | + new_goal = Goal( |
| 42 | + user_id=current_user.id, |
| 43 | + name=data["name"], |
| 44 | + target_amount=data["target_amount"], |
| 45 | + current_amount=data.get("current_amount", 0.0), |
| 46 | + currency=data.get("currency", "INR"), |
| 47 | + deadline=deadline |
| 48 | + ) |
| 49 | + db.session.add(new_goal) |
| 50 | + db.session.commit() |
| 51 | + |
| 52 | + if "milestones" in data: |
| 53 | + for m in data["milestones"]: |
| 54 | + new_ms = GoalMilestone( |
| 55 | + goal_id=new_goal.id, |
| 56 | + name=m["name"], |
| 57 | + target_amount=m["target_amount"], |
| 58 | + achieved=m.get("achieved", False) |
| 59 | + ) |
| 60 | + db.session.add(new_ms) |
| 61 | + db.session.commit() |
| 62 | + |
| 63 | + return jsonify({"message": "Goal created successfully", "id": new_goal.id}), 201 |
| 64 | + |
| 65 | +@bp.route("/<int:goal_id>", methods=["PUT"]) |
| 66 | +@token_required |
| 67 | +def update_goal(current_user, goal_id): |
| 68 | + goal = Goal.query.filter_by(id=goal_id, user_id=current_user.id).first() |
| 69 | + if not goal: |
| 70 | + return jsonify({"error": "Goal not found"}), 404 |
| 71 | + |
| 72 | + data = request.json |
| 73 | + if "name" in data: |
| 74 | + goal.name = data["name"] |
| 75 | + if "target_amount" in data: |
| 76 | + goal.target_amount = data["target_amount"] |
| 77 | + if "current_amount" in data: |
| 78 | + goal.current_amount = data["current_amount"] |
| 79 | + if "deadline" in data: |
| 80 | + try: |
| 81 | + goal.deadline = datetime.strptime(data["deadline"], "%Y-%m-%d").date() if data["deadline"] else None |
| 82 | + except ValueError: |
| 83 | + return jsonify({"error": "Invalid date format"}), 400 |
| 84 | + |
| 85 | + db.session.commit() |
| 86 | + return jsonify({"message": "Goal updated successfully"}), 200 |
| 87 | + |
| 88 | +@bp.route("/<int:goal_id>", methods=["DELETE"]) |
| 89 | +@token_required |
| 90 | +def delete_goal(current_user, goal_id): |
| 91 | + goal = Goal.query.filter_by(id=goal_id, user_id=current_user.id).first() |
| 92 | + if not goal: |
| 93 | + return jsonify({"error": "Goal not found"}), 404 |
| 94 | + db.session.delete(goal) |
| 95 | + db.session.commit() |
| 96 | + return jsonify({"message": "Goal deleted successfully"}), 200 |
0 commit comments