вынес в повторяющийся код в отдельные функци в сервисах, изменил обновление и создание контента набора

This commit is contained in:
Андрей Дувакин 2025-02-22 19:41:35 +05:00
parent c3ddff6173
commit 03c67c4d2f
8 changed files with 253 additions and 244 deletions

View File

@ -25,6 +25,12 @@ class SetContentRepository:
result = await self.db.execute(stmt)
return result.scalars().all()
async def create_list(self, sets_content: list[SetContent]) -> list[SetContent]:
self.db.add_all(sets_content)
await self.db.commit()
await self.db.refresh(sets_content)
return sets_content
async def create(self, set_content: SetContent) -> SetContent:
self.db.add(set_content)
await self.db.commit()
@ -40,3 +46,8 @@ class SetContentRepository:
await self.db.delete(set_content)
await self.db.commit()
return set_content
async def delete_list_sets(self, sets_content: list[SetContent]) -> list[SetContent]:
await self.db.delete(sets_content)
await self.db.commit()
return sets_content

View File

@ -25,46 +25,32 @@ async def get_set_content_by_set_id(
@router.post(
'/set_content/',
response_model=SetContentEntity,
summary='Create a new set content',
description='Create a new set content',
'/set_content/{set_id}/',
response_model=list[SetContentEntity],
summary='Create a new set content by set_id',
description='Create a new set content by set_id',
)
async def create_set_content(
set_content: SetContentEntity,
set_id: int,
set_content: list[SetContentEntity],
db: AsyncSession = Depends(get_db),
user=Depends(get_current_user),
):
set_content_service = SetContentService(db)
return await set_content_service.create_set_content(set_content)
return await set_content_service.create_list_sets(set_id, set_content)
@router.put(
'/set_content/{set_content_id}/',
response_model=SetContentEntity,
summary='Update a set content',
description='Update a set content',
'/set_content/{set_id}/',
response_model=list[SetContentEntity],
summary='Update a set content by set_id',
description='Update a set content by set_id',
)
async def update_set_content(
set_content_id: int,
set_content: SetContentEntity,
set_id: int,
set_content: list[SetContentEntity],
db: AsyncSession = Depends(get_db),
user=Depends(get_current_user),
):
set_content_service = SetContentService(db)
return await set_content_service.update_set_content(set_content_id, set_content)
@router.delete(
'/set_content/{set_content_id}/',
response_model=SetContentEntity,
summary='Delete set content',
description='Delete an existing set content',
)
async def delete_set_content(
set_content_id: int,
db: AsyncSession = Depends(get_db),
user=Depends(get_current_user),
):
set_content_service = SetContentService(db)
return await set_content_service.delete_set_content(set_content_id)
return await set_content_service.update_set_content_by_set_id(set_id, set_content)

View File

@ -10,3 +10,4 @@ class LensType(BaseModel):
title = Column(VARCHAR(150), nullable=False, unique=True)
lenses = relationship('Lens', back_populates='type')
contents = relationship('SetContent', back_populates='type')

View File

@ -21,5 +21,5 @@ class SetContent(BaseModel):
type_id = Column(Integer, ForeignKey('lens_types.id'), nullable=False)
set_id = Column(Integer, ForeignKey('sets.id'), nullable=False)
type = relationship('LensType', back_populates='lenses')
type = relationship('LensType', back_populates='contents')
set = relationship('Set', back_populates='contents')

View File

@ -18,20 +18,9 @@ class LensesService:
async def get_all_lenses(self) -> list[LensEntity]:
lenses = await self.lenses_repository.get_all()
return [
LensEntity(
id=lens.id,
tor=lens.tor,
trial=lens.trial,
esa=lens.esa,
fvc=lens.fvc,
preset_refraction=lens.preset_refraction,
diameter=lens.diameter,
periphery_toricity=lens.periphery_toricity,
side=lens.side,
issued=lens.issued,
type_id=lens.type_id,
)
self.model_to_entity(lens)
for lens in lenses
]
@ -44,41 +33,11 @@ class LensesService:
detail='The lens type with this ID was not found',
)
try:
side_enum = SideEnum(lens.side)
except ValueError:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Invalid side value: {lens.side}. Must be 'левая' or 'правая'."
)
lens_model = Lens(
tor=lens.tor,
trial=lens.trial,
esa=lens.esa,
fvc=lens.fvc,
preset_refraction=lens.preset_refraction,
diameter=lens.diameter,
periphery_toricity=lens.periphery_toricity,
side=side_enum,
type_id=lens.type_id,
)
lens_model = self.entity_to_model(lens)
await self.lenses_repository.create(lens_model)
return LensEntity(
id=lens_model.id,
tor=lens_model.tor,
trial=lens_model.trial,
esa=lens_model.esa,
fvc=lens_model.fvc,
preset_refraction=lens_model.preset_refraction,
diameter=lens_model.diameter,
periphery_toricity=lens_model.periphery_toricity,
side=lens_model.side.value,
issued=lens_model.issued,
type_id=lens_model.type_id,
)
return self.model_to_entity(lens_model)
async def update_lens(self, lens_id: int, lens: LensEntity) -> LensEntity:
lens_model = await self.lenses_repository.get_by_id(lens_id)
@ -107,6 +66,48 @@ class LensesService:
await self.lenses_repository.update(lens_model)
return self.model_to_entity(lens_model)
async def delete_lens(self, lens_id: int) -> Optional[LensEntity]:
lens = await self.lenses_repository.get_by_id(lens_id)
if not lens:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Lens not found")
result = await self.lenses_repository.delete(lens)
return self.model_to_entity(result)
@staticmethod
def entity_to_model(lens: LensEntity) -> Lens:
try:
side_enum = SideEnum(lens.side)
except ValueError:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Invalid side value: {lens.side}. Must be 'левая' or 'правая'."
)
lens_model = Lens(
tor=lens.tor,
trial=lens.trial,
esa=lens.esa,
fvc=lens.fvc,
preset_refraction=lens.preset_refraction,
diameter=lens.diameter,
periphery_toricity=lens.periphery_toricity,
side=side_enum,
type_id=lens.type_id,
)
if lens.id is not None:
lens.id = lens.id
return lens_model
@staticmethod
def model_to_entity(lens_model: Lens) -> LensEntity:
return LensEntity(
id=lens_model.id,
tor=lens_model.tor,
@ -120,25 +121,3 @@ class LensesService:
issued=lens_model.issued,
type_id=lens_model.type_id,
)
async def delete_lens(self, lens_id: int) -> Optional[LensEntity]:
lens = await self.lenses_repository.get_by_id(lens_id)
if not lens:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Lens not found")
result = await self.lenses_repository.delete(lens)
return LensEntity(
id=result.id,
tor=result.tor,
trial=result.trial,
esa=result.esa,
fvc=result.fvc,
preset_refraction=result.preset_refraction,
diameter=result.diameter,
periphery_toricity=result.periphery_toricity,
side=result.side.value,
issued=result.issued,
type_id=result.type_id,
)

