From a893e86cdf6d91eb4b30fd4f32583758a113bc3f Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 21 Dec 2024 22:38:21 +0100 Subject: [PATCH] add tags rouger work --- app/main.py | 3 +- app/models/tags.py | 2 +- app/routers/tags.py | 159 +++++++------------------------------------- 3 files changed, 27 insertions(+), 137 deletions(-) diff --git a/app/main.py b/app/main.py index be6f657..0d88ef2 100644 --- a/app/main.py +++ b/app/main.py @@ -1,7 +1,7 @@ from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware -from .routers import users, token, mail, events +from .routers import users, token, mail, events, tags from .dependencies import user_add import os @@ -29,6 +29,7 @@ app.include_router(users.router) app.include_router(token.router) app.include_router(mail.router) app.include_router(events.router) +app.include_router(tags.router) @app.on_event("startup") diff --git a/app/models/tags.py b/app/models/tags.py index db00299..1fcf75d 100644 --- a/app/models/tags.py +++ b/app/models/tags.py @@ -15,7 +15,7 @@ class TagsOut(BaseModel): class TagsIn(BaseModel): name: str -class EventIDS(BaseModel): +class TagsIDS(BaseModel): ids: list[str] diff --git a/app/routers/tags.py b/app/routers/tags.py index c0eab93..aa965ec 100644 --- a/app/routers/tags.py +++ b/app/routers/tags.py @@ -72,159 +72,48 @@ async def read_tags_id(item_id : str, authorize: Annotated[bool, Depends(permiss @router.delete("/tags/groups",tags=["tags"]) -async def delete_tags_groups(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], remove: bool = False, tagsids: tags.TagsIDS | None = None): - if len(eventids.ids) == 0: +async def delete_tags_groups(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], tagsids: tags.TagsIDS | None = None): + if len(tagsids.ids) == 0: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="eventids should be greater than 0" ) - event_repository = event.EventRepository(database=database.database) - for i in eventids.ids: - event = event_repository.find_one_by_id(ObjectId(i)) - if remove is True: - event.status = -1 - event.deleted_at = datetime.today() - content = {"message": "events are deleted "} - else: - event.status = 0 - event.disabled_at = datetime.today() - content = {"message": "events are disabled"} - event_repository.save(event) - + tag_repository = tags.TagsRepository(database=database.database) + for i in tagsids.ids: + tag = tag_repository.find_one_by_id(ObjectId(i)) + tag_repository.delete_one(tag) + content = {"message": "tags removed"} response = JSONResponse(content=content) return response -@router.delete("/events/{item_id}", tags=["events"]) -async def delete_events_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], remove : bool = False): - event_repository = events.EventRepository(database=database.database) - event = event_repository.find_one_by_id(ObjectId(item_id)) - if remove is True: - event.status = -1 - event.deleted_at = datetime.today() - content = {"message": "events are deleted"} - else: - event.status = 0 - event.disabled_at = datetime.today() - content = {"message": "events are disabled"} - event_repository.save(event) +@router.delete("/tags/{item_id}", tags=["tags"]) +async def delete_tags_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))]): + tag_repository = tags.TagsRepository(database=database.database) + tag = tag_repository.find_one_by_id(ObjectId(item_id)) + event_repository.delete_one(event) + content = {"message": "tags delete"} response = JSONResponse(content=content) - return response + return responsed -@router.put("/events/me/{item_id}",tags=["events"]) -async def update_events_me(item_id: str, current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], eventSingle: events.EventIn | None = None): - event_repository = event.EventRepository(database=database.database) - event = event_repository.find_one_by_id(ObjectId(item_id)) - event.name = eventSingle.name - event.description = eventSingle.description - event.place = eventSingle.place - event.start_date = eventSingle.start_date - event.tags = eventSingle.tags - event.end_date = eventSingle.end_date - event.latitude = eventSingle.latitude - event.longitude = eventSingle.longitude - event.updated_at = datetime.today() - event.imgUrl = eventSingle.imgUrl - event_repository.save(event) - content = {"message": "event is updated"} - response = JSONResponse(content=content) - return response - -@router.put("/events", tags=["events"], status_code=status.HTTP_201_CREATED) -async def update_events(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], eventSingle: events.EventIn | None = None): - if eventSingle is None: +@router.put("/tags", tags=["tags"], status_code=status.HTTP_201_CREATED) +async def update_tags(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], tagSingle: tags.TagsIn | None = None): + if tagSingle is None: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="Body request is empty" ) - event_repository = events.EventRepository(database=database.database) + tag_repository = tags.TagsRepository(database=database.database) - event = event_repository.find_one_by({"name": {'$eq': eventSingle.name}}) - if event is not None: + tag = tag_repository.find_one_by({"name": {'$eq': tagSingle.name}}) + if tag is not None: raise HTTPException( status_code=status.HTTP_204_NO_CONTENT, detail="name" ) - - - event = events.Event(name=eventSingle.name, description=eventSingle.description, place=eventSingle.place) - event.start_date = eventSingle.start_date - event.end_date = eventSingle.end_date - event.organizers = eventSingle.organizers - event.latitude = eventSingle.latitude - event.longitude = eventSingle.longitude - event.imgUrl = eventSingle.imgUrl - event.tags = eventSingle.tags - event.status = 1 - event.created_at = datetime.today() - event_repository.save(event) - content = {"message": "event is created"} + tag = tags.Tags(name=tagSingle.name) + tag.created_at = datetime.today() + tag_repository.save(event) + content = {"message": "tags created"} response = JSONResponse(content=content, status_code=status.HTTP_201_CREATED) return response - - -@router.put("/events/{item_id}", tags=["events"], status_code=status.HTTP_200_OK) -async def update_events_id(item_id: str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], eventSingle: events.EventIn | None = None, response: Response = Response): - if eventSingle is None: - raise HTTPException( - status_code=status.HTTP_400_BAD_REQUEST, - detail="Body request is empty" - ) - event_repository = events.EventRepository(database=database.database) - - event = event_repository.find_one_by({"id": {'$eq': ObjectId(item_id)}}) - if event is None: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="Event not found" - ) - - event.name = eventSingle.name - event.place = eventSingle.place - event.description = eventSingle.description - event.start_date = eventSingle.start_date - event.end_date = eventSingle.end_date - event.organizers = eventSingle.organizers - event.tags = eventSingle.tags - event.latitude = eventSingle.latitude - event.longitude = eventSingle.longitude - event.updated_at = datetime.today() - event.imgUrl = eventSingle.imgUrl - event_repository.save(event) - content = {"message": "event is updated"} - response = JSONResponse(content=content) - return response - - -@router.patch("/events/groups",tags=["events"]) -async def patch_events_groups(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], eventids: events.EventIDS | None = None): - if len(eventids.ids) == 0: - raise HTTPException( - status_code=status.HTTP_400_BAD_REQUEST, - detail="eventids should be greater than 0" - ) - - event_repository = events.EventRepository(database=database.database) - content = {"message": "events are enabled"} - for i in eventids.ids: - event = event_repository.find_one_by_id(ObjectId(i)) - event.status = 1 - event.disabled_at = None - event.deleted_at = None - event_repository.save(event) - - - response = JSONResponse(content=content) - return response - -@router.patch("/events/{item_id}", tags=["events"]) -async def patch_events_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))]): - event_repository = events.EventRepository(database=database.database) - event = event_repository.find_one_by_id(ObjectId(item_id)) - event.status = 1 - event.disabled_at = None - event.deleted_at = None - event_repository.save(event) - content = {"message": "event is enabled"} - response = JSONResponse(content=content) - return response \ No newline at end of file