Skip to content

Commit

Permalink
Merge pull request #68 from PCS-Poli-USP/develop
Browse files Browse the repository at this point in the history
Develop to main
  • Loading branch information
VitorMakiyama authored Nov 11, 2024
2 parents 01e627b + aabd285 commit 3e8602e
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 30 deletions.
18 changes: 4 additions & 14 deletions .github/workflows/ci_cd.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: CI/CD

on:
push:
branches:
- develop
on:
push:
branches:
- main

jobs:
ci:
Expand Down Expand Up @@ -88,16 +88,6 @@ jobs:
- name: Notify Command Success
run: echo "Command executed on ${{ secrets.HOST }} successfully!"


# - name: Connect to USP Server Machine
# run: |
# ssh [email protected] -p 2222
# ${{ secrets.SSH_PASSCODE }}
# cd /diskb/home/backend
# source venv/bin/activate
# git pull
# alembic upgrade heads




46 changes: 46 additions & 0 deletions migrations/versions/d4cdecd00183_added_like_feature.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Added_like_feature
Revision ID: d4cdecd00183
Revises: 5fc1e85f84a3
Create Date: 2024-10-11 17:35:27.491045
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
import sqlmodel
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = 'd4cdecd00183'
down_revision: Union[str, None] = '9ffe46f33c09'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('forumpostreactslink',
sa.Column('forum_post_id', sa.Integer(), nullable=False),
sa.Column('mobile_user_id', sa.Integer(), nullable=False),
sa.Column('post_like', sa.Boolean(), nullable=False),
sa.ForeignKeyConstraint(['forum_post_id'], ['forumpost.id'], ),
sa.ForeignKeyConstraint(['mobile_user_id'], ['mobileuser.id'], ),
sa.PrimaryKeyConstraint('forum_post_id', 'mobile_user_id')
)
op.add_column('forumpost', sa.Column('likes_count', sa.Integer(), nullable=True))
op.alter_column('forumpost', 'enabled',
existing_type=sa.BOOLEAN(),
nullable=False)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('forumpost', 'enabled',
existing_type=sa.BOOLEAN(),
nullable=True)
op.drop_column('forumpost', 'likes_count')
op.drop_table('forumpostreactslink')
# ### end Alembic commands ###
1 change: 1 addition & 0 deletions server/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class_db_model,
classroom_db_model,
forum_db_model,
forum_post_reacts_link,
forum_post_report_link,
holiday_category_db_model,
holiday_db_model,
Expand Down
6 changes: 6 additions & 0 deletions server/models/database/forum_db_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from server.models.database.forum_post_report_link import ForumPostReportLink
from server.models.database.mobile_user_db_model import MobileUser
from server.models.database.forum_post_reacts_link import ForumPostReactsLink



class ForumPost(SQLModel, table=True):
Expand Down Expand Up @@ -34,3 +36,7 @@ class ForumPost(SQLModel, table=True):
replies_count: int = Field(default=0)

enabled: bool = Field(default=True)

liked_by_users: list[MobileUser] = Relationship(link_model=ForumPostReactsLink)

likes_count: int = Field(default=0)
8 changes: 8 additions & 0 deletions server/models/database/forum_post_reacts_link.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from sqlmodel import Field, SQLModel



class ForumPostReactsLink(SQLModel, table=True):
forum_post_id: int = Field(default=0, foreign_key="forumpost.id", primary_key=True)
mobile_user_id: int | None = Field(default=None, foreign_key="mobileuser.id", primary_key=True)
post_like: bool = Field(default=False)
8 changes: 8 additions & 0 deletions server/models/http/requests/forum_request_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ def to_forumreply_model(post_id: int, replyDTO: ForumPostRegister) -> ForumPost:
class ForumReportRegister(BaseModel):
post_id: int
user_id: int

class ForumPostLike(BaseModel):
post_id: int
user_id: int
like_state: bool

class ForumUserLikesRegister(BaseModel):
user_id: int
27 changes: 19 additions & 8 deletions server/models/http/responses/forum_post_response.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from datetime import datetime
from pydantic import BaseModel
from server.models.database.forum_db_model import ForumPost
from server.models.database.forum_post_reacts_link import ForumPostReactsLink
from server.repositories.forum_repository import ForumRepository
from sqlmodel import Session, col, select

