DAS_2024_1/kashin_maxim_lab_3/faculty_service/service.py

29 lines
867 B
Python

from db import db, Faculty
class FacultyService:
def get_all_faculties(self):
return [faculty.to_dict() for faculty in Faculty.query.all()]
def get_faculty(self, faculty_id):
return Faculty.query.get(faculty_id)
def create_faculty(self, name, university_id):
faculty = Faculty(name=name, university_id=university_id)
db.session.add(faculty)
db.session.commit()
return faculty.to_dict()
def update_faculty(self, faculty_id, name):
faculty = self.get_faculty(faculty_id)
if faculty:
faculty.name = name
db.session.commit()
return faculty.to_dict()
return None
def delete_faculty(self, faculty_id):
faculty = self.get_faculty(faculty_id)
if faculty:
db.session.delete(faculty)
db.session.commit()