2024-10-05 23:40:53 +04:00
|
|
|
from db import db, Faculty, FacultyUniversity
|
|
|
|
|
2024-10-05 22:02:08 +04:00
|
|
|
|
|
|
|
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()
|
2024-10-05 23:40:53 +04:00
|
|
|
return faculty
|
2024-10-05 22:02:08 +04:00
|
|
|
|
|
|
|
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:
|
2024-10-05 23:40:53 +04:00
|
|
|
db.session.delete(Faculty.query.get(faculty_id))
|
2024-10-05 22:02:08 +04:00
|
|
|
db.session.commit()
|
2024-10-05 23:40:53 +04:00
|
|
|
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()
|