[V0.4] Added TODOs
This commit is contained in:
2
backend/modules/todo/__init__.py
Normal file
2
backend/modules/todo/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
# backend/modules/todo/__init__.py
|
||||
# This file makes the 'todo' directory a Python package.
|
||||
BIN
backend/modules/todo/__pycache__/__init__.cpython-312.pyc
Normal file
BIN
backend/modules/todo/__pycache__/__init__.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/modules/todo/__pycache__/api.cpython-312.pyc
Normal file
BIN
backend/modules/todo/__pycache__/api.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/modules/todo/__pycache__/models.cpython-312.pyc
Normal file
BIN
backend/modules/todo/__pycache__/models.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/modules/todo/__pycache__/schemas.cpython-312.pyc
Normal file
BIN
backend/modules/todo/__pycache__/schemas.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/modules/todo/__pycache__/service.cpython-312.pyc
Normal file
BIN
backend/modules/todo/__pycache__/service.cpython-312.pyc
Normal file
Binary file not shown.
62
backend/modules/todo/api.py
Normal file
62
backend/modules/todo/api.py
Normal file
@@ -0,0 +1,62 @@
|
||||
# backend/modules/todo/api.py
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List
|
||||
|
||||
from . import service, schemas
|
||||
from core.database import get_db
|
||||
from modules.auth.dependencies import get_current_user # Corrected import
|
||||
from modules.auth.models import User # Assuming User model is in auth.models
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/todos",
|
||||
tags=["todos"],
|
||||
dependencies=[Depends(get_current_user)], # Corrected dependency
|
||||
responses={404: {"description": "Not found"}},
|
||||
)
|
||||
|
||||
@router.post("/", response_model=schemas.Todo, status_code=status.HTTP_201_CREATED)
|
||||
def create_todo_endpoint(
|
||||
todo: schemas.TodoCreate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user) # Corrected dependency
|
||||
):
|
||||
return service.create_todo(db=db, todo=todo, user=current_user)
|
||||
|
||||
@router.get("/", response_model=List[schemas.Todo])
|
||||
def read_todos_endpoint(
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user) # Corrected dependency
|
||||
):
|
||||
todos = service.get_todos(db=db, user=current_user, skip=skip, limit=limit)
|
||||
return todos
|
||||
|
||||
@router.get("/{todo_id}", response_model=schemas.Todo)
|
||||
def read_todo_endpoint(
|
||||
todo_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user) # Corrected dependency
|
||||
):
|
||||
db_todo = service.get_todo(db=db, todo_id=todo_id, user=current_user)
|
||||
if db_todo is None:
|
||||
raise HTTPException(status_code=404, detail="Todo not found")
|
||||
return db_todo
|
||||
|
||||
@router.put("/{todo_id}", response_model=schemas.Todo)
|
||||
def update_todo_endpoint(
|
||||
todo_id: int,
|
||||
todo_update: schemas.TodoUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user) # Corrected dependency
|
||||
):
|
||||
return service.update_todo(db=db, todo_id=todo_id, todo_update=todo_update, user=current_user)
|
||||
|
||||
@router.delete("/{todo_id}", response_model=schemas.Todo)
|
||||
def delete_todo_endpoint(
|
||||
todo_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user) # Corrected dependency
|
||||
):
|
||||
return service.delete_todo(db=db, todo_id=todo_id, user=current_user)
|
||||
17
backend/modules/todo/models.py
Normal file
17
backend/modules/todo/models.py
Normal file
@@ -0,0 +1,17 @@
|
||||
# backend/modules/todo/models.py
|
||||
from sqlalchemy import Column, Integer, String, Boolean, DateTime, ForeignKey
|
||||
from sqlalchemy.orm import relationship
|
||||
from core.database import Base
|
||||
import datetime
|
||||
|
||||
class Todo(Base):
|
||||
__tablename__ = "todos"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
task = Column(String, index=True, nullable=False)
|
||||
date = Column(DateTime, nullable=True)
|
||||
remind = Column(Boolean, default=False)
|
||||
complete = Column(Boolean, default=False)
|
||||
owner_id = Column(Integer, ForeignKey("users.id"))
|
||||
|
||||
owner = relationship("User") # Add relationship if needed, assuming User model exists in auth.models
|
||||
26
backend/modules/todo/schemas.py
Normal file
26
backend/modules/todo/schemas.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# backend/modules/todo/schemas.py
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
import datetime
|
||||
|
||||
class TodoBase(BaseModel):
|
||||
task: str
|
||||
date: Optional[datetime.datetime] = None
|
||||
remind: bool = False
|
||||
complete: bool = False
|
||||
|
||||
class TodoCreate(TodoBase):
|
||||
pass
|
||||
|
||||
class TodoUpdate(BaseModel):
|
||||
task: Optional[str] = None
|
||||
date: Optional[datetime.datetime] = None
|
||||
remind: Optional[bool] = None
|
||||
complete: Optional[bool] = None
|
||||
|
||||
class Todo(TodoBase):
|
||||
id: int
|
||||
owner_id: int
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
36
backend/modules/todo/service.py
Normal file
36
backend/modules/todo/service.py
Normal file
@@ -0,0 +1,36 @@
|
||||
# backend/modules/todo/service.py
|
||||
from sqlalchemy.orm import Session
|
||||
from . import models, schemas
|
||||
from modules.auth.models import User # Assuming User model is in auth.models
|
||||
from fastapi import HTTPException, status
|
||||
|
||||
def create_todo(db: Session, todo: schemas.TodoCreate, user: User):
|
||||
db_todo = models.Todo(**todo.dict(), owner_id=user.id)
|
||||
db.add(db_todo)
|
||||
db.commit()
|
||||
db.refresh(db_todo)
|
||||
return db_todo
|
||||
|
||||
def get_todos(db: Session, user: User, skip: int = 0, limit: int = 100):
|
||||
return db.query(models.Todo).filter(models.Todo.owner_id == user.id).offset(skip).limit(limit).all()
|
||||
|
||||
def get_todo(db: Session, todo_id: int, user: User):
|
||||
db_todo = db.query(models.Todo).filter(models.Todo.id == todo_id, models.Todo.owner_id == user.id).first()
|
||||
if db_todo is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Todo not found")
|
||||
return db_todo
|
||||
|
||||
def update_todo(db: Session, todo_id: int, todo_update: schemas.TodoUpdate, user: User):
|
||||
db_todo = get_todo(db=db, todo_id=todo_id, user=user) # Reuse get_todo to check ownership and existence
|
||||
update_data = todo_update.dict(exclude_unset=True)
|
||||
for key, value in update_data.items():
|
||||
setattr(db_todo, key, value)
|
||||
db.commit()
|
||||
db.refresh(db_todo)
|
||||
return db_todo
|
||||
|
||||
def delete_todo(db: Session, todo_id: int, user: User):
|
||||
db_todo = get_todo(db=db, todo_id=todo_id, user=user) # Reuse get_todo to check ownership and existence
|
||||
db.delete(db_todo)
|
||||
db.commit()
|
||||
return db_todo
|
||||
Reference in New Issue
Block a user