78 lines
2.1 KiB
Python
78 lines
2.1 KiB
Python
|
|
from services.database_service import DatabaseService
|
||
|
|
from models.entities.celula import Celula
|
||
|
|
|
||
|
|
class CelulaService:
|
||
|
|
"""Service for Celula operations"""
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def get_all_celulas():
|
||
|
|
"""Get all celulas from the database"""
|
||
|
|
db = DatabaseService.get_db_connection()
|
||
|
|
try:
|
||
|
|
celulas = db.query(Celula).all()
|
||
|
|
return celulas
|
||
|
|
finally:
|
||
|
|
db.close()
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def get_celula_by_id(celula_id):
|
||
|
|
"""Get a celula by its ID"""
|
||
|
|
db = DatabaseService.get_db_connection()
|
||
|
|
try:
|
||
|
|
celula = db.query(Celula).get(celula_id)
|
||
|
|
return celula
|
||
|
|
finally:
|
||
|
|
db.close()
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def create_celula(data):
|
||
|
|
"""Create a new celula"""
|
||
|
|
db = DatabaseService.get_db_connection()
|
||
|
|
try:
|
||
|
|
celula = Celula(**data)
|
||
|
|
db.add(celula)
|
||
|
|
db.commit()
|
||
|
|
return celula
|
||
|
|
except Exception as e:
|
||
|
|
db.rollback()
|
||
|
|
raise e
|
||
|
|
finally:
|
||
|
|
db.close()
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def update_celula(celula_id, data):
|
||
|
|
"""Update an existing celula"""
|
||
|
|
db = DatabaseService.get_db_connection()
|
||
|
|
try:
|
||
|
|
celula = db.query(Celula).get(celula_id)
|
||
|
|
if not celula:
|
||
|
|
return None
|
||
|
|
|
||
|
|
for key, value in data.items():
|
||
|
|
setattr(celula, key, value)
|
||
|
|
|
||
|
|
db.commit()
|
||
|
|
return celula
|
||
|
|
except Exception as e:
|
||
|
|
db.rollback()
|
||
|
|
raise e
|
||
|
|
finally:
|
||
|
|
db.close()
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def delete_celula(celula_id):
|
||
|
|
"""Delete a celula"""
|
||
|
|
db = DatabaseService.get_db_connection()
|
||
|
|
try:
|
||
|
|
celula = db.query(Celula).get(celula_id)
|
||
|
|
if not celula:
|
||
|
|
return False
|
||
|
|
|
||
|
|
db.delete(celula)
|
||
|
|
db.commit()
|
||
|
|
return True
|
||
|
|
except Exception as e:
|
||
|
|
db.rollback()
|
||
|
|
raise e
|
||
|
|
finally:
|
||
|
|
db.close()
|