From ca983f1199d2dcba1df94db81c25426eca5ab5bc Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 21 Dec 2024 21:38:14 +0100 Subject: [PATCH] add routers tags --- app/routers/tags.py | 230 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 230 insertions(+) create mode 100644 app/routers/tags.py diff --git a/app/routers/tags.py b/app/routers/tags.py new file mode 100644 index 0000000..c0eab93 --- /dev/null +++ b/app/routers/tags.py @@ -0,0 +1,230 @@ +from fastapi import APIRouter, Depends, HTTPException, status, Response +from fastapi.responses import JSONResponse +from datetime import datetime +from ..dependencies import users_token, permissions_checker, database +from ..models import tags, users +from pydantic import EmailStr +from typing import Annotated, Union +from bson import ObjectId +from datetime import datetime +router = APIRouter() + + +@router.get("/tags", tags=["tags"], response_model=list[tags.TagsOut]) +async def read_tags( + authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], + skip: int = 0, + limit: int = 20, + id_tags: str | None = None, + name: str | None = None +): + # Validate `skip` and `limit` + if limit < 1 or skip < 0 or limit < skip: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="`skip` should be >= 0 and `limit` should be > 0 and greater than `skip`.", + ) + limit = limit + skip + + # Initialize filters + filters = [] + + + if name: + filters.append({"name": {"$eq": name, "$options": "i"}}) + + # Add ID filter + if id_tags: + try: + tags_id = ObjectId(id_tags) + filters.append({"_id": {"$eq": event_id}}) + except Exception: + raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid event ID format.") + + # Combine all filters + object_search = {"$and": filters} if filters else {} + + # Fetch and return results + tags_repository = tags.TagsRepository(database=database.database) + list_tags = [] + for tag_index in tags_repository.find_by(object_search, limit=limit, skip=skip): + tag = tags.TagsOut( + id=tag_index.id, + name=tag_index.name + ) + list_tags.append(event) + + return list_tags + +@router.get("/tags/count", tags=["tags"]) +async def read_tags_count(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))]): + count = database.database.get_collection("tags").estimated_document_count() + content = {"count":count} + response = JSONResponse(content=content) + return response + + +@router.get("/tags/{item_id}", tags=["tags"], response_model=tags.Tags) +async def read_tags_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))]): + tags_repository = tags.TagsRepository(database=database.database) + tag = tags_repository.find_one_by_id(ObjectId(item_id)) + return tag + + +@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: + 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) + + 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) + response = JSONResponse(content=content) + return response + +@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: + 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({"name": {'$eq': eventSingle.name}}) + if event 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"} + 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