From 7e628d5e8d8797c19c80375fef9e7fed1919866c Mon Sep 17 00:00:00 2001 From: "mauro.cortellazzi" Date: Fri, 26 Jul 2024 11:52:00 +0200 Subject: [PATCH] feat(api): add update module API --- api/app/db/dao/model_dao.py | 10 ++++++++++ api/app/routes/model_route.py | 8 ++++++++ api/app/services/model_service.py | 9 +++++++++ 3 files changed, 27 insertions(+) diff --git a/api/app/db/dao/model_dao.py b/api/app/db/dao/model_dao.py index 0ea13f90..287aae66 100644 --- a/api/app/db/dao/model_dao.py +++ b/api/app/db/dao/model_dao.py @@ -32,6 +32,16 @@ def get_by_uuid(self, uuid: UUID) -> Optional[Model]: .one_or_none() ) + def update(self, uuid: UUID, model: Model): + with self.db.begin_session() as session: + query = ( + sqlalchemy.update(Model) + .where(Model.uuid == uuid) + .values(**model.attributes()) + ) + return session.execute(query).rowcount + + def delete(self, uuid: UUID) -> int: with self.db.begin_session() as session: deleted_at = datetime.datetime.now(tz=datetime.UTC) diff --git a/api/app/routes/model_route.py b/api/app/routes/model_route.py index d19978c2..fd2b0da9 100644 --- a/api/app/routes/model_route.py +++ b/api/app/routes/model_route.py @@ -1,6 +1,7 @@ import logging from typing import Annotated, List, Optional from uuid import UUID +from requests import Response from fastapi import APIRouter from fastapi.params import Query @@ -51,4 +52,11 @@ def delete_model(model_uuid: UUID): logger.info('Model %s with name %s deleted.', model.uuid, model.name) return model + @router.post('/{model_uuid}', status_code=200) + def update_model_by_uuid(model_uuid: UUID, model_in: ModelIn): + if model_service.update_model_by_uuid(model_uuid, model_in): + return Response(status_code=200) + else: + return Response(status_code=404) + return router diff --git a/api/app/services/model_service.py b/api/app/services/model_service.py index 9dd77758..5af163f8 100644 --- a/api/app/services/model_service.py +++ b/api/app/services/model_service.py @@ -46,6 +46,15 @@ def get_model_by_uuid(self, model_uuid: UUID) -> Optional[ModelOut]: latest_current_dataset=latest_current_dataset, ) + def update_model_by_uuid(self, model_uuid: UUID, model_in: ModelIn) -> bool: + try: + to_update = model_in.to_model() + return self.model_dao.update(model_uuid, to_update) > 0 + except Exception as e: + raise ModelInternalError( + f'An error occurred while updating the model: {e}' + ) from e + def delete_model(self, model_uuid: UUID) -> Optional[ModelOut]: model = self.check_and_get_model(model_uuid) self.model_dao.delete(model_uuid)