-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## 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
Showing
5 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |