Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

soft deletes #22

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Zimovchik marked this conversation as resolved.
Show resolved Hide resolved
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()
Zimovchik marked this conversation as resolved.
Show resolved Hide resolved
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тоже не требуется

return StatusResponseModel(
status="Success", message="Lecturer has been deleted", ru="Преподаватель удален из RatingAPI"
)
Loading