Update slot imports

This commit is contained in:
Yapollon 2024-01-26 09:29:47 +01:00
parent 4809068888
commit f0585f22fa
2 changed files with 68 additions and 18 deletions

View File

@ -75,20 +75,6 @@ def create_habit(user_id: int, name: str, times: int, unit: int, slot: int, note
return cursor.lastrowid 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): def get_habit(id: int):
query = f"SELECT * FROM habits WHERE id = {id};" query = f"SELECT * FROM habits WHERE id = {id};"
conn = con3() conn = con3()
@ -109,6 +95,44 @@ def get_habits(user_id: int):
return habits 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): def delete_habit(id: int):
query = f"DELETE FROM habits WHERE id = {id};" query = f"DELETE FROM habits WHERE id = {id};"
conn = con3() conn = con3()

View File

@ -1,6 +1,7 @@
from dataclasses import dataclass 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 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: # Unit wird als Integers wie folgt gemessen:
@ -30,9 +31,34 @@ class Habit:
habit = get_habit(id) habit = get_habit(id)
return Habit(habit[0], habit[1], habit[2], habit[3], habit[4], habit[5], habit[6]) if habit else None return Habit(habit[0], habit[1], habit[2], habit[3], habit[4], habit[5], habit[6]) if habit else None
@staticmethod def update(self, name: str=None, note: str=None, times: int=None, unit: int=None):
def delete(id: int): update_habit(self.id, name, note, times, unit)
delete_habit(id) 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]: def get_habitTrackings(self) -> list[HabitTrackings]: