1
0
mirror of https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git synced 2025-08-14 00:25:46 +02:00

добавление редисаъ4

This commit is contained in:
MoonTestUse1
2025-01-03 16:53:39 +06:00
parent 1749cff039
commit 9f5cb4d4c2
3 changed files with 132 additions and 77 deletions

View File

@@ -1,24 +1,31 @@
"""Employee CRUD operations"""
from sqlalchemy.orm import Session
from ..models.employee import Employee
from ..schemas.employee import EmployeeCreate, EmployeeUpdate
from ..utils.loggers import auth_logger
def get_employees(db: Session):
def get_employees(db: Session, skip: int = 0, limit: int = 100):
"""Get all employees"""
return db.query(Employee).all()
return db.query(Employee).offset(skip).limit(limit).all()
def get_employee(db: Session, employee_id: int):
"""Get employee by ID"""
return db.query(Employee).filter(Employee.id == employee_id).first()
def get_employee_by_lastname(db: Session, last_name: str):
def get_employee_by_last_name(db: Session, last_name: str):
"""Get employee by last name"""
return db.query(Employee).filter(Employee.last_name == last_name).first()
def create_employee(db: Session, employee_data: dict):
def create_employee(db: Session, employee: EmployeeCreate, hashed_password: str):
"""Create new employee"""
try:
db_employee = Employee(**employee_data)
db_employee = Employee(
first_name=employee.first_name,
last_name=employee.last_name,
department=employee.department,
office=employee.office,
hashed_password=hashed_password
)
db.add(db_employee)
db.commit()
db.refresh(db_employee)
@@ -26,4 +33,24 @@ def create_employee(db: Session, employee_data: dict):
except Exception as e:
db.rollback()
auth_logger.error(f"Error creating employee: {e}")
raise
raise
def update_employee(db: Session, employee_id: int, employee: EmployeeUpdate):
db_employee = get_employee(db, employee_id)
if not db_employee:
return None
update_data = employee.model_dump(exclude_unset=True)
for field, value in update_data.items():
setattr(db_employee, field, value)
db.commit()
db.refresh(db_employee)
return db_employee
def delete_employee(db: Session, employee_id: int):
db_employee = get_employee(db, employee_id)
if db_employee:
db.delete(db_employee)
db.commit()
return db_employee