сделал основные репозитории для всех таблиц

This commit is contained in:
Андрей Дувакин 2025-02-09 13:31:14 +05:00
parent 3ae2b72563
commit a00fba8eca
23 changed files with 704 additions and 3 deletions

View File

@ -0,0 +1,35 @@
from sqlalchemy.orm import Session
from app.domain.models.answer_files import AnswerFile
class AnswerFilesRepository:
def __init__(self, db: Session):
self.db = db
def get_all(self):
return self.db.query(AnswerFile).all()
def get_by_id(self, answer_file_id: int):
return self.db.query(AnswerFile).filter(AnswerFile.id == answer_file_id).first()
def create(self, answer_file: AnswerFile):
self.db.add(answer_file)
self.db.commit()
self.db.refresh(answer_file)
return answer_file
def update(self, answer_file: AnswerFile):
self.db.merge(answer_file)
self.db.commit()
self.db.refresh(answer_file)
return answer_file
def delete(self, answer_file_id: int):
answer_file = self.db.query(AnswerFile).filter(AnswerFile.id == answer_file_id).first()
if answer_file:
self.db.delete(answer_file)
self.db.commit()
return answer_file
return None

View File

@ -0,0 +1,33 @@
from app.domain.models.answer_options import AnswerOption
class AnswerOptionsRepository:
def __init__(self, db):
self.db = db
def get_all(self):
return self.db.query(AnswerOption).all()
def get_by_id(self, answer_option_id: int):
return self.db.query(AnswerOption).filter(AnswerOption.id == answer_option_id).first()
def create(self, answer_option: AnswerOption):
self.db.add(answer_option)
self.db.commit()
self.db.refresh(answer_option)
return answer_option
def update(self, answer_option: AnswerOption):
self.db.merge(answer_option)
self.db.commit()
self.db.refresh(answer_option)
return answer_option
def delete(self, answer_option_id: int):
answer_option = self.db.query(AnswerOption).filter(AnswerOption.id == answer_option_id).first()
if answer_option:
self.db.delete(answer_option)
self.db.commit()
return answer_option
return None

View File

@ -0,0 +1,35 @@
from sqlalchemy.orm import Session
from app.domain.models.appeals import Appeal
class AppealsRepository:
def __init__(self, db: Session):
self.db = db
def get_all(self):
return self.db.query(Appeal).all()
def get_by_id(self, appeal_id: int):
return self.db.query(Appeal).filter(Appeal.id == appeal_id).first()
def create(self, appeal: Appeal):
self.db.add(appeal)
self.db.commit()
self.db.refresh(appeal)
return appeal
def update(self, appeal: Appeal):
self.db.merge(appeal)
self.db.commit()
self.db.refresh(appeal)
return appeal
def delete(self, appeal_id: int):
appeal = self.db.query(Appeal).filter(Appeal.id == appeal_id).first()
if appeal:
self.db.delete(appeal)
self.db.commit()
return appeal
return None

View File

@ -0,0 +1,35 @@
from sqlalchemy.orm import Session
from app.domain.models.appeals_topics import AppealsTopic
class AppealsTopicsRepository:
def __init__(self, db: Session):
self.db = db
def get_all(self):
return self.db.query(AppealsTopic).all()
def get_by_id(self, appeals_topic_id: int):
return self.db.query(AppealsTopic).filter(AppealsTopic.id == appeals_topic_id).first()
def create(self, appeals_topic: AppealsTopic):
self.db.add(appeals_topic)
self.db.commit()
self.db.refresh(appeals_topic)
return appeals_topic
def update(self, appeals_topic: AppealsTopic):
self.db.merge(appeals_topic)
self.db.commit()
self.db.refresh(appeals_topic)
return appeals_topic
def delete(self, appeals_topic_id: int):
appeals_topic = self.db.query(AppealsTopic).filter(AppealsTopic.id == appeals_topic_id).first()
if appeals_topic:
self.db.delete(appeals_topic)
self.db.commit()
return appeals_topic
return None

View File

@ -0,0 +1,35 @@
from sqlalchemy.orm import Session
from app.domain.models.categories import Category
class CategoriesRepository:
def __init__(self, db: Session):
self.db = db
def get_all(self):
return self.db.query(Category).all()
def get_by_id(self, category_id: int):
return self.db.query(Category).filter(Category.id == category_id).first()
def create(self, category: Category):
self.db.add(category)
self.db.commit()
self.db.refresh(category)
return category
def update(self, category: Category):
self.db.merge(category)
self.db.commit()
self.db.refresh(category)
return category
def delete(self, category_id: int):
category = self.db.query(Category).filter(Category.id == category_id).first()
if category:
self.db.delete(category)
self.db.commit()
return category
return None

