mirror of
https://github.com/a-mayb3/Kanban_clone_backend.git
synced 2026-03-21 18:15:37 +01:00
Starting working on endpoints
This commit is contained in:
parent
552ab862bb
commit
ec9e0fad78
1 changed files with 60 additions and 18 deletions
78
main.py
78
main.py
|
|
@ -1,24 +1,66 @@
|
||||||
|
from enum import Enum
|
||||||
from fastapi import FastAPI, HTTPException, Depends
|
from fastapi import FastAPI, HTTPException, Depends
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel, ConfigDict
|
||||||
from typing import List, Annotated
|
from typing import List, Annotated, Optional
|
||||||
|
|
||||||
|
import models
|
||||||
|
from database import SessionLocal, engine
|
||||||
|
from sqlalchemy.orm import Session, joinedload
|
||||||
|
|
||||||
|
from tasks import TaskBase, TaskList
|
||||||
|
from users import UserBase
|
||||||
|
from projects import ProjectBase, ProjectCreate, ProjectList
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
class TaskStatus():
|
models.Base.metadata.create_all(bind=engine)
|
||||||
PENDING = "pending"
|
|
||||||
IN_PROGRESS = "in_progress"
|
def get_db():
|
||||||
COMPLETED = "completed"
|
db = SessionLocal()
|
||||||
FAILED = "failed"
|
try:
|
||||||
STASHED = "stashed"
|
yield db
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
db_dependency = Annotated[Session, Depends(get_db)]
|
||||||
|
|
||||||
|
@app.post("/projects/", response_model=ProjectBase)
|
||||||
|
def create_project(project: ProjectCreate, db: db_dependency):
|
||||||
|
db_project = models.Project(name=project.name, description=project.description)
|
||||||
|
db.add(db_project)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_project)
|
||||||
|
|
||||||
|
for task in project.tasks:
|
||||||
|
db_task = models.Task(
|
||||||
|
title=task.title,
|
||||||
|
description=task.description,
|
||||||
|
status=task.status.value,
|
||||||
|
project_id=db_project.id
|
||||||
|
)
|
||||||
|
db.add(db_task)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
if project.user_ids:
|
||||||
|
users = db.query(models.User).filter(models.User.id.in_(project.user_ids)).all()
|
||||||
|
db_project.users.extend(users)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
db_project = db.query(models.Project).options(
|
||||||
|
joinedload(models.Project.tasks),
|
||||||
|
joinedload(models.Project.users)
|
||||||
|
).filter(models.Project.id == db_project.id).first()
|
||||||
|
|
||||||
|
return db_project
|
||||||
|
|
||||||
|
@app.get("/projects/{project_id}", response_model=ProjectBase)
|
||||||
|
def read_project(project_id: int, db: db_dependency):
|
||||||
|
db_project = db.query(models.Project).options(
|
||||||
|
joinedload(models.Project.tasks),
|
||||||
|
joinedload(models.Project.users)
|
||||||
|
).filter(models.Project.id == project_id).first()
|
||||||
|
if db_project is None:
|
||||||
|
raise HTTPException(status_code=404, detail="Project not found")
|
||||||
|
return db_project
|
||||||
|
|
||||||
class TaskBase(BaseModel):
|
|
||||||
id: int
|
|
||||||
title: str
|
|
||||||
description: str
|
|
||||||
status: TaskStatus
|
|
||||||
|
|
||||||
class ProjectBase(BaseModel):
|
|
||||||
id: int
|
|
||||||
name: str
|
|
||||||
description: str
|
|
||||||
tasks: List[TaskBase]
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue