Skip to content

Commit

Permalink
[Feat] report user (#42)
Browse files Browse the repository at this point in the history
## Description
- create ReportUser Pydantic model
- create Report table
- report post api
<!-- Add a more detailed description of the changes if needed. -->

## Related Issue
close #37 
<!-- If your PR refers to a related issue, link it here. -->
  • Loading branch information
nahyun0121 authored Dec 3, 2023
2 parents 415aeee + 709a472 commit 55bb971
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/api/routes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from app.api.routes import oauth
from app.api.routes import messages
from app.api.routes import likes
from app.api.routes import reports

router = APIRouter()
router.include_router(users.router, tags=["users"], prefix="/user")
Expand All @@ -16,3 +17,4 @@
router.include_router(oauth.router, tags=["auth"], prefix="/auth")
router.include_router(messages.router, tags=["messages"], prefix="/message")
router.include_router(likes.router, tags=["likes"], prefix="/like")
router.include_router(reports.router, tags=["reports"], prefix="/report")
29 changes: 29 additions & 0 deletions app/api/routes/reports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from typing import Any
from app.models.domain.reports import ReportUser, ReportResponse
from app.crud.crud_reports import create_report
from sqlalchemy.orm import Session
from app.api.dependencies import database
from fastapi import APIRouter, Depends
from app.services.user_manager import current_active_user
from app.crud.crud_users import get_user_by_email
from app.db.fastapi_user import User


router = APIRouter()


@router.post("", description="악성 사용자를 신고합니다.")
def report_user(
*,
db: Session = Depends(database.get_db),
current_user: User = Depends(current_active_user),
report_in: ReportUser
) -> Any:
reporter_id = get_user_by_email(db=db, email=current_user.email).id

report = create_report(
db=db,
reporter_id=reporter_id,
report_in=report_in,
)
return {"message": "Report submitted successfully", "report_record": report}
32 changes: 32 additions & 0 deletions app/crud/crud_reports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from sqlalchemy.orm import Session
from app.models.schemas.reports import Report
from app.models.domain.reports import ReportUser, ReportResponse
from app.models.schemas.users import User
from fastapi import HTTPException


def create_report(
db: Session, reporter_id: int, report_in: ReportUser
) -> ReportResponse:
# Get the reported user by user name
reported_user = (
db.query(User).filter(User.username == report_in.reported_user_name).first()
)

if not reported_user:
raise HTTPException(status_code=400, detail="Reported user not found")

# Create the report
report = Report(
reporter_id=reporter_id, reported_id=reported_user.id, reason=report_in.reason
)
db.add(report)
db.commit()
db.refresh(report)

report_response = ReportResponse(
reporter_id=report.reporter_id,
reported_id=report.reported_id,
reason=report.reason,
)
return report_response
15 changes: 15 additions & 0 deletions app/models/domain/reports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from pydantic import BaseModel


class ReportUser(BaseModel):
reported_user_name: str
reason: str


class ReportResponse(BaseModel):
reporter_id: int
reported_id: int
reason: str

class Config:
orm_mode = True
13 changes: 13 additions & 0 deletions app/models/schemas/reports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship
from app.db.base_class import Base


class Report(Base):
id = Column(Integer, primary_key=True, autoincrement=True, index=True)
reporter_id = Column(Integer, ForeignKey("user.id"))
reported_id = Column(Integer, ForeignKey("user.id"))
reason = Column(String(255), nullable=False)

reporter = relationship("User", foreign_keys=[reporter_id])
reported = relationship("User", foreign_keys=[reported_id])

0 comments on commit 55bb971

Please sign in to comment.