Switch to angular
This commit is contained in:
117
app/api/tags.py
Normal file
117
app/api/tags.py
Normal file
@@ -0,0 +1,117 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List
|
||||
|
||||
from app.database import get_db
|
||||
from app.models.tag import Tag
|
||||
from app.schemas.tag import TagCreate, TagUpdate, TagResponse
|
||||
|
||||
router = APIRouter(prefix="/api/v1/tags", tags=["tags"])
|
||||
|
||||
|
||||
@router.post("/", response_model=TagResponse, status_code=201)
|
||||
async def create_tag(tag: TagCreate, db: Session = Depends(get_db)):
|
||||
"""
|
||||
Create a new tag
|
||||
|
||||
- **name**: Tag name (unique, required)
|
||||
- **description**: Tag description (optional)
|
||||
- **color**: Hex color code (optional, e.g., #FF5733)
|
||||
"""
|
||||
# Check if tag already exists
|
||||
existing_tag = db.query(Tag).filter(Tag.name == tag.name).first()
|
||||
if existing_tag:
|
||||
raise HTTPException(status_code=400, detail=f"Tag with name '{tag.name}' already exists")
|
||||
|
||||
db_tag = Tag(**tag.model_dump())
|
||||
db.add(db_tag)
|
||||
db.commit()
|
||||
db.refresh(db_tag)
|
||||
|
||||
return db_tag
|
||||
|
||||
|
||||
@router.get("/", response_model=List[TagResponse])
|
||||
async def list_tags(
|
||||
limit: int = Query(default=100, le=1000),
|
||||
offset: int = Query(default=0, ge=0),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""List all tags with pagination"""
|
||||
tags = db.query(Tag).order_by(Tag.name).offset(offset).limit(limit).all()
|
||||
return tags
|
||||
|
||||
|
||||
@router.get("/{tag_id}", response_model=TagResponse)
|
||||
async def get_tag(tag_id: int, db: Session = Depends(get_db)):
|
||||
"""Get tag by ID"""
|
||||
tag = db.query(Tag).filter(Tag.id == tag_id).first()
|
||||
if not tag:
|
||||
raise HTTPException(status_code=404, detail="Tag not found")
|
||||
return tag
|
||||
|
||||
|
||||
@router.get("/name/{tag_name}", response_model=TagResponse)
|
||||
async def get_tag_by_name(tag_name: str, db: Session = Depends(get_db)):
|
||||
"""Get tag by name"""
|
||||
tag = db.query(Tag).filter(Tag.name == tag_name).first()
|
||||
if not tag:
|
||||
raise HTTPException(status_code=404, detail="Tag not found")
|
||||
return tag
|
||||
|
||||
|
||||
@router.put("/{tag_id}", response_model=TagResponse)
|
||||
async def update_tag(tag_id: int, tag_update: TagUpdate, db: Session = Depends(get_db)):
|
||||
"""
|
||||
Update a tag
|
||||
|
||||
- **name**: Tag name (optional)
|
||||
- **description**: Tag description (optional)
|
||||
- **color**: Hex color code (optional)
|
||||
"""
|
||||
tag = db.query(Tag).filter(Tag.id == tag_id).first()
|
||||
if not tag:
|
||||
raise HTTPException(status_code=404, detail="Tag not found")
|
||||
|
||||
# Check if new name conflicts with existing tag
|
||||
if tag_update.name and tag_update.name != tag.name:
|
||||
existing_tag = db.query(Tag).filter(Tag.name == tag_update.name).first()
|
||||
if existing_tag:
|
||||
raise HTTPException(status_code=400, detail=f"Tag with name '{tag_update.name}' already exists")
|
||||
|
||||
# Update fields
|
||||
update_data = tag_update.model_dump(exclude_unset=True)
|
||||
for field, value in update_data.items():
|
||||
setattr(tag, field, value)
|
||||
|
||||
db.commit()
|
||||
db.refresh(tag)
|
||||
|
||||
return tag
|
||||
|
||||
|
||||
@router.delete("/{tag_id}")
|
||||
async def delete_tag(tag_id: int, db: Session = Depends(get_db)):
|
||||
"""Delete a tag"""
|
||||
tag = db.query(Tag).filter(Tag.id == tag_id).first()
|
||||
if not tag:
|
||||
raise HTTPException(status_code=404, detail="Tag not found")
|
||||
|
||||
db.delete(tag)
|
||||
db.commit()
|
||||
|
||||
return {"message": f"Tag '{tag.name}' deleted successfully"}
|
||||
|
||||
|
||||
@router.post("/search", response_model=List[TagResponse])
|
||||
async def search_tags(
|
||||
query: str = Query(..., min_length=1, description="Search query"),
|
||||
limit: int = Query(default=100, le=1000),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Search tags by name or description"""
|
||||
tags = db.query(Tag).filter(
|
||||
(Tag.name.ilike(f"%{query}%")) | (Tag.description.ilike(f"%{query}%"))
|
||||
).order_by(Tag.name).limit(limit).all()
|
||||
|
||||
return tags
|
||||
Reference in New Issue
Block a user