Skip to content

Commit

Permalink
feat(api): add update module API
Browse files Browse the repository at this point in the history
  • Loading branch information
maocorte committed Jul 26, 2024
1 parent 48b780b commit 7e628d5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions api/app/db/dao/model_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions api/app/routes/model_route.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
9 changes: 9 additions & 0 deletions api/app/services/model_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7e628d5

Please sign in to comment.