1- from sqlalchemy .engine .cursor import LegacyCursorResult
21from time_entries ._domain import ActivitiesDao , Activity
3- from sqlalchemy import create_engine ,Table , Column , String , MetaData
42from sqlalchemy .sql import select
3+ from ..database import db
54import dataclasses
6- import json
75import typing
86
9-
107class ActivitiesSQLDao (ActivitiesDao ):
118 def __init__ (self ):
129 self .activity_keys = [
1310 field .name for field in dataclasses .fields (Activity )
1411 ]
15- self .engine = create_engine ('postgresql://postgres:root@localhost/time-tracker' )
16- self .conn = self .engine .connect ()
17- metadata_obj = MetaData ()
18- self .activity = Table ('activity' , metadata_obj ,
19- Column ('id' , String , primary_key = True ),
20- Column ('name' , String ),
21- Column ('description' , String ),
22- Column ('deleted' , String ),
23- Column ('status' , String ),
24- Column ('tenant_id' , String ),
25- )
12+ db .db_init ()
2613
2714
28-
15+ #GET Activity by id
2916 def get_by_id (self , activity_id : str ) -> Activity :
30- query = select (self .activity ).where (self .activity .c .id == activity_id )
31- activity = self .conn .execute (query ).one ()
17+ """search for the activity by the given id
18+ parameters: str (the id string of the searched activity)
19+ returns: Activity (The activity model with the response if found or a void if not)."""
20+ query = select (db .activity ).where (db .activity .c .id == activity_id )
21+ activity = db .connection .execute (query ).one ()
3222 return self .__create_activity_dto (dict (activity )) if activity else None
3323
3424 def get_all (self ) -> typing .List [Activity ]:
35- query = select (self .activity )
36- result = self . conn .execute (query )
25+ query = select (db .activity )
26+ result = db . connection .execute (query )
3727 return [
3828 self .__create_activity_dto (dict (activity ))
3929 for activity in result
4030 ]
4131
32+ def create_activity (self , activity_data : dict ) -> Activity :
33+ query = db .activity .insert ().values (activity_data )
34+ activity = db .connection .execute (query )
35+ return self .__create_activity_dto (activity .last_inserted_params ())
36+
4237 def delete (self , activity_id : str ) -> Activity :
43- query = self .activity .update ().where (self .activity .c .id == activity_id ).values (status = 'inactive' )
44- self . conn .execute (query )
38+ query = db .activity .update ().where (db .activity .c .id == activity_id ).values (status = 'inactive' )
39+ db . connection .execute (query )
4540 return self .get_by_id (activity_id );
4641
42+ def update (self , activity_id : str , new_activity : dict ) -> Activity :
43+ query = db .activity .update ().where (db .activity .c .id == activity_id ).values (new_activity )
44+ db .connection .execute (query )
45+ return self .get_by_id (activity_id )
46+
4747 def __create_activity_dto (self , activity : dict ) -> Activity :
48- activity = {key : activity .get (key ) for key in self .activity_keys }
48+ activity = {key : activity .get (key ). hex if key == "id" else activity . get ( key ) for key in self .activity_keys }
4949 return Activity (** activity )
0 commit comments