Skip to content

Commit f574a1e

Browse files
committed
feat: ✨ Participant update
1 parent 0906c11 commit f574a1e

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎app/routers/participants.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,21 @@ async def get_all_participant(
6767
finds = ParticipantService.get_all()
6868
return finds
6969

70+
@router.put("/{id}", response_model=Participant)
71+
async def update_participant(
72+
id: PyObjectId,
73+
participant: Participant,
74+
current_user: Annotated[UserInDB, Depends(get_current_active_user)],
75+
):
76+
find = ParticipantService.get(id)
77+
if find is None:
78+
return HTMLResponse(
79+
status_code=status.HTTP_404_NOT_FOUND, content="Participant not found"
80+
)
81+
participantDB = ParticipantInDB(**participant.dict())
82+
participantDB.username_update = current_user.username
83+
updated = ParticipantService.update(id, participantDB)
84+
return updated
7085

7186
@router.post("/registration", response_model=Participant)
7287
async def register_participant(

‎app/services/participant.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ def delete(cls, id: PyObjectId) -> ParticipantInDB | None:
3434
return ParticipantInDB(**deleted)
3535
else:
3636
return None
37+
38+
@classmethod
39+
def update(cls, id: PyObjectId, item: ParticipantInDB) -> ParticipantInDB | None:
40+
if hasattr(item, "id"):
41+
delattr(item, "id")
42+
item.date_update = datetime.utcnow()
43+
44+
updated = cls.TABLE.find_one_and_update(
45+
{"_id": id, "disabled": False},
46+
{"$set": item.dict(by_alias=True)},
47+
return_document=ReturnDocument.AFTER,
48+
)
49+
if updated is not None:
50+
return ParticipantInDB(**updated)
51+
else:
52+
return None
3753

3854
@classmethod
3955
def get(cls, id: PyObjectId) -> ParticipantInDB | None:

0 commit comments

Comments
 (0)