Skip to content

Feature/gen 75 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -166,3 +166,4 @@ cython_debug/
#.idea/
data/books.db
*.db
.aider*
24 changes: 12 additions & 12 deletions fastapi_demo/dtos.py
Original file line number Diff line number Diff line change
@@ -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
class Config:
orm_mode = True
17 changes: 14 additions & 3 deletions fastapi_demo/models.py
Original file line number Diff line number Diff line change
@@ -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")
27 changes: 27 additions & 0 deletions fastapi_demo/routers/comments.py
Original file line number Diff line number Diff line change
@@ -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")
28 changes: 28 additions & 0 deletions tests/test_comments.py
Original file line number Diff line number Diff line change
@@ -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"