-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from lukas-holzner/add-crud-endpoints
Sperated into users and meeting and deleted items endpoint
- Loading branch information
Showing
4 changed files
with
114 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |