DAS_2024_1/kashin_maxim_lab_3/university_service/service.py

31 lines
1.1 KiB
Python
Raw Normal View History

from db import db, University
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()