View File

@ -16,46 +16,16 @@ class PatientsService:
async def get_all_patients(self) -> list[PatientEntity]:
patients = await self.patient_repository.get_all()
return [
PatientEntity(
id=patient.id,
first_name=patient.first_name,
last_name=patient.last_name,
patronymic=patient.patronymic,
birthday=patient.birthday,
address=patient.address,
email=patient.email,
phone=patient.phone,
diagnosis=patient.diagnosis,
correction=patient.correction,
)
self.model_to_entity(patient)
for patient in patients
]
async def create_patient(self, patient: PatientEntity) -> PatientEntity:
patient_model = Patient(
first_name=patient.first_name,
last_name=patient.last_name,
patronymic=patient.patronymic,
birthday=patient.birthday,
address=patient.address,
email=patient.email,
phone=patient.phone,
diagnosis=patient.diagnosis,
correction=patient.correction,
)
patient_model = self.entity_to_model(patient)
await self.patient_repository.create(patient_model)
return PatientEntity(
id=patient_model.id,
first_name=patient_model.first_name,
last_name=patient_model.last_name,
patronymic=patient_model.patronymic,
birthday=patient_model.birthday,
address=patient_model.address,
email=patient_model.email,
phone=patient_model.phone,
diagnosis=patient_model.diagnosis,
correction=patient_model.correction,
)
return self.model_to_entity(patient_model)
async def update_patient(self, patient_id: int, patient: PatientEntity) -> Optional[PatientEntity]:
patient_model = await self.patient_repository.get_by_id(patient_id)
@ -72,20 +42,10 @@ class PatientsService:
patient_model.phone = patient.phone
patient_model.diagnosis = patient.diagnosis
patient_model.correction = patient.correction
await self.patient_repository.update(patient_model)
return PatientEntity(
id=patient_model.id,
first_name=patient_model.first_name,
last_name=patient_model.last_name,
patronymic=patient_model.patronymic,
birthday=patient_model.birthday,
address=patient_model.address,
email=patient_model.email,
phone=patient_model.phone,
diagnosis=patient_model.diagnosis,
correction=patient_model.correction,
)
await self.patient_repository.update(patient_model)
return self.model_to_entity(patient_model)
async def delete_patient(self, patient_id: int) -> Optional[PatientEntity]:
patient = await self.patient_repository.get_by_id(patient_id)
@ -95,15 +55,38 @@ class PatientsService:
result = await self.patient_repository.delete(patient)
return self.model_to_entity(result)
@staticmethod
def model_to_entity(patient: Patient) -> PatientEntity:
return PatientEntity(
id=result.id,
first_name=result.first_name,
last_name=result.last_name,
patronymic=result.patronymic,
birthday=result.birthday,
address=result.address,
email=result.email,
phone=result.phone,
diagnosis=result.diagnosis,
correction=result.correction,
id=patient.id,
first_name=patient.first_name,
last_name=patient.last_name,
patronymic=patient.patronymic,
birthday=patient.birthday,
address=patient.address,
email=patient.email,
phone=patient.phone,
diagnosis=patient.diagnosis,
correction=patient.correction,
)
@staticmethod
def entity_to_model(patient: PatientEntity) -> Patient:
patient_model = Patient(
first_name=patient.first_name,
last_name=patient.last_name,
patronymic=patient.patronymic,
birthday=patient.birthday,
address=patient.address,
email=patient.email,
phone=patient.phone,
diagnosis=patient.diagnosis,
correction=patient.correction,
)
if patient.id is not None:
patient_model.id = patient.id
return patient_model

