adision de bases para trabajo completo de back-v1.0.0
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
from fastapi import HTTPException
|
||||
from app.modules.coments.models import Coments
|
||||
from app.modules.coments.schema import CommentCreate, CommentResponse, Commentupdate, MessageResponse
|
||||
|
||||
|
||||
class ComentsService:
|
||||
@staticmethod
|
||||
def create_coments(db: Session, data: CommentCreate):
|
||||
new_coments = Coments(**data.dict())
|
||||
db.add(new_coments)
|
||||
db.commit()
|
||||
db.refresh(new_coments)
|
||||
return new_coments
|
||||
|
||||
@staticmethod
|
||||
def get_coments(db: Session, coments_id: int, current_user):
|
||||
coments = db.query(Coments).filter(Coments.id == coments_id).first()
|
||||
if not coments:
|
||||
raise ValueError("coments no encontrado")
|
||||
return coments
|
||||
|
||||
@staticmethod
|
||||
def update_coments(db: Session, coments_id: int, data:Commentupdate , current_user):
|
||||
coments = db.query(Coments).filter(Coments.id == coments_id).first()
|
||||
if not coments:
|
||||
raise ValueError("coments no encontrado")
|
||||
|
||||
if current_user.role not in ["ROOT", "ADMIN"]:
|
||||
raise ValueError("No tienes permisos para actualizar este coments")
|
||||
|
||||
update_data = data.dict(exclude_unset=True)
|
||||
for key, value in update_data.items():
|
||||
setattr(coments, key, value)
|
||||
|
||||
db.commit()
|
||||
db.refresh(coments)
|
||||
return coments
|
||||
|
||||
@staticmethod
|
||||
def delete_coments(db: Session, coments_id: int, current_user):
|
||||
coments = db.query(Coments).filter(Coments.id == coments_id).first()
|
||||
if not coments:
|
||||
raise ValueError("coments no encontrado")
|
||||
|
||||
if current_user.role not in ["ROOT", "ADMIN"]:
|
||||
raise ValueError("No tienes permisos para eliminar este coments")
|
||||
|
||||
try:
|
||||
coments.is_active = False
|
||||
coments.deleted_at = datetime.utcnow()
|
||||
coments.deleted_by = current_user.id
|
||||
db.commit()
|
||||
db.refresh(coments)
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise ValueError(f"Error al eliminar el coments: {e}")
|
||||
|
||||
return {"message": "coments eliminado correctamente"}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user