69 lines
2.4 KiB
Python
69 lines
2.4 KiB
Python
from pydantic import BaseModel, EmailStr, Field, validator
|
|
from datetime import datetime
|
|
from typing import Optional
|
|
#
|
|
import re
|
|
from enum import Enum
|
|
|
|
from sqlalchemy.orm import Session
|
|
from datetime import datetime
|
|
from typing import Optional
|
|
from fastapi import HTTPException
|
|
from app.modules.location.models import Locations
|
|
from app.modules.location.schema import LocationBase, LocationUpdate
|
|
|
|
class LocationsService:
|
|
@staticmethod
|
|
def create_locations(db: Session, data:LocationBase ):
|
|
new_locations = Locations(**data.dict())
|
|
db.add(new_locations)
|
|
db.commit()
|
|
db.refresh(new_locations)
|
|
return new_locations
|
|
|
|
@staticmethod
|
|
def get_locations(db: Session, locations_id: int, current_user):
|
|
locations = db.query(Locations).filter(Locations.id == locations_id).first()
|
|
if not locations:
|
|
raise ValueError("locations no encontrado")
|
|
return locations
|
|
|
|
@staticmethod
|
|
def update_locations(db: Session, locations_id: int, data: LocationUpdate, current_user):
|
|
locations = db.query(Locations).filter(Locations.id == locations_id).first()
|
|
if not locations:
|
|
raise ValueError("locations no encontrado")
|
|
|
|
if current_user.role not in ["ROOT", "ADMIN"]:
|
|
raise ValueError("No tienes permisos para actualizar este locations")
|
|
|
|
update_data = data.dict(exclude_unset=True)
|
|
for key, value in update_data.items():
|
|
setattr(locations, key, value)
|
|
|
|
db.commit()
|
|
db.refresh(locations)
|
|
return locations
|
|
|
|
@staticmethod
|
|
def delete_locations(db: Session, locations_id: int, current_user):
|
|
locations = db.query(Locations).filter(Locations.id == locations_id).first()
|
|
if not locations:
|
|
raise ValueError("locations no encontrado")
|
|
|
|
if current_user.role not in ["ROOT", "ADMIN"]:
|
|
raise ValueError("No tienes permisos para eliminar este locations")
|
|
|
|
try:
|
|
locations.is_active = False
|
|
locations.deleted_at = datetime.utcnow()
|
|
locations.deleted_by = current_user.id
|
|
db.commit()
|
|
db.refresh(locations)
|
|
except Exception as e:
|
|
db.rollback()
|
|
raise ValueError(f"Error al eliminar el locations: {e}")
|
|
|
|
return {"message": "locations eliminado correctamente"}
|
|
|
|
|