View File

@ -20,26 +20,14 @@ class SetContentService:
async def get_all_set_content(self) -> list[SetContentEntity]:
set_content = await self.set_content_repository.get_all()
return [
SetContentEntity(
id=content.id,
tor=content.tor,
trial=content.trial,
esa=content.esa,
fvc=content.fvc,
preset_refraction=content.preset_refraction,
diameter=content.diameter,
periphery_toricity=content.periphery_toricity,
side=content.side,
count=content.count,
type_id=content.type_id,
set_id=content.set_id,
)
self.model_to_entity(content)
for content in set_content
]
async def get_content_by_set_id(self, set_id: int) -> Optional[list[SetContentEntity]]:
_set = self.set_repository.get_by_id(set_id)
_set = await self.set_repository.get_by_id(set_id)
if not _set:
raise HTTPException(
@ -50,24 +38,50 @@ class SetContentService:
set_content = await self.set_content_repository.get_by_set_id(set_id)
return [
SetContentEntity(
id=content.id,
tor=content.tor,
trial=content.trial,
esa=content.esa,
fvc=content.fvc,
preset_refraction=content.preset_refraction,
diameter=content.diameter,
periphery_toricity=content.periphery_toricity,
side=content.side,
count=content.count,
type_id=content.type_id,
set_id=content.set_id,
)
self.model_to_entity(content)
for content in set_content
]
async def create_set_content(self, set_content: SetContentEntity) -> SetContentEntity:
async def create_list_sets(self, set_id: int, sets_content: list[SetContentEntity]) -> list[SetContentEntity]:
_set = await self.set_repository.get_by_id(set_id)
if not _set:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail='The set with this ID was not found',
)
sets_content_models = []
for content in sets_content:
lens_type = await self.lens_types_repository.get_by_id(content.type_id)
if not lens_type:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail='The lens type with this ID was not found',
)
sets_content_models.append(
self.entity_to_model(content, set_id)
)
await self.set_content_repository.create_list(sets_content_models)
return [
self.model_to_entity(content)
for content in sets_content_models
]
async def create_set_content(self, set_id: int, set_content: SetContentEntity) -> SetContentEntity:
_set = await self.set_repository.get_by_id(set_id)
if not _set:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail='The set with this ID was not found',
)
lens_type = await self.lens_types_repository.get_by_id(set_content.type_id)
if not lens_type:
@ -76,7 +90,7 @@ class SetContentService:
detail='The lens type with this ID was not found',
)
_set = self.set_repository.get_by_id(set_content.set_id)
_set = await self.set_repository.get_by_id(set_content.set_id)
if not _set:
raise HTTPException(
@ -84,46 +98,44 @@ class SetContentService:
detail='The set with this ID was not found',
)
try:
side_enum = SideEnum(set_content.side)
except ValueError:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Invalid side value: {set_content.side}. Must be 'левая' or 'правая'."
)
set_content_model = SetContent(
tor=set_content.tor,
trial=set_content.trial,
esa=set_content.esa,
fvc=set_content.fvc,
preset_refraction=set_content.preset_refraction,
diameter=set_content.diameter,
periphery_toricity=set_content.periphery_toricity,
side=side_enum,
count=set_content.count,
type_id=set_content.type_id,
set_id=set_content.set_id,
)
set_content_model = self.entity_to_model(set_content, set_id)
await self.set_content_repository.create(set_content_model)
return SetContentEntity(
id=set_content_model.id,
tor=set_content_model.tor,
trial=set_content_model.trial,
esa=set_content_model.esa,
fvc=set_content_model.fvc,
preset_refraction=set_content_model.preset_refraction,
diameter=set_content_model.diameter,
periphery_toricity=set_content_model.periphery_toricity,
side=set_content_model.side.value,
count=set_content_model.count,
type_id=set_content_model.type_id,
set_id=set_content_model.set_id,
return self.model_to_entity(set_content_model)
async def update_set_content_by_set_id(self, set_id: int, sets_content: list[SetContentEntity]) -> list[
SetContentEntity
]:
_set = await self.set_repository.get_by_id(set_id)
if not _set:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail='The set with this ID was not found',
)
old_set_content = await self.set_content_repository.get_by_set_id(set_id)
await self.set_content_repository.delete_list_sets(
list(old_set_content)
)
async def update_set_content(self, set_content_id: int, set_content: SetContentEntity):
result = []
for content in sets_content:
model_content = self.entity_to_model(content)
model_content = await self.set_content_repository.create(model_content)
result.append(model_content)
return [
self.model_to_entity(content)
for content in result
]
async def update_set_content(self, set_content_id: int, set_content: SetContentEntity) -> Optional[
SetContentEntity
]:
set_content_model = await self.set_content_repository.get_by_id(set_content_id)
if not set_content_model:
@ -159,6 +171,53 @@ class SetContentService:
await self.set_content_repository.update(set_content_model)
return self.model_to_entity(set_content_model)
async def delete_set_content(self, set_content_id: int) -> Optional[SetContentEntity]:
set_content = await self.set_content_repository.get_by_id(set_content_id)
if not set_content:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Set content not found")
result = await self.set_content_repository.delete(set_content)
return self.model_to_entity(result)
@staticmethod
def entity_to_model(set_content: SetContentEntity, set_id=None) -> SetContent:
if set_id is None:
set_id = set_content.set_id
try:
side_enum = SideEnum(set_content.side)
except ValueError:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Invalid side value: {set_content.side}. Must be 'левая' or 'правая'."
)
set_content_model = SetContent(
tor=set_content.tor,
trial=set_content.trial,
esa=set_content.esa,
fvc=set_content.fvc,
preset_refraction=set_content.preset_refraction,
diameter=set_content.diameter,
periphery_toricity=set_content.periphery_toricity,
side=side_enum,
count=set_content.count,
type_id=set_content.type_id,
set_id=set_id,
)
if set_content.id is not None:
set_content_model.id = set_content.id
return set_content_model
@staticmethod
def model_to_entity(set_content_model: SetContent) -> SetContentEntity:
return SetContentEntity(
id=set_content_model.id,
tor=set_content_model.tor,
@ -173,26 +232,3 @@ class SetContentService:
type_id=set_content_model.type_id,
set_id=set_content_model.set_id,
)
async def delete_set_content(self, set_content_id: int) -> Optional[SetContentEntity]:
set_content = await self.set_content_repository.get_by_id(set_content_id)
if not set_content:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Set content not found")
result = await self.set_content_repository.delete(set_content)
return SetContentEntity(
id=result.id,
tor=result.tor,
trial=result.trial,
esa=result.esa,
fvc=result.fvc,
preset_refraction=result.preset_refraction,
diameter=result.diameter,
periphery_toricity=result.periphery_toricity,
side=result.side.value,
count=result.count,
type_id=result.type_id,
set_id=result.set_id,
)

View File

@ -30,7 +30,7 @@ class SetsService:
await self.sets_repository.create(set_model)
return SetEntity(
id=set_model.id,
title=set_model.id,
title=set_model.title,
)
async def update_set(self, set_id: int, _set: SetEntity) -> SetEntity:
@ -60,3 +60,16 @@ class SetsService:
id=result.id,
title=result.title,
)
@staticmethod
def model_to_entity(_set: Set) -> SetEntity:
return SetEntity(
id=_set.id,
title=_set.title,
)
@staticmethod
def entity_to_model(_set: SetEntity) -> Set:
set_model = Set(
title=_set.title,
)