30 lines
1.1 KiB
Python
30 lines
1.1 KiB
Python
import uuid
|
|
|
|
from fastapi import HTTPException
|
|
|
|
from app.models.grand_project import GrandProjectModel
|
|
from core.repositories.base import BaseRepository
|
|
from core.utils.crudify import mongo_inserter, mongo_updater, ConfigMongoWriter
|
|
|
|
COLLECTION = "grand_project"
|
|
class GrandProjectRepository(BaseRepository[GrandProjectModel]):
|
|
def __init__(self):
|
|
super().__init__(collection=COLLECTION, model=GrandProjectModel)
|
|
|
|
def check_weight(self,_inp:GrandProjectModel):
|
|
sum = 0
|
|
for project in _inp.assign_project:
|
|
sum += project.weight
|
|
print(sum)
|
|
if sum != 100:
|
|
raise HTTPException(422, 'مجموعه وزن ها باید صد درصد باشد')
|
|
async def create(self, create: GrandProjectModel):
|
|
self.check_weight(create)
|
|
config = ConfigMongoWriter(
|
|
has_owner= False
|
|
)
|
|
return await mongo_inserter(create, self.COLLECTION, options=config,getter=self.get)
|
|
|
|
async def update(self,_id:uuid.UUID, update:GrandProjectModel):
|
|
self.check_weight(update)
|
|
return await mongo_updater(update,_id,COLLECTION,getter=self.get) |