Skip to content

Commit

Permalink
soft deletes initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Zimovchik committed Oct 23, 2024
1 parent 5979594 commit 5cb2d73
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
3 changes: 3 additions & 0 deletions rating_api/models/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Lecturer(BaseDbModel):
avatar_link: Mapped[str] = mapped_column(String, nullable=True)
timetable_id: Mapped[int] = mapped_column(Integer, unique=True, nullable=False)
comments: Mapped[list[Comment]] = relationship("Comment", back_populates="lecturer")
is_deleted: Mapped[bool] = mapped_column(Boolean, default=False)


class Comment(BaseDbModel):
Expand All @@ -48,6 +49,7 @@ class Comment(BaseDbModel):
lecturer_id: Mapped[int] = mapped_column(Integer, ForeignKey("lecturer.id"))
lecturer: Mapped[Lecturer] = relationship("Lecturer", back_populates="comments")
review_status: Mapped[ReviewStatus] = mapped_column(DbEnum(ReviewStatus, native_enum=False), nullable=False)
is_deleted: Mapped[bool] = mapped_column(Boolean, default=False)


class LecturerUserComment(BaseDbModel):
Expand All @@ -56,3 +58,4 @@ class LecturerUserComment(BaseDbModel):
user_id: Mapped[int] = mapped_column(Integer, nullable=False)
create_ts: Mapped[datetime.datetime] = mapped_column(DateTime, default=datetime.datetime.utcnow, nullable=False)
update_ts: Mapped[datetime.datetime] = mapped_column(DateTime, default=datetime.datetime.utcnow, nullable=False)
is_deleted: Mapped[bool] = mapped_column(Boolean, default=False)
13 changes: 4 additions & 9 deletions rating_api/routes/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@
from fastapi import APIRouter, Depends, Query
from fastapi_sqlalchemy import db

from rating_api.models import Comment, Lecturer, LecturerUserComment, ReviewStatus
from rating_api.exceptions import ForbiddenAction, ObjectNotFound, TooManyCommentRequests
from rating_api.models import Comment, Lecturer, LecturerUserComment, ReviewStatus
from rating_api.schemas.base import StatusResponseModel
from rating_api.schemas.models import (
CommentGet,
CommentGetAll,
CommentPost,
)
from rating_api.schemas.models import CommentGet, CommentGetAll, CommentPost
from rating_api.settings import Settings, get_settings


Expand Down Expand Up @@ -86,7 +82,7 @@ async def get_comments(
`unreviewed` - вернет все непроверенные комментарии, если True. По дефолту False.
"""
comments = Comment.query(session=db.session).all()
comments = Comment.query(session=db.session).filter(Comment.is_deleted == False).all()
if not comments:
raise ObjectNotFound(Comment, 'all')
result = CommentGetAll(limit=limit, offset=offset, total=len(comments))
Expand Down Expand Up @@ -142,8 +138,7 @@ async def delete_comment(
check_comment = Comment.get(session=db.session, id=uuid)
if check_comment is None:
raise ObjectNotFound(Comment, uuid)
Comment.delete(session=db.session, id=uuid)

Comment.update(session=db.session, id=uuid, is_deleted=True)
return StatusResponseModel(
status="Success", message="Comment has been deleted", ru="Комментарий удален из RatingAPI"
)
6 changes: 3 additions & 3 deletions rating_api/routes/lecturer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from fastapi_sqlalchemy import db
from sqlalchemy import and_

from rating_api.models import Comment, Lecturer, LecturerUserComment, ReviewStatus
from rating_api.exceptions import AlreadyExists, ObjectNotFound
from rating_api.models import Comment, Lecturer, LecturerUserComment, ReviewStatus
from rating_api.schemas.base import StatusResponseModel
from rating_api.schemas.models import CommentGet, LecturerGet, LecturerGetAll, LecturerPatch, LecturerPost

Expand Down Expand Up @@ -101,7 +101,7 @@ async def get_lecturers(
Если передано `subject` - возвращает всех преподавателей, для которых переданное значение совпадает с их предметом преподавания.
Также возвращает всех преподавателей, у которых есть комментарий с совпадающим с данным subject.
"""
lecturers = Lecturer.query(session=db.session).all()
lecturers = Lecturer.query(session=db.session).filter(Lecturer.is_deleted == False).all()
if not lecturers:
raise ObjectNotFound(Lecturer, 'all')
result = LecturerGetAll(limit=limit, offset=offset, total=len(lecturers))
Expand Down Expand Up @@ -188,7 +188,7 @@ async def delete_lecturer(
for lecturer_user_comment in lecturer_user_comments:
LecturerUserComment.delete(lecturer_user_comment.id, session=db.session)

Lecturer.delete(session=db.session, id=id)
Lecturer.update(session=db.session, id=id, is_deleted=True)
return StatusResponseModel(
status="Success", message="Lecturer has been deleted", ru="Преподаватель удален из RatingAPI"
)

0 comments on commit 5cb2d73

Please sign in to comment.