add tag in put endpiint

This commit is contained in:
Valentin CZERYBA 2024-12-21 23:05:24 +01:00
parent a893e86cdf
commit 2a194c163a

View File

@ -2,7 +2,7 @@ from fastapi import APIRouter, Depends, HTTPException, status, Response
from fastapi.responses import JSONResponse from fastapi.responses import JSONResponse
from datetime import datetime from datetime import datetime
from ..dependencies import users_token, permissions_checker, database from ..dependencies import users_token, permissions_checker, database
from ..models import events, users from ..models import events, users, tags
from pydantic import EmailStr from pydantic import EmailStr
from typing import Annotated, Union from typing import Annotated, Union
from bson import ObjectId from bson import ObjectId
@ -330,6 +330,7 @@ async def update_events(authorize: Annotated[bool, Depends(permissions_checker.P
detail="Body request is empty" detail="Body request is empty"
) )
event_repository = events.EventRepository(database=database.database) event_repository = events.EventRepository(database=database.database)
tags_repository = tags.TagsRepository(database=database.database)
event = event_repository.find_one_by({"name": {'$eq': eventSingle.name}}) event = event_repository.find_one_by({"name": {'$eq': eventSingle.name}})
if event is not None: if event is not None:
@ -347,6 +348,11 @@ async def update_events(authorize: Annotated[bool, Depends(permissions_checker.P
event.longitude = eventSingle.longitude event.longitude = eventSingle.longitude
event.imgUrl = eventSingle.imgUrl event.imgUrl = eventSingle.imgUrl
event.tags = eventSingle.tags event.tags = eventSingle.tags
for tag_name in eventSingle:
tag = tags_repository.find_one_by({"name": {'$eq': tag_name}})
if tag is None:
tag = Tags(name=tag_name)
tags_repository.save(tag)
event.status = 1 event.status = 1
event.created_at = datetime.today() event.created_at = datetime.today()
event_repository.save(event) event_repository.save(event)
@ -363,7 +369,7 @@ async def update_events_id(item_id: str, authorize: Annotated[bool, Depends(perm
detail="Body request is empty" detail="Body request is empty"
) )
event_repository = events.EventRepository(database=database.database) event_repository = events.EventRepository(database=database.database)
tags_repository = tags.TagsRepository(database=database)
event = event_repository.find_one_by({"id": {'$eq': ObjectId(item_id)}}) event = event_repository.find_one_by({"id": {'$eq': ObjectId(item_id)}})
if event is None: if event is None:
raise HTTPException( raise HTTPException(
@ -378,6 +384,11 @@ async def update_events_id(item_id: str, authorize: Annotated[bool, Depends(perm
event.end_date = eventSingle.end_date event.end_date = eventSingle.end_date
event.organizers = eventSingle.organizers event.organizers = eventSingle.organizers
event.tags = eventSingle.tags event.tags = eventSingle.tags
for tag_name in eventSingle:
tag = tags_repository.find_one_by({"name": {'$eq': tag_name}})
if tag is None:
tag = Tags(name=tag_name)
tags_repository.save(tag)
event.latitude = eventSingle.latitude event.latitude = eventSingle.latitude
event.longitude = eventSingle.longitude event.longitude = eventSingle.longitude
event.updated_at = datetime.today() event.updated_at = datetime.today()