Skip to content

Commit

Permalink
Sperated into users and meeting and deleted items endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-holzner committed Sep 30, 2024
1 parent 1c87693 commit 3dee047
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 66 deletions.
6 changes: 3 additions & 3 deletions backend/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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():
Expand Down
26 changes: 0 additions & 26 deletions backend/app/routers/items.py

This file was deleted.

71 changes: 34 additions & 37 deletions backend/app/routers/meetings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
77 changes: 77 additions & 0 deletions backend/app/routers/users.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3dee047

Please sign in to comment.