diff --git a/backend/app/main.py b/backend/app/main.py index 963bbe2..dc1de32 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -6,7 +6,7 @@ from fastapi.middleware.cors import CORSMiddleware from .database import engine from . import models -from .routers import items, meetings +from .routers import meetings, users import os models.Base.metadata.create_all(bind=engine) @@ -32,8 +32,8 @@ ) # Include the router with the /api prefix -app.include_router(items.router, prefix="/api") -app.include_router(meetings.router, tags=["meetings"], prefix="/api") +app.include_router(meetings.router, tags=["meetings"]) +app.include_router(users.router, tags=["users"]) @app.get("/", include_in_schema=False) async def serve_index(): diff --git a/backend/app/routers/items.py b/backend/app/routers/items.py deleted file mode 100644 index 9452025..0000000 --- a/backend/app/routers/items.py +++ /dev/null @@ -1,26 +0,0 @@ -from fastapi import APIRouter, Depends, HTTPException -from sqlalchemy.orm import Session -from .. import models, schemas -from ..database import get_db - -router = APIRouter() - -@router.post("/items/", response_model=schemas.Item) -def create_item(item: schemas.ItemCreate, db: Session = Depends(get_db)): - db_item = models.Item(**item.dict()) - db.add(db_item) - db.commit() - db.refresh(db_item) - return db_item - -@router.get("/items/", response_model=list[schemas.Item]) -def read_items(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): - items = db.query(models.Item).offset(skip).limit(limit).all() - return items - -@router.get("/items/{item_id}", response_model=schemas.Item) -def read_item(item_id: int, db: Session = Depends(get_db)): - db_item = db.query(models.Item).filter(models.Item.id == item_id).first() - if db_item is None: - raise HTTPException(status_code=404, detail="Item not found") - return db_item \ No newline at end of file diff --git a/backend/app/routers/meetings.py b/backend/app/routers/meetings.py index 254d57c..42517d1 100644 --- a/backend/app/routers/meetings.py +++ b/backend/app/routers/meetings.py @@ -4,70 +4,67 @@ from .. import schemas from ..database import get_db +from ..models import Meeting router = APIRouter() -# User CRUD endpoints -@router.post("/users/", response_model=schemas.User) -def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)): - db_user = crud.get_user_by_username(db, username=user.username) - if db_user: - raise HTTPException(status_code=400, detail="Username already registered") - return crud.create_user(db=db, user=user) +# Meeting CRUD operations +def get_meeting(db: Session, meeting_id: int): + return db.query(Meeting).filter(Meeting.id == meeting_id).first() -@router.get("/users/", response_model=List[schemas.User]) -def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): - users = crud.get_users(db, skip=skip, limit=limit) - return users +def get_meetings(db: Session, skip: int = 0, limit: int = 100): + return db.query(Meeting).offset(skip).limit(limit).all() -@router.get("/users/{user_id}", response_model=schemas.UserWithMeetings) -def read_user(user_id: int, db: Session = Depends(get_db)): - db_user = crud.get_user(db, user_id=user_id) - if db_user is None: - raise HTTPException(status_code=404, detail="User not found") - return db_user +def create_meeting(db: Session, meeting: schemas.MeetingCreate): + db_meeting = Meeting(**meeting.dict()) + db.add(db_meeting) + db.commit() + db.refresh(db_meeting) + return db_meeting -@router.put("/users/{user_id}", response_model=schemas.User) -def update_user(user_id: int, user: schemas.UserCreate, db: Session = Depends(get_db)): - db_user = crud.update_user(db, user_id=user_id, user=user) - if db_user is None: - raise HTTPException(status_code=404, detail="User not found") - return db_user +def update_meeting(db: Session, meeting_id: int, meeting: schemas.MeetingCreate): + db_meeting = get_meeting(db, meeting_id) + if db_meeting: + for key, value in meeting.dict().items(): + setattr(db_meeting, key, value) + db.commit() + db.refresh(db_meeting) + return db_meeting -@router.delete("/users/{user_id}", response_model=schemas.User) -def delete_user(user_id: int, db: Session = Depends(get_db)): - db_user = crud.delete_user(db, user_id=user_id) - if db_user is None: - raise HTTPException(status_code=404, detail="User not found") - return db_user +def delete_meeting(db: Session, meeting_id: int): + db_meeting = get_meeting(db, meeting_id) + if db_meeting: + db.delete(db_meeting) + db.commit() + return db_meeting # Meeting CRUD endpoints @router.post("/meetings/", response_model=schemas.Meeting) -def create_meeting(meeting: schemas.MeetingCreate, db: Session = Depends(get_db)): - return crud.create_meeting(db=db, meeting=meeting) +def create_meeting_route(meeting: schemas.MeetingCreate, db: Session = Depends(get_db)): + return create_meeting(db=db, meeting=meeting) @router.get("/meetings/", response_model=List[schemas.Meeting]) def read_meetings(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): - meetings = crud.get_meetings(db, skip=skip, limit=limit) + meetings = get_meetings(db, skip=skip, limit=limit) return meetings @router.get("/meetings/{meeting_id}", response_model=schemas.MeetingWithUser) def read_meeting(meeting_id: int, db: Session = Depends(get_db)): - db_meeting = crud.get_meeting(db, meeting_id=meeting_id) + db_meeting = get_meeting(db, meeting_id=meeting_id) if db_meeting is None: raise HTTPException(status_code=404, detail="Meeting not found") return db_meeting @router.put("/meetings/{meeting_id}", response_model=schemas.Meeting) -def update_meeting(meeting_id: int, meeting: schemas.MeetingCreate, db: Session = Depends(get_db)): - db_meeting = crud.update_meeting(db, meeting_id=meeting_id, meeting=meeting) +def update_meeting_route(meeting_id: int, meeting: schemas.MeetingCreate, db: Session = Depends(get_db)): + db_meeting = update_meeting(db, meeting_id=meeting_id, meeting=meeting) if db_meeting is None: raise HTTPException(status_code=404, detail="Meeting not found") return db_meeting @router.delete("/meetings/{meeting_id}", response_model=schemas.Meeting) -def delete_meeting(meeting_id: int, db: Session = Depends(get_db)): - db_meeting = crud.delete_meeting(db, meeting_id=meeting_id) +def delete_meeting_route(meeting_id: int, db: Session = Depends(get_db)): + db_meeting = delete_meeting(db, meeting_id=meeting_id) if db_meeting is None: raise HTTPException(status_code=404, detail="Meeting not found") return db_meeting diff --git a/backend/app/routers/users.py b/backend/app/routers/users.py new file mode 100644 index 0000000..113a92a --- /dev/null +++ b/backend/app/routers/users.py @@ -0,0 +1,77 @@ +from fastapi import APIRouter, Depends, HTTPException +from sqlalchemy.orm import Session +from typing import List +from sqlalchemy.exc import IntegrityError + +from .. import schemas +from ..database import get_db +from ..models import User + +router = APIRouter() + +# User CRUD operations +def get_user(db: Session, user_id: int): + return db.query(User).filter(User.id == user_id).first() + +def get_user_by_username(db: Session, username: str): + return db.query(User).filter(User.username == username).first() + +def get_users(db: Session, skip: int = 0, limit: int = 100): + return db.query(User).offset(skip).limit(limit).all() + +def create_user(db: Session, user: schemas.UserCreate): + db_user = User(**user.dict()) + db.add(db_user) + db.commit() + db.refresh(db_user) + return db_user + +def update_user(db: Session, user_id: int, user: schemas.UserCreate): + db_user = get_user(db, user_id) + if db_user: + for key, value in user.dict().items(): + setattr(db_user, key, value) + db.commit() + db.refresh(db_user) + return db_user + +def delete_user(db: Session, user_id: int): + db_user = get_user(db, user_id) + if db_user: + db.delete(db_user) + db.commit() + return db_user + +# User CRUD endpoints +@router.post("/users/", response_model=schemas.User) +def create_user_route(user: schemas.UserCreate, db: Session = Depends(get_db)): + db_user = get_user_by_username(db, username=user.username) + if db_user: + raise HTTPException(status_code=400, detail="Username already registered") + return create_user(db=db, user=user) + +@router.get("/users/", response_model=List[schemas.User]) +def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): + users = get_users(db, skip=skip, limit=limit) + return users + +@router.get("/users/{user_id}", response_model=schemas.UserWithMeetings) +def read_user(user_id: int, db: Session = Depends(get_db)): + db_user = get_user(db, user_id=user_id) + if db_user is None: + raise HTTPException(status_code=404, detail="User not found") + return db_user + +@router.put("/users/{user_id}", response_model=schemas.User) +def update_user_route(user_id: int, user: schemas.UserCreate, db: Session = Depends(get_db)): + db_user = update_user(db, user_id=user_id, user=user) + if db_user is None: + raise HTTPException(status_code=404, detail="User not found") + return db_user + +@router.delete("/users/{user_id}", response_model=schemas.User) +def delete_user_route(user_id: int, db: Session = Depends(get_db)): + db_user = delete_user(db, user_id=user_id) + if db_user is None: + raise HTTPException(status_code=404, detail="User not found") + return db_user