Fixed get_all()

and added delete methods for user and habits
This commit is contained in:
Yapollon 2024-01-17 11:23:22 +01:00
parent 70c9a3fbdf
commit cc8f2f56a1
2 changed files with 26 additions and 5 deletions

1
app.py
View File

@ -4,6 +4,7 @@ import hashlib
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.User import User
from utils import anonymous_required

View File

@ -41,6 +41,17 @@ def get_user_by_email(email: str):
return user
def delete_user(id: int):
query = f"DELETE FROM habits WHERE user_id = {id};"
query2 = f"DELETE FROM users WHERE id = {id};"
conn = con3()
cursor = conn.cursor()
cursor.execute(query)
cursor.execute(query2)
conn.commit()
conn.close()
def create_habit(name: str, user_id: int, times: int, unit: int, slot: int, note: str | None=None):
now = datetime.now().isoformat()
query = (f"INSERT INTO habits (user_id, name, note, times, unit, list_index, created_at, updated_at) VALUES ('{user_id}', "
@ -58,9 +69,9 @@ def get_habit(id: int):
conn = con3()
cursor = conn.cursor()
cursor.execute(query)
habits = cursor.fetchone()
habit = cursor.fetchone()
conn.close()
return habits
return habit
def get_habits(user_id: int):
@ -68,6 +79,15 @@ def get_habits(user_id: int):
conn = con3()
cursor = conn.cursor()
cursor.execute(query)
habits = cursor.fetchone()
habits = cursor.fetchall()
conn.close()
return habits
def delete_habit(id: int):
query = f"DELETE FROM habits WHERE id = {id};"
conn = con3()
cursor = conn.cursor()
cursor.execute(query)
conn.commit()
conn.close()