87 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			87 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
from sqlalchemy import select, and_
 | 
						||
 | 
						||
from models import Attribute, AttributeType
 | 
						||
from schemas.attribute import *
 | 
						||
from services.base import BaseService
 | 
						||
 | 
						||
 | 
						||
class AttributeService(BaseService):
 | 
						||
    async def get_all(self) -> GetAttributesResponse:
 | 
						||
        stmt = (
 | 
						||
            select(Attribute)
 | 
						||
            .where(Attribute.is_deleted == False)
 | 
						||
            .order_by(Attribute.label)
 | 
						||
        )
 | 
						||
        attrs = (await self.session.scalars(stmt)).all()
 | 
						||
        return GetAttributesResponse(attributes=attrs)
 | 
						||
 | 
						||
    async def get_types(self) -> GetAttributeTypesResponse:
 | 
						||
        stmt = (
 | 
						||
            select(AttributeType)
 | 
						||
            .where(AttributeType.is_deleted == False)
 | 
						||
        )
 | 
						||
        types = (await self.session.scalars(stmt)).all()
 | 
						||
        return GetAttributeTypesResponse(types=types)
 | 
						||
 | 
						||
    async def get_attr_by_name(self, attr_name: str) -> Optional[Attribute]:
 | 
						||
        stmt = (
 | 
						||
            select(Attribute)
 | 
						||
            .where(
 | 
						||
                and_(
 | 
						||
                    Attribute.name == attr_name,
 | 
						||
                    Attribute.is_deleted == False,
 | 
						||
                )
 | 
						||
            )
 | 
						||
        )
 | 
						||
        attribute = (await self.session.scalars(stmt)).first()
 | 
						||
        return attribute
 | 
						||
 | 
						||
    async def create(self, request: CreateAttributeRequest) -> CreateAttributeResponse:
 | 
						||
        existing_attr = await self.get_attr_by_name(request.attribute.name)
 | 
						||
        if existing_attr:
 | 
						||
            return CreateAttributeResponse(ok=False, message="Атрибут с данным уникальным ключом уже существует")
 | 
						||
 | 
						||
        default_value = pickle.dumps(request.attribute.default_value)
 | 
						||
        values = request.attribute.model_dump()
 | 
						||
        del values["default_value"]
 | 
						||
 | 
						||
        attribute = Attribute(
 | 
						||
            **values,
 | 
						||
            default_value=default_value,
 | 
						||
        )
 | 
						||
        self.session.add(attribute)
 | 
						||
        await self.session.commit()
 | 
						||
        return CreateAttributeResponse(ok=True, message="Атрибут успешно создан")
 | 
						||
 | 
						||
    async def update(self, request: UpdateAttributeRequest) -> UpdateAttributeResponse:
 | 
						||
        attribute = await self.session.get(Attribute, request.attribute.id)
 | 
						||
        if not attribute:
 | 
						||
            return UpdateAttributeResponse(ok=False, message=f"Атрибут с ID {request.attribute.id} не найден")
 | 
						||
 | 
						||
        if attribute.name != request.attribute.name:
 | 
						||
            attr_with_same_name = await self.get_attr_by_name(request.attribute.name)
 | 
						||
            if attr_with_same_name:
 | 
						||
                return CreateAttributeResponse(ok=False, message="Атрибут с данным уникальным ключом уже существует")
 | 
						||
 | 
						||
        default_value = pickle.dumps(request.attribute.default_value) if request.attribute.default_value else None
 | 
						||
 | 
						||
        attribute.name = request.attribute.name
 | 
						||
        attribute.label = request.attribute.label
 | 
						||
        attribute.default_value = default_value
 | 
						||
        attribute.is_applicable_to_group = request.attribute.is_applicable_to_group
 | 
						||
        attribute.is_shown_on_dashboard = request.attribute.is_shown_on_dashboard
 | 
						||
        attribute.is_nullable = request.attribute.is_nullable
 | 
						||
        attribute.description = request.attribute.description
 | 
						||
        await self.session.commit()
 | 
						||
 | 
						||
        return UpdateAttributeResponse(ok=True, message="Атрибут успешно обновлен")
 | 
						||
 | 
						||
    async def delete(self, attribute_id: int) -> DeleteAttributeResponse:
 | 
						||
        attribute: Optional[Attribute] = await self.session.get(Attribute, attribute_id)
 | 
						||
        if not attribute:
 | 
						||
            return DeleteAttributeResponse(ok=False, message=f"Атрибут с ID {attribute_id} не найден")
 | 
						||
 | 
						||
        attribute.is_deleted = True
 | 
						||
        await self.session.commit()
 | 
						||
        return DeleteAttributeResponse(ok=True, message=f"Атрибут успешно удален")
 |