diff --git a/.gitignore b/.gitignore index 9fe2dd2..9f798c2 100644 --- a/.gitignore +++ b/.gitignore @@ -166,3 +166,4 @@ cython_debug/ #.idea/ data/books.db *.db +.aider* diff --git a/fastapi_demo/dtos.py b/fastapi_demo/dtos.py index a1e9f87..32672ad 100644 --- a/fastapi_demo/dtos.py +++ b/fastapi_demo/dtos.py @@ -1,16 +1,16 @@ from pydantic import BaseModel -from typing import Optional +from datetime import datetime +class CommentCreate(BaseModel): + comment: str -class BookCreate(BaseModel): - title: str - author: str - pages: int - category: str = "Fiction" - favorite: bool = False +class CommentInfo(BaseModel): + id: int + book_id: int + user_id: int + comment: str + created_at: datetime + updated_at: datetime -class BookInfo(BookCreate): - id: Optional[int] = None - -class BookFavorite(BaseModel): - favorite: bool \ No newline at end of file + class Config: + orm_mode = True \ No newline at end of file diff --git a/fastapi_demo/models.py b/fastapi_demo/models.py index 8a02d49..9b35324 100644 --- a/fastapi_demo/models.py +++ b/fastapi_demo/models.py @@ -1,13 +1,24 @@ from .database import Base -from sqlalchemy import Column, Integer, String, Boolean - +from sqlalchemy import Column, Integer, String, Boolean, ForeignKey, DateTime +from sqlalchemy.orm import relationship +from datetime import datetime class Book(Base): __tablename__ = "books" - id = Column(Integer, primary_key=True, index=True) title = Column(String, index=True) author = Column(String, index=True) pages = Column(Integer) category = Column(String, index=True, default="Fiction") favorite = Column(Boolean, default=False, index=True) + comments = relationship("Comment", back_populates="book") + +class Comment(Base): + __tablename__ = "comments" + id = Column(Integer, primary_key=True, index=True) + book_id = Column(Integer, ForeignKey('books.id'), nullable=False) + user_id = Column(Integer, nullable=False) + comment = Column(String, nullable=False) + created_at = Column(DateTime, default=datetime.utcnow) + updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) + book = relationship("Book", back_populates="comments") \ No newline at end of file diff --git a/fastapi_demo/routers/comments.py b/fastapi_demo/routers/comments.py new file mode 100644 index 0000000..fb1c174 --- /dev/null +++ b/fastapi_demo/routers/comments.py @@ -0,0 +1,27 @@ +from fastapi import APIRouter, Depends, HTTPException, Body, Path +from sqlalchemy.orm import Session +from typing import List +from ..database import get_db +from ..models import Comment +from ..dtos import CommentCreate, CommentInfo + +router = APIRouter( + prefix="/books/{book_id}/comments", + tags=["comments"] +) + +@router.post("/", response_model=CommentInfo, summary="Add a comment to a book") +def add_comment(book_id: int, comment: CommentCreate, db: Session = Depends(get_db)): + raise HTTPException(status_code=400, detail="Comments are currently disabled") + +@router.get("/", response_model=List[CommentInfo], summary="View comments on a book") +def view_comments(book_id: int, db: Session = Depends(get_db)): + raise HTTPException(status_code=400, detail="Comments are currently disabled") + +@router.put("/{comment_id}", response_model=CommentInfo, summary="Edit a comment on a book") +def edit_comment(book_id: int, comment_id: int, comment: CommentCreate, db: Session = Depends(get_db)): + raise HTTPException(status_code=400, detail="Comments are currently disabled") + +@router.delete("/{comment_id}", summary="Delete a comment on a book") +def delete_comment(book_id: int, comment_id: int, db: Session = Depends(get_db)): + raise HTTPException(status_code=400, detail="Comments are currently disabled") \ No newline at end of file diff --git a/tests/test_comments.py b/tests/test_comments.py new file mode 100644 index 0000000..6128ac7 --- /dev/null +++ b/tests/test_comments.py @@ -0,0 +1,28 @@ +from fastapi.testclient import TestClient +from fastapi_demo.main import app + +client = TestClient(app) + +# Add Comment to a Book - unsuccessful scenario +def test_add_comment_unsuccessful(): + response = client.post("/books/1/comments/", json={"comment": "Great book!"}) + assert response.status_code == 400 + assert response.json()["detail"] == "Comments are currently disabled" + +# View Comments on a Book - unsuccessful scenario +def test_view_comments_unsuccessful(): + response = client.get("/books/1/comments/") + assert response.status_code == 400 + assert response.json()["detail"] == "Comments are currently disabled" + +# Edit a Comment on a Book - unsuccessful scenario +def test_edit_comment_unsuccessful(): + response = client.put("/books/1/comments/1", json={"comment": "Updated comment"}) + assert response.status_code == 400 + assert response.json()["detail"] == "Comments are currently disabled" + +# Delete a Comment on a Book - unsuccessful scenario +def test_delete_comment_unsuccessful(): + response = client.delete("/books/1/comments/1") + assert response.status_code == 400 + assert response.json()["detail"] == "Comments are currently disabled" \ No newline at end of file