View File

@ -0,0 +1,35 @@
from sqlalchemy.orm import Session
from app.domain.models.course_students import CourseStudent
class CourseStudentsRepository:
def __init__(self, db: Session):
self.db = db
def get_all(self):
return self.db.query(CourseStudent).all()
def get_by_id(self, course_student_id: int):
return self.db.query(CourseStudent).filter(CourseStudent.id == course_student_id).first()
def create(self, course_student: CourseStudent):
self.db.add(course_student)
self.db.commit()
self.db.refresh(course_student)
return course_student
def update(self, course_student: CourseStudent):
self.db.merge(course_student)
self.db.commit()
self.db.refresh(course_student)
return course_student
def delete(self, course_student_id: int):
course_student = self.db.query(CourseStudent).filter(CourseStudent.id == course_student_id).first()
if course_student:
self.db.delete(course_student)
self.db.commit()
return course_student
return None

View File

@ -0,0 +1,35 @@
from sqlalchemy.orm import Session
from app.domain.models.courses import Course
class CoursesRepository:
def __init__(self, db: Session):
self.db = db
def get_all(self):
return self.db.query(Course).all()
def get_by_id(self, course_id: int):
return self.db.query(Course).filter(Course.id == course_id).first()
def create(self, course: Course):
self.db.add(course)
self.db.commit()
self.db.refresh(course)
return course
def update(self, course: Course):
self.db.merge(course)
self.db.commit()
self.db.refresh(course)
return course
def delete(self, course_id: int):
course = self.db.query(Course).filter(Course.id == course_id).first()
if course:
self.db.delete(course)
self.db.commit()
return course
return None

View File

@ -0,0 +1,35 @@
from sqlalchemy.orm import Session
from app.domain.models.lecture import Lecture
class LecturesRepository:
def __init__(self, db: Session):
self.db = db
def get_all(self):
return self.db.query(Lecture).all()
def get_by_id(self, lecture_id: int):
return self.db.query(Lecture).filter(Lecture.id == lecture_id).first()
def create(self, lecture: Lecture):
self.db.add(lecture)
self.db.commit()
self.db.refresh(lecture)
return lecture
def update(self, lecture: Lecture):
self.db.merge(lecture)
self.db.commit()
self.db.refresh(lecture)
return lecture
def delete(self, lecture_id: int):
lecture = self.db.query(Lecture).filter(Lecture.id == lecture_id).first()
if lecture:
self.db.delete(lecture)
self.db.commit()
return lecture
return None

View File

@ -0,0 +1,35 @@
from sqlalchemy.orm import Session
from app.domain.models.lessons import Lesson
class LessonsRepository:
def __init__(self, db: Session):
self.db = db
def get_all(self):
return self.db.query(Lesson).all()
def get_by_id(self, lesson_id: int):
return self.db.query(Lesson).filter(Lesson.id == lesson_id).first()
def create(self, lesson: Lesson):
self.db.add(lesson)
self.db.commit()
self.db.refresh(lesson)
return lesson
def update(self, lesson: Lesson):
self.db.merge(lesson)
self.db.commit()
self.db.refresh(lesson)
return lesson
def delete(self, lesson_id: int):
lesson = self.db.query(Lesson).filter(Lesson.id == lesson_id).first()
if lesson:
self.db.delete(lesson)
self.db.commit()
return lesson
return None

View File

@ -0,0 +1,35 @@
from sqlalchemy.orm import Session
from app.domain.models.notification_types import NotificationType
class NotificationTypesRepository:
def __init__(self, db: Session):
self.db = db
def get_all(self):
return self.db.query(NotificationType).all()
def get_by_id(self, notification_type_id: int):
return self.db.query(NotificationType).filter(NotificationType.id == notification_type_id).first()
def create(self, notification_type: NotificationType):
self.db.add(notification_type)
self.db.commit()
self.db.refresh(notification_type)
return notification_type
def update(self, notification_type: NotificationType):
self.db.merge(notification_type)
self.db.commit()
self.db.refresh(notification_type)
return notification_type
def delete(self, notification_type_id: int):
notification_type = self.db.query(NotificationType).filter(NotificationType.id == notification_type_id).first()
if notification_type:
self.db.delete(notification_type)
self.db.commit()
return notification_type
return None

View File

@ -0,0 +1,35 @@
from sqlalchemy.orm import Session
from app.domain.models.notifications import Notification
class NotificationsRepository:
def __init__(self, db: Session):
self.db = db
def get_all(self):
return self.db.query(Notification).all()
def get_by_id(self, notification_id: int):
return self.db.query(Notification).filter(Notification.id == notification_id).first()
def create(self, notification: Notification):
self.db.add(notification)
self.db.commit()
self.db.refresh(notification)
return notification
def update(self, notification: Notification):
self.db.merge(notification)
self.db.commit()
self.db.refresh(notification)
return notification
def delete(self, notification_id: int):
notification = self.db.query(Notification).filter(Notification.id == notification_id).first()
if notification:
self.db.delete(notification)
self.db.commit()
return notification
return None

View File

@ -0,0 +1,35 @@
from sqlalchemy.orm import Session
from app.domain.models.roles import Role
class RolesRepository:
def __init__(self, db: Session):
self.db = db
def get_all(self):
return self.db.query(Role).all()
def get_by_id(self, role_id: int):
return self.db.query(Role).filter(Role.id == role_id).first()
def create(self, role: Role):
self.db.add(role)
self.db.commit()
self.db.refresh(role)
return role
def update(self, role: Role):
self.db.merge(role)
self.db.commit()
self.db.refresh(role)
return role
def delete(self, role_id: int):
role = self.db.query(Role).filter(Role.id == role_id).first()
if role:
self.db.delete(role)
self.db.commit()
return role
return None

View File

@ -0,0 +1,35 @@
from sqlalchemy.orm import Session
from app.domain.models.step_tasks import StepTask
class StepTasksRepository:
def __init__(self, db: Session):
self.db = db
def get_all(self):
return self.db.query(StepTask).all()
def get_by_id(self, step_task_id: int):
return self.db.query(StepTask).filter(StepTask.id == step_task_id).first()
def create(self, step_task: StepTask):
self.db.add(step_task)
self.db.commit()
self.db.refresh(step_task)
return step_task
def update(self, step_task: StepTask):
self.db.merge(step_task)
self.db.commit()
self.db.refresh(step_task)
return step_task
def delete(self, step_task_id: int):
step_task = self.db.query(StepTask).filter(StepTask.id == step_task_id).first()
if step_task:
self.db.delete(step_task)
self.db.commit()
return step_task
return None

View File

@ -0,0 +1,35 @@
from sqlalchemy.orm import Session
from app.domain.models.step_types import StepType
class StepTypesRepository:
def __init__(self, db: Session):
self.db = db
def get_all(self):
return self.db.query(StepType).all()
def get_by_id(self, step_type_id: int):
return self.db.query(StepType).filter(StepType.id == step_type_id).first()
def create(self, step_type: StepType):
self.db.add(step_type)
self.db.commit()
self.db.refresh(step_type)
return step_type
def update(self, step_type: StepType):
self.db.merge(step_type)
self.db.commit()
self.db.refresh(step_type)
return step_type
def delete(self, step_type_id: int):
step_type = self.db.query(StepType).filter(StepType.id == step_type_id).first()
if step_type:
self.db.delete(step_type)
self.db.commit()
return step_type
return None

View File

@ -0,0 +1,35 @@
from sqlalchemy.orm import Session
from app.domain.models.steps import Step
class StepsRepository:
def __init__(self, db: Session):
self.db = db
def get_all(self):
return self.db.query(Step).all()
def get_by_id(self, step_id: int):
return self.db.query(Step).filter(Step.id == step_id).first()
def create(self, step: Step):
self.db.add(step)
self.db.commit()
self.db.refresh(step)
return step
def update(self, step: Step):
self.db.merge(step)
self.db.commit()
self.db.refresh(step)
return step
def delete(self, step_id: int):
step = self.db.query(Step).filter(Step.id == step_id).first()
if step:
self.db.delete(step)
self.db.commit()
return step
return None

View File

@ -0,0 +1,35 @@
from sqlalchemy.orm import Session
from app.domain.models.task_answers import TaskAnswer
class TaskAnswersRepository:
def __init__(self, db: Session):
self.db = db
def get_all(self):
return self.db.query(TaskAnswer).all()
def get_by_id(self, task_answer_id: int):
return self.db.query(TaskAnswer).filter(TaskAnswer.id == task_answer_id).first()
def create(self, task_answer: TaskAnswer):
self.db.add(task_answer)
self.db.commit()
self.db.refresh(task_answer)
return task_answer
def update(self, task_answer: TaskAnswer):
self.db.merge(task_answer)
self.db.commit()
self.db.refresh(task_answer)
return task_answer
def delete(self, task_answer_id: int):
task_answer = self.db.query(TaskAnswer).filter(TaskAnswer.id == task_answer_id).first()
if task_answer:
self.db.delete(task_answer)
self.db.commit()
return task_answer
return None

View File

@ -0,0 +1,35 @@
from sqlalchemy.orm import Session
from app.domain.models.task_files import TaskFile
class TaskFilesRepository:
def __init__(self, db: Session):
self.db = db
def get_all(self):
return self.db.query(TaskFile).all()
def get_by_id(self, task_file_id: int):
return self.db.query(TaskFile).filter(TaskFile.id == task_file_id).first()
def create(self, task_file: TaskFile):
self.db.add(task_file)
self.db.commit()
self.db.refresh(task_file)
return task_file
def update(self, task_file: TaskFile):
self.db.merge(task_file)
self.db.commit()
self.db.refresh(task_file)
return task_file
def delete(self, task_file_id: int):
task_file = self.db.query(TaskFile).filter(TaskFile.id == task_file_id).first()
if task_file:
self.db.delete(task_file)
self.db.commit()
return task_file
return None

View File

@ -0,0 +1,35 @@
from sqlalchemy.orm import Session
from app.domain.models.task_types import TaskType
class TaskTypesRepository:
def __init__(self, db: Session):
self.db = db
def get_all(self):
return self.db.query(TaskType).all()
def get_by_id(self, task_type_id: int):
return self.db.query(TaskType).filter(TaskType.id == task_type_id).first()
def create(self, task_type: TaskType):
self.db.add(task_type)
self.db.commit()
self.db.refresh(task_type)
return task_type
def update(self, task_type: TaskType):
self.db.merge(task_type)
self.db.commit()
self.db.refresh(task_type)
return task_type
def delete(self, task_type_id: int):
task_type = self.db.query(TaskType).filter(TaskType.id == task_type_id).first()
if task_type:
self.db.delete(task_type)
self.db.commit()
return task_type
return None

View File

@ -0,0 +1,35 @@
from sqlalchemy.orm import Session
from app.domain.models.test_answers import TestAnswer
class TestAnswersRepository:
def __init__(self, db: Session):
self.db = db
def get_all(self):
return self.db.query(TestAnswer).all()
def get_by_id(self, test_answer_id: int):
return self.db.query(TestAnswer).filter(TestAnswer.id == test_answer_id).first()
def create(self, test_answer: TestAnswer):
self.db.add(test_answer)
self.db.commit()
self.db.refresh(test_answer)
return test_answer
def update(self, test_answer: TestAnswer):
self.db.merge(test_answer)
self.db.commit()
self.db.refresh(test_answer)
return test_answer
def delete(self, test_answer_id: int):
test_answer = self.db.query(TestAnswer).filter(TestAnswer.id == test_answer_id).first()
if test_answer:
self.db.delete(test_answer)
self.db.commit()
return test_answer
return None

View File

@ -0,0 +1,38 @@
from sqlalchemy.orm import Session
from app.domain.models.users import User
class UsersRepository:
def __init__(self, db: Session):
self.db = db
def get_all(self):
return self.db.query(User).all()
def get_by_id(self, user_id: int):
return self.db.query(User).filter(User.id == user_id).first()
def get_by_login(self, login: str):
return self.db.query(User).filter(User.login == login).first()
def create(self, user: User):
self.db.add(user)
self.db.commit()
self.db.refresh(user)
return user
def update(self, user: User):
self.db.merge(user)
self.db.commit()
self.db.refresh(user)
return user
def delete(self, user_id: int):
user = self.db.query(User).filter(User.id == user_id).first()
if user:
self.db.delete(user)
self.db.commit()
return user
return None

View File

@ -9,7 +9,7 @@ import app.domain.models.appeals_topics
import app.domain.models.categories
import app.domain.models.courses
import app.domain.models.course_students
import app.domain.models.lectures
import app.domain.models.lecture
import app.domain.models.lessons
import app.domain.models.notification_types
import app.domain.models.notifications

View File

@ -4,7 +4,7 @@ from sqlalchemy.orm import relationship
from app.domain.models import Base
class Lectures(Base):
class Lecture(Base):
__tablename__ = 'lectures'
id = Column(Integer, primary_key=True, autoincrement=True)

View File

@ -16,5 +16,5 @@ class Step(Base):
lesson = relationship('Lesson', back_populates='steps')
type = relationship('StepType', back_populates='steps')
lectures = relationship('Lectures', back_populates='step')
lectures = relationship('Lecture', back_populates='step')
tasks = relationship('StepTask', back_populates='step')