diff --git a/src/impl/Event/router_v1.py b/src/impl/Event/router_v1.py index 252f8cde..53505c4d 100644 --- a/src/impl/Event/router_v1.py +++ b/src/impl/Event/router_v1.py @@ -6,7 +6,7 @@ from src.configuration.Configuration import Configuration from src.error.AuthenticationException import AuthenticationException from src.impl.Company.schema import CompanyGet -from src.impl.Event.schema import EventCreate, HackerEventRegistration +from src.impl.Event.schema import EventCreate, HackerEventRegistration, HackerEventRegistrationUpdate from src.impl.Event.schema import EventGet from src.impl.Event.schema import EventGetAll from src.impl.Event.schema import EventUpdate @@ -142,6 +142,15 @@ def register_hacker(id: int, return {'success': True, 'event_id': event.id, 'user_id': hacker_id} +@router.put("/{id}/update-register/{hacker_id}") +def register_hacker(id: int, + hacker_id: int, + payload: HackerEventRegistrationUpdate, + token: BaseToken = Depends(JWTBearer())): + event = event_service.update_register(id, hacker_id, payload, token) + return {'success': True, 'event_id': event.id, 'user_id': hacker_id} + + # @router.delete("/{id}/participants/{hacker_id}") # def remove_event_participant(id: int, # hacker_id: int, diff --git a/src/impl/Event/service.py b/src/impl/Event/service.py index f6429aee..1b46aa59 100644 --- a/src/impl/Event/service.py +++ b/src/impl/Event/service.py @@ -227,6 +227,30 @@ def add_hacker(self, event_id: int, hacker_id: int, db.session.commit() db.session.refresh(event) return event + + @BaseService.needs_service(UserService) + def update_register(self, event_id: int, hacker_id: int, + payload: HackerEventRegistration, data: BaseToken): + if not data.check([UserType.LLEIDAHACKER]) and not data.check( + [UserType.HACKER], hacker_id): + raise AuthenticationException("Not authorized") + event = self.get_by_id(event_id) + if event.archived: + raise InvalidDataException( + "Unable to operate with an archived event, unarchive it first") + if not data.is_admin: + if event.max_participants <= len(event.registered_hackers): + raise InvalidDataException("Event is full") + hacker = self.user_service.get_by_id(hacker_id) + if hacker not in event.registered_hackers: + raise InvalidDataException('Hacker is not registered') + reg = db.session.query(HackerEventRegistration).filter(HackerRegistration.user_id == hacker_id, HackerRegistration.event_id == event_id).first() + if reg is None: + raise InvalidDataException('Hacker is not registered') + set_existing_data(reg, payload) + db.session.commit() + db.session.refresh(event) + return event @BaseService.needs_service('HackerGroupService') def add_hacker_group(self, id: int, hacker_group_id: int, data: BaseToken):