diff --git a/db/SQLiteClient.py b/db/SQLiteClient.py index aa65254..08a06c9 100644 --- a/db/SQLiteClient.py +++ b/db/SQLiteClient.py @@ -75,20 +75,6 @@ def create_habit(user_id: int, name: str, times: int, unit: int, slot: int, note return cursor.lastrowid -def get_next_slot(user_id: int): - query = f"SELECT slot FROM habits WHERE user_id = {user_id} ORDER BY slot DESC LIMIT 1;" - conn = con3() - cursor = conn.cursor() - cursor.execute(query) - slot = cursor.fetchone() - conn.close() - - if not slot: - return 0 - - return slot[0] + 1 if slot else 0 - - def get_habit(id: int): query = f"SELECT * FROM habits WHERE id = {id};" conn = con3() @@ -109,6 +95,44 @@ def get_habits(user_id: int): return habits +def get_next_slot(user_id: int): + query = f"SELECT slot FROM habits WHERE user_id = {user_id} ORDER BY slot DESC LIMIT 1;" + conn = con3() + cursor = conn.cursor() + cursor.execute(query) + slot = cursor.fetchone() + conn.close() + return slot[0] + 1 if slot else 0 + + +def get_slots(user_id: int): + query = f"SELECT id, slot FROM habits WHERE user_id = {user_id} ORDER BY slot;" + conn = con3() + cursor = conn.cursor() + cursor.execute(query) + slots = cursor.fetchall() + conn.close() + return slots + + +def update_habit(id: int, name: str, note: str, times: int, unit: int): + query = f"UPDATE habits SET name = {name}, note = {note}, times = {times}, unit = {unit} WHERE id = {id};" + conn = con3() + cursor = conn.cursor() + cursor.execute(query) + conn.close() + return cursor.lastrowid + + +def update_slot(id: int, slot: int): + query = f"UPDATE habits SET slot = {slot} WHERE id = {id};" + conn = con3() + cursor = conn.cursor() + cursor.execute(query) + conn.close() + return cursor.lastrowid + + def delete_habit(id: int): query = f"DELETE FROM habits WHERE id = {id};" conn = con3() diff --git a/models/Habit.py b/models/Habit.py index 940227f..dd5ec8e 100644 --- a/models/Habit.py +++ b/models/Habit.py @@ -1,6 +1,7 @@ from dataclasses import dataclass -from db.SQLiteClient import create_habit, get_habit, delete_habit, get_next_slot, get_habitTrackings_by_habit_id from models.HabitTrackings import HabitTrackings +from db.SQLiteClient import update_slot, create_habit, get_habit, delete_habit, get_next_slot, \ + get_habitTrackings_by_habit_id, get_slots, update_habit # Unit wird als Integers wie folgt gemessen: @@ -30,9 +31,34 @@ class Habit: habit = get_habit(id) return Habit(habit[0], habit[1], habit[2], habit[3], habit[4], habit[5], habit[6]) if habit else None - @staticmethod - def delete(id: int): - delete_habit(id) + def update(self, name: str=None, note: str=None, times: int=None, unit: int=None): + update_habit(self.id, name, note, times, unit) + if name is not None: + self.name = name + if note is not None: + self.note = note + if times is not None: + self.times = times + if unit is not None: + self.unit = unit + + # So sollte die Slots Liste aussehen + #[(id, 1), (id, 2), (id, 3), (id, 4), (id, 5)] + def update_slot(self, new_slot: int): + slots = get_slots(self.user_id) + if new_slot > self.slot: + slots = slots[self.slot:new_slot] + for slot in slots: + update_slot(slot[0], slot[1]-1) + if new_slot < self.slot: + slots = slots[new_slot-1:self.slot-1] + for slot in slots: + update_slot(slot[0], slot[1]+1) + self.slot = new_slot + + + def delete(self): + delete_habit(self.id) def get_habitTrackings(self) -> list[HabitTrackings]: