Skip to content
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

Added Project CRUD operations #1

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from fastapi import FastAPI

from src.projects import router as projects_router

app = FastAPI()

app.include_router(projects_router.router)


@app.get("/health")
def read_health() -> dict[str, str]:
Expand Down
Empty file added src/projects/__init__.py
Empty file.
68 changes: 68 additions & 0 deletions src/projects/router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from fastapi import APIRouter, HTTPException

from src.projects.schemas import Project

# - - - - - - - - - - IN-MEMORY DB - - - - - - - - - - #

# Projects
first = Project(id=1, name="first", description="The First project")
second = Project(id=2, name="second", description="The Second project")
third = Project(id=3, name="third", description="The Third project")

# Projects DB
projects_db = {1: first, 2: second, 3: third}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - #

router = APIRouter(prefix="/projects")


@router.get("/")
def read_projects() -> dict[int, Project]:
"""Read all projects"""
return projects_db


@router.get("/{project_id}/info")
def read_project(project_id: int) -> Project:
"""Read project description"""
if project_id not in projects_db:
raise HTTPException(status_code=404, detail="Project not found")

return projects_db[project_id]


@router.post("/")
def create_project(project: Project) -> Project:
"""Create new project"""
if project.id in projects_db:
raise HTTPException(
status_code=400, detail=f"Project with {project.id} already exists."
)

projects_db[project.id] = project
return project


@router.put("/{project_id}/info")
def update_project(project_id: int, project: Project) -> Project:
"""Update project"""
if project_id not in projects_db:
raise HTTPException(
status_code=404, detail=f"Project with {project_id} does not exist."
)

projects_db[project_id] = project
return project


@router.delete("/{project_id}")
def delete_project(project_id: int) -> Project:
"""Delete project"""
if project_id not in projects_db:
raise HTTPException(
status_code=404, detail=f"Project with {project_id} does not exist."
)

project = projects_db.pop(project_id)
return project
9 changes: 9 additions & 0 deletions src/projects/schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from pydantic import BaseModel


class Project(BaseModel):
id: int
name: str
description: str
# logo: ?
# documents: ?
Empty file added tests/projects/__init__.py
Empty file.
33 changes: 33 additions & 0 deletions tests/projects/test_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from fastapi.testclient import TestClient

from src.main import app

client = TestClient(app)


def test_read_project() -> None:
data = {"id": 1, "name": "first", "description": "The First project"}
res = client.get("/projects/1/info")
assert res.status_code == 200
assert res.json() == data


def test_create_project() -> None:
data = {"id": 4, "name": "fourth", "description": "The Fourth project"}
res = client.post("/projects/", json=data)
assert res.status_code == 200
assert res.json() == data


def test_update_project() -> None:
data = {"id": 3, "name": "third", "description": "The Third project"}
res = client.put("/projects/3/info", json=data)
assert res.status_code == 200
assert res.json() == data


def test_delete_project() -> None:
data = {"id": 2, "name": "second", "description": "The Second project"}
res = client.delete("/projects/2")
assert res.status_code == 200
assert res.json() == data