mirror of
https://github.com/283375/arcaea-offline.git
synced 2025-04-21 15:00:18 +00:00
feat(db): update_score(score_id, new_score)
This commit is contained in:
parent
15beeb5f35
commit
619d4029f8
@ -208,11 +208,15 @@ class Database(metaclass=Singleton):
|
||||
def get_scores(
|
||||
self,
|
||||
*,
|
||||
score_id: Optional[int] = None,
|
||||
song_id: Optional[List[str]] = None,
|
||||
rating_class: Optional[List[int]] = None,
|
||||
):
|
||||
where_clauses = []
|
||||
params = []
|
||||
if score_id:
|
||||
where_clauses.append("id = ?")
|
||||
params.append(score_id)
|
||||
if song_id:
|
||||
where_clauses.append(f"song_id IN ({','.join('?'*len(song_id))})")
|
||||
params.extend(song_id)
|
||||
@ -261,6 +265,23 @@ class Database(metaclass=Singleton):
|
||||
conn.commit()
|
||||
self.__trigger_update_hooks()
|
||||
|
||||
def update_score(self, score_id: int, new_score: ScoreInsert):
|
||||
# ensure we are only updating 1 row
|
||||
scores = self.get_scores(score_id=score_id)
|
||||
print(score_id)
|
||||
assert len(scores) == 1, "Cannot update multiple or non-existing score(s)"
|
||||
columns = self.__get_columns_from_dataclass(ScoreInsert)
|
||||
params = [getattr(new_score, column) for column in columns] + [score_id]
|
||||
update_columns_param_clause = ", ".join([f"{column} = ?" for column in columns])
|
||||
with self.conn as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
f"UPDATE scores SET {update_columns_param_clause} WHERE id = ?",
|
||||
params,
|
||||
)
|
||||
conn.commit()
|
||||
self.__trigger_update_hooks()
|
||||
|
||||
def delete_score(self, score_id: int):
|
||||
with self.conn as conn:
|
||||
cursor = conn.cursor()
|
||||
|
Loading…
x
Reference in New Issue
Block a user