Skip to content
This repository was archived by the owner on Jul 31, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
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
165 changes: 161 additions & 4 deletions backend/app/daos/post.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
from typing import List, Optional, Tuple
from typing import List, Optional, Tuple, Union

from fastapi import HTTPException
from sqlalchemy import or_
from sqlalchemy.orm import joinedload

from app.models.classroom import Post
from app.models.classroom import Activity, Post, PostAttachment
from app.models.common import Response
from app.schemas.classroom import ActivityPostIn, PostIn, ResponsePostIn

from .base import BaseDao


class PostDao(BaseDao):

def get_by_id(self, postId: int) -> Post:
_post = self.session.query(Post).get(postId)

if not _post:
raise HTTPException(status_code=404, detail="Post não encontrado")

return _post

def get_posts(
self,
classroom_ids: List[int],
Expand All @@ -20,11 +32,11 @@ def get_posts(
query = (
self.session.query(Post)
.options(joinedload(Post.classroom))
.filter(Post.classromId.in_(classroom_ids))
.filter(Post.classroomId.in_(classroom_ids))
)

if subject_id:
query = query.filter(Post.classromId == subject_id)
query = query.filter(Post.classroomId == subject_id)

if search_query:
query = query.filter(
Expand All @@ -44,3 +56,148 @@ def get_posts(
)

return posts, total

def _create_post(
self,
post: Union[PostIn, ActivityPostIn, ResponsePostIn],
classroomId: int,
userId: int,
) -> Post:
_post = Post(
classroomId=classroomId,
addedById=userId,
title=post.title,
content=post.content,
type=post.type,
)

if post.attachments:
_post.attachments = (
self.session.query(PostAttachment)
.filter(PostAttachment.id.in_(post.attachments))
.all()
)

self.session.add(_post)
self.session.commit()
self.session.refresh(_post)

return _post

def create_notice(self, post: PostIn, classroomId: int, userId: int) -> Post:
return self._create_post(post, classroomId, userId)

def create_lecture(self, post: PostIn, classroomId: int, userId: int) -> Post:
return self._create_post(post, classroomId, userId)

def create_activity(
self, post: ActivityPostIn, classroomId: int, userId: int
) -> Post:
_post = self._create_post(post, classroomId, userId)

# TODO: Create or Add (?) Activity Group

_activity = Activity(
postId=_post.id,
startDate=post.startDate,
endDate=post.endDate,
maxGrade=post.maxGrade,
linearCoefficient=post.linearCoefficient,
angularCoefficient=post.angularCoefficient,
weight=post.weight,
)

self.session.add(_activity)
self.session.commit()

self.session.refresh(_post)

return _post

def create_test(self, post: ActivityPostIn, classroomId: int, userId: int) -> Post:
return self.create_activity(post, classroomId, userId)

def create_response(
self, post: ResponsePostIn, classroomId: int, userId: int
) -> Post:
_post = self._create_post(post, classroomId, userId)

_response = Response(
postId=_post.id, activityId=post.activityId, grade=post.grade
)

self.session.add(_response)
self.session.commit()

self.session.refresh(_post)

return _post

def _update_post(
self, postId: int, post: Union[PostIn, ActivityPostIn, ResponsePostIn]
) -> Post:
_post = self.get_by_id(postId)

_post.title = post.title
_post.content = post.content

if post.attachments:
_post.attachments = (
self.session.query(PostAttachment)
.filter(PostAttachment.id.in_(post.attachments))
.all()
)

self.session.commit()
self.session.refresh(_post)

return _post

def update_notice(self, postId: int, post: PostIn) -> Post:
return self._update_post(postId, post)

def update_lecture(self, postId: int, post: PostIn) -> Post:
return self._update_post(postId, post)

def update_activity(self, postId: int, post: ActivityPostIn) -> Post:

_post = self._update_post(postId, post)

ACTIVITY_KEYS = [
"startDate",
"endDate",
"maxGrade",
"linearCoefficient",
"angularCoefficient",
"weight",
]

if _post.activity:
for key, value in post.model_dump().items():
if key in ACTIVITY_KEYS:
setattr(_post.activity, key, value)

self.session.commit()
self.session.refresh(_post)

return _post

def update_test(self, postId: int, post: ActivityPostIn) -> Post:
return self.update_activity(postId, post)

def update_response(self, postId: int, post: ResponsePostIn) -> Post:
_post = self._update_post(postId, post)

if _post.response:
_post.response.activityId = post.activityId
_post.response.grade = post.grade

self.session.commit()
self.session.refresh(_post)

return _post

def delete_by_id(self, postId: int):
_post = self.get_by_id(postId)
self.session.delete(_post)
self.session.commit()
52 changes: 23 additions & 29 deletions backend/app/models/classroom.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from datetime import datetime
from typing import List, Optional, get_args

from sqlalchemy import JSON
import sqlalchemy as sa
from sqlalchemy import JSON
from sqlalchemy.orm import Mapped, mapped_column, relationship

from .base import BaseTable
Expand All @@ -17,14 +17,16 @@ class Classroom(BaseTable):

name: Mapped[str]
thumbnail: Mapped["Attach"] = relationship()
posts: Mapped[List["Post"]] = relationship(back_populates="classroom")
posts: Mapped[List["Post"]] = relationship(
back_populates="classroom", cascade="all, delete"
)
activityGroups: Mapped[List["ActivityGroup"]] = relationship(
back_populates="classroom"
)
members: Mapped[List["Enrollment"]] = relationship(
back_populates="classroom"
back_populates="classroom", cascade="all, delete"
)
video_conference: Mapped[List[str]] = mapped_column(JSON)
video_conference: Mapped[List[str]] = mapped_column(JSON, default="")

@property
def teachers(self):
Expand All @@ -34,9 +36,7 @@ def teachers(self):
class Activity(BaseTable):
__tablename__ = "activities"

postId: Mapped[Optional[int]] = mapped_column(
sa.Integer, sa.ForeignKey("posts.id")
)
postId: Mapped[Optional[int]] = mapped_column(sa.Integer, sa.ForeignKey("posts.id"))
post: Mapped["Post"] = relationship("Post", back_populates="activity")

startDate: Mapped[Optional[datetime]]
Expand All @@ -56,64 +56,58 @@ class Activity(BaseTable):
class Serie(BaseTable):
__tablename__ = "series"

student: Mapped[List["Student"]] = relationship(
"Student", back_populates="serie"
)
student: Mapped[List["Student"]] = relationship("Student", back_populates="serie")
name: Mapped[str]


class Post(BaseTable):
__tablename__ = "posts"

classromId: Mapped[Optional[int]] = mapped_column(
classroomId: Mapped[Optional[int]] = mapped_column(
sa.Integer, sa.ForeignKey("classrooms.id")
)
classroom: Mapped["Classroom"] = relationship(
"Classroom", back_populates="posts"
)
classroom: Mapped["Classroom"] = relationship("Classroom", back_populates="posts")
title: Mapped[str]
content: Mapped[str]
frequency: Mapped[List["Frequency"]] = relationship(
"Frequency", back_populates="lecture"
"Frequency", back_populates="lecture", cascade="all, delete"
)
type: Mapped[PostType] = mapped_column(sa.Enum(*get_args(PostType)))
attachments: Mapped[List["PostAttachment"]] = relationship(
"PostAttachment", back_populates="post"
"PostAttachment", back_populates="post", cascade="all, delete"
)
activity: Mapped[Optional["Activity"]] = relationship(
"Activity", back_populates="post"
"Activity", back_populates="post", cascade="all, delete"
)
response: Mapped[Optional["Response"]] = relationship(
"Response", back_populates="post", foreign_keys="Response.postId"
"Response",
back_populates="post",
foreign_keys="Response.postId",
cascade="all, delete",
)
responses: Mapped[List["Response"]] = relationship(
"Response",
back_populates="activity",
foreign_keys="Response.activityId",
cascade="all, delete",
)

comments: Mapped[List["Comment"]] = relationship(
"Comment", back_populates="post"
"Comment", back_populates="post", cascade="all, delete"
)
messages: Mapped[List["Message"]] = relationship(
"Message", back_populates="post"
"Message", back_populates="post", cascade="all, delete"
)


class Frequency(BaseTable):
__tablename__ = "frequencies"

studentId: Mapped[int] = mapped_column(
sa.Integer, sa.ForeignKey("users.id")
)
studentId: Mapped[int] = mapped_column(sa.Integer, sa.ForeignKey("users.id"))
student: Mapped["User"] = relationship("User", foreign_keys=[studentId])
lecture: Mapped["Post"] = relationship("Post", back_populates="frequency")
postId: Mapped[Optional[int]] = mapped_column(
sa.Integer, sa.ForeignKey("posts.id")
)
status: Mapped[FrequencyStatus] = mapped_column(
sa.Enum(*get_args(FrequencyStatus))
)
postId: Mapped[Optional[int]] = mapped_column(sa.Integer, sa.ForeignKey("posts.id"))
status: Mapped[FrequencyStatus] = mapped_column(sa.Enum(*get_args(FrequencyStatus)))
justification: Mapped[Optional[str]]


Expand Down
2 changes: 1 addition & 1 deletion backend/app/models/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
AchievementType = Literal["olympic medal", "certificate"]
AchievementStatus = Literal["ready", "pending", "soft delete"]
MedalType = Literal["participation", "bronze", "silver", "gold"]
PostType = Literal["notice", "class material", "activity", "other"]
PostType = Literal["notice", "lecture", "activity", "test", "response"]
FrequencyStatus = Literal["present", "missed", "justified"]
CommentType = Literal["public", "private"]
ExpenseLogType = Literal["deposit", "removal"]
Expand Down
Loading