diff --git a/app.py b/app.py index 22aeedd..123178a 100644 --- a/app.py +++ b/app.py @@ -5,6 +5,7 @@ from flask import Flask, render_template, redirect, url_for, request from flask_login import login_required, LoginManager, login_user, logout_user, current_user from models.Habit import Habit +from models.HabitTrackings import HabitTrackings from models.User import User from utils import anonymous_required @@ -223,8 +224,33 @@ def habit_create(): @app.route('/check', methods=['POST']) @login_required def check_habit(): - habit = request.get_json()["habitId"] - return {} + habit_id = request.get_json()["habitId"] + + habit = Habit.get(habit_id) + + if habit is None: + return {"error": "Habit not found"} + + # Check if habit belongs to user + if habit.user_id != current_user.id: + return {"error": "Habit does not belong to user"} + + trackings = habit.get_habitTrackings() + + # Check if habit has been tracked today + unchecked = False + for tracking in trackings: + if tracking.created_at.date() == datetime.date.today(): + tracking.delete() + unchecked = True + + if not unchecked: + HabitTrackings.create(habit_id, 1) + + return { + "habitId": habit_id, + "unchecked": unchecked, + } # Run the application