class ForumPostResponse(BaseModel):
id: int
Expand All @@ -11,9 +14,12 @@ class ForumPostResponse(BaseModel):
subject_id: int
created_at : datetime
replies_count: int
likes_count: int | None
user_liked: bool| None

@classmethod
def from_forum_post(cls, post: ForumPost) -> "ForumPostResponse":
def from_forum_post(cls, mobile_user_id: int, post: ForumPost, session: Session) -> "ForumPostResponse":

return cls(
id = post.id,
user_id = post.user_id,
Expand All @@ -22,18 +28,21 @@ def from_forum_post(cls, post: ForumPost) -> "ForumPostResponse":
content = post.content,
user_name = post.user.given_name+" "+post.user.family_name,
created_at = post.created_at,
replies_count = post.replies_count
replies_count = post.replies_count,
likes_count = post.likes_count,
user_liked = ForumRepository.get_post_like_reaction(mobile_user_id=mobile_user_id, post_id= post.id, session=session )
)

@classmethod
def from_forum_post_list(cls, posts: list[ForumPost]) -> list["ForumPostResponse"]:
return [cls.from_forum_post(post) for post in posts]
def from_forum_post_list(cls, mobile_user_id:int, posts: list[ForumPost], session:Session) -> list["ForumPostResponse"]:
return [cls.from_forum_post(mobile_user_id, post, session) for post in posts]

class ForumPostReplyResponse(ForumPostResponse):
reply_of_post_id: int

@classmethod
def from_forum_reply(cls, reply: ForumPost) -> "ForumPostReplyResponse":
def from_forum_reply(cls, reply: ForumPost, mobile_user_id: int, session: Session) -> "ForumPostReplyResponse":

return cls(
id = reply.id,
reply_of_post_id = reply.reply_of_post_id,
Expand All @@ -43,9 +52,11 @@ def from_forum_reply(cls, reply: ForumPost) -> "ForumPostReplyResponse":
content = reply.content,
user_name = reply.user.given_name+" "+reply.user.family_name,
created_at = reply.created_at,
replies_count = reply.replies_count
replies_count = reply.replies_count,
likes_count = reply.likes_count,
user_liked = ForumRepository.get_post_like_reaction(mobile_user_id=mobile_user_id, post_id= reply.id, session=session ),
)

@classmethod
def from_forum_post_reply_list(cls, replies: list[ForumPost]) -> list["ForumPostReplyResponse"]:
return [cls.from_forum_reply(reply) for reply in replies]
def from_forum_post_reply_list(cls, replies: list[ForumPost], mobile_user_id: int, session: Session) -> list["ForumPostReplyResponse"]:
return [cls.from_forum_reply(reply, mobile_user_id, session) for reply in replies]
60 changes: 59 additions & 1 deletion server/repositories/forum_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from sqlmodel import Session, col, select
from server.models.database.forum_db_model import ForumPost
from server.models.database.forum_post_report_link import ForumPostReportLink
from server.models.database.forum_post_reacts_link import ForumPostReactsLink



class ForumRepository:
Expand All @@ -19,12 +21,28 @@ def create(*, input: ForumPost, session: Session) -> ForumPost:
return new_post

@staticmethod
def get_all_posts(*, subject_id: int, session: Session) -> list[ForumPost]:
def get_post_like_reaction(mobile_user_id: int, post_id: int, session:Session):
user_statement = select(ForumPostReactsLink).where(
col(ForumPostReactsLink.mobile_user_id)==mobile_user_id,
col(ForumPostReactsLink.forum_post_id)==post_id
)

user_liked_this_post = False
user_like_post = session.exec(user_statement).first()

if user_like_post != None:
user_liked_this_post = user_like_post.post_like

return user_liked_this_post

@staticmethod
def get_all_posts(*, subject_id: int, mobile_user_id: int, session: Session) -> list[ForumPost]:
statement = select(ForumPost).where(
col(ForumPost.subject_id)==subject_id,
col(ForumPost.reply_of_post_id)==None,
col(ForumPost.enabled) == True)
posts = session.exec(statement).all()

