From 85d169aa726efb7a04208616038093e0df1b1b18 Mon Sep 17 00:00:00 2001 From: Senne Drent Date: Fri, 8 Dec 2023 12:33:30 +0100 Subject: [PATCH] feat: add_training_event with subcategory (#79) * InsertTrainingEvent method #78 * removed unused imports * fixed type errors * added type def to event_rows * fixed .append() call syntax * made requested changes regarding id formatting * fixed Ruff CI errors --- src/apiserver/data/api/trainings.py | 32 +++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/apiserver/data/api/trainings.py diff --git a/src/apiserver/data/api/trainings.py b/src/apiserver/data/api/trainings.py new file mode 100644 index 00000000..6dc21354 --- /dev/null +++ b/src/apiserver/data/api/trainings.py @@ -0,0 +1,32 @@ +from datetime import date +from schema.model.model import C_EVENTS_CATEGORY, C_EVENTS_DATE, C_EVENTS_DESCRIPTION, C_EVENTS_ID, CLASS_EVENTS_TABLE, CLASS_ID +from sqlalchemy.ext.asyncio import AsyncConnection +from store.db import LiteralDict, insert_many + +async def add_training_event( + conn: AsyncConnection, + classification_id: int, + categories: list[str], + event_date: date, + description: str = "", +) -> list[str]: + """breaks up a training into their subcategories and insert them into the class_events table. + event_id will be created in the form of: "training[yyyy/dd/mm][category]" + we make the assumption that there are no two trainings in a day""" + idList = [] + event_rows: list[LiteralDict] = [] + for subCategory in categories: + subEventId = f'training{event_date.isoformat()}{subCategory}' + idList.append(subEventId) + + event_row: LiteralDict = { + C_EVENTS_ID: subEventId, + CLASS_ID: classification_id, + C_EVENTS_CATEGORY: subCategory, + C_EVENTS_DATE: event_date, + C_EVENTS_DESCRIPTION: description, + } + event_rows.append(event_row) + await insert_many(conn, CLASS_EVENTS_TABLE, event_rows) + + return idList