DAS_2024_1/kashin_maxim_lab_3/faculty_service/service.py

39 lines
1.4 KiB
Python
Raw Normal View History

from db import db, Faculty, FacultyUniversity
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
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.query.get(faculty_id))
db.session.commit()
def add_university_to_faculty(self, faculty_id, university_id):
association = FacultyUniversity(faculty_id=faculty_id, university_id=university_id)
db.session.add(association)
db.session.commit()
def remove_university_from_faculty(self, faculty_id, university_id):
association = FacultyUniversity.query.filter_by(faculty_id=faculty_id, university_id=university_id).first()
if association:
db.session.delete(association)
db.session.commit()