return list(posts)

@staticmethod
Expand Down Expand Up @@ -102,7 +120,47 @@ def get_all_replies(*, post_id: int ,session: Session):
col(ForumPost.reply_of_post_id)==post_id,
col(ForumPost.enabled) == True)
replies = session.exec(statement).all()

return list(replies)

@staticmethod
def change_forum_post_like(
*, post_id: int, mobile_user_id: int, like_state: bool, session: Session
) -> ForumPost:

statement = select(ForumPost).where(col(ForumPost.id) == post_id)

user_statement = select(ForumPostReactsLink).where(
col(ForumPostReactsLink.mobile_user_id)==mobile_user_id,
col(ForumPostReactsLink.forum_post_id)==post_id
)

post = session.exec(statement).one()
liked_post = session.exec(user_statement).first()

# Create or Update
if liked_post == None:
new_react = ForumPostReactsLink(forum_post_id=post_id, mobile_user_id=mobile_user_id, post_like=like_state)
session.add(new_react)

else:
liked_post.post_like = like_state

session.add(liked_post)

# Update Like Count
likes_statement = select(ForumPostReactsLink).where(
col(ForumPostReactsLink.forum_post_id)==post_id,
col(ForumPostReactsLink.post_like)==True
)
post.likes_count = len(list(session.exec(likes_statement).all()))

session.add(post)
session.commit()
session.refresh(post)

return post


class PostNotFoundException(HTTPException):
def __init__(self, post_id: int) -> None:
Expand Down
31 changes: 24 additions & 7 deletions server/routes/public/forum_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
to_forumpost_model,
ForumReportRegister,
to_forumreply_model,
ForumPostLike,
)
from server.models.http.responses.forum_post_response import (
ForumPostReplyResponse,
Expand All @@ -24,10 +25,11 @@


@router.get("/posts")
async def get_posts(subject_id: int, session: SessionDep) -> list[ForumPostResponse]:
async def get_posts(subject_id: int, user_id:int, session: SessionDep) -> list[ForumPostResponse]:
"""Get all posts"""
posts = ForumRepository.get_all_posts(subject_id=subject_id, session=session)
return ForumPostResponse.from_forum_post_list(posts)
posts = ForumRepository.get_all_posts(subject_id=subject_id, mobile_user_id=user_id, session=session)

return ForumPostResponse.from_forum_post_list(user_id, posts, session)


@router.post("/posts")
Expand All @@ -42,7 +44,8 @@ async def create_forum_post(
input=to_forumpost_model(input),
session=session
)
return ForumPostResponse.from_forum_post(forum_post)

return ForumPostResponse.from_forum_post(input.user_id, forum_post, session)

@router.delete("/posts/{post_id}")
async def delete_forum_post(
Expand Down Expand Up @@ -106,12 +109,26 @@ async def create_forum_post_reply(
input=to_forumreply_model(post_id, input),
session=session,
)
return ForumPostReplyResponse.from_forum_reply(reply)
return ForumPostReplyResponse.from_forum_reply(reply, input.user_id, session)

@router.get("/posts/{post_id}")
async def get_forum_post_replies(
post_id: int, session: SessionDep
post_id: int, user_id:int, session: SessionDep
) -> list[ForumPostReplyResponse]:
"""Get all replies from a single post"""
replies = ForumRepository.get_all_replies(post_id=post_id, session=session)
return ForumPostReplyResponse.from_forum_post_reply_list(replies)

return ForumPostReplyResponse.from_forum_post_reply_list(replies, user_id, session)

@router.post("/posts/{post_id}/liked")
async def update_forum_post_like(
post_id: int,
input: ForumPostLike,
session: SessionDep
) -> ForumPost:
"""Change post like reaction state"""
like_changed_post = ForumRepository.change_forum_post_like(post_id=post_id, mobile_user_id=input.user_id, like_state=input.like_state ,session=session, )

return like_changed_post


0 comments on commit 3e8602e

Please sign in to comment.