DAS_2024_1/kashin_maxim_lab_3/university_service/service.py

42 lines
1.6 KiB
Python
Raw Permalink Normal View History

from db import db, University, FacultyUniversity
class UniversityService:
def get_all_universities(self):
return [university.to_dict() for university in University.query.all()]
def get_university(self, university_id):
university = University.query.get(university_id)
return university.to_dict() if university else None
def create_university(self, name):
university = University(name=name)
db.session.add(university)
db.session.commit()
return university.to_dict()
def update_university(self, university_id, name):
university = self.get_university(university_id)
if university:
university_obj = University.query.get(university_id)
university_obj.name = name
db.session.commit()
return university_obj.to_dict()
return None
def delete_university(self, university_id):
university = self.get_university(university_id)
if university:
db.session.delete(University.query.get(university_id))
db.session.commit()
def add_faculty_to_university(self, university_id, faculty_id):
association = FacultyUniversity(university_id=university_id, faculty_id=faculty_id)
db.session.add(association)
db.session.commit()
def remove_faculty_from_university(self, university_id, faculty_id):
association = FacultyUniversity.query.filter_by(university_id=university_id, faculty_id=faculty_id).first()
if association:
db.session.delete(association)
db.session.commit()