Skip to content

Commit

Permalink
first schema
Browse files Browse the repository at this point in the history
  • Loading branch information
GuidoGrogger committed Sep 30, 2024
1 parent 6aae00f commit 1c87693
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 5 deletions.
4 changes: 2 additions & 2 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
from .routers import items, meetings
import os

models.Base.metadata.create_all(bind=engine)
Expand All @@ -33,7 +33,7 @@

# Include the router with the /api prefix
app.include_router(items.router, prefix="/api")

app.include_router(meetings.router, tags=["meetings"], prefix="/api")

@app.get("/", include_in_schema=False)
async def serve_index():
Expand Down
26 changes: 24 additions & 2 deletions backend/app/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
from sqlalchemy import Column, Integer, String
from datetime import datetime
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime
from sqlalchemy.orm import relationship
from .database import Base

class Item(Base):
__tablename__ = "items"

id = Column(Integer, primary_key=True, index=True)
name = Column(String, index=True)
description = Column(String)
description = Column(String)

class User(Base):
__tablename__ = "users"

id = Column(Integer, primary_key=True, index=True)
username = Column(String, unique=True, index=True)

meetings = relationship("Meeting", back_populates="user")

class Meeting(Base):
__tablename__ = "meetings"

id = Column(Integer, primary_key=True, index=True)
title = Column(String, index=True)
description = Column(String)
date = Column(DateTime)
user_id = Column(Integer, ForeignKey("users.id"))

user = relationship("User", back_populates="meetings")

73 changes: 73 additions & 0 deletions backend/app/routers/meetings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from typing import List

from .. import schemas
from ..database import get_db

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)

@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

@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

@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

@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

# 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)

@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)
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)
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)
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)
if db_meeting is None:
raise HTTPException(status_code=404, detail="Meeting not found")
return db_meeting
36 changes: 35 additions & 1 deletion backend/app/schemas.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pydantic import BaseModel
from datetime import datetime

class ItemBase(BaseModel):
name: str
Expand All @@ -11,4 +12,37 @@ class Item(ItemBase):
id: int

class Config:
orm_mode = True
orm_mode = True

class UserBase(BaseModel):
username: str

class UserCreate(UserBase):
pass

class User(UserBase):
id: int

class Config:
orm_mode = True

class MeetingBase(BaseModel):
title: str
description: str | None = None
date: datetime
user_id: int

class MeetingCreate(MeetingBase):
pass

class Meeting(MeetingBase):
id: int

class Config:
orm_mode = True

class UserWithMeetings(User):
meetings: list[Meeting] = []

class MeetingWithUser(Meeting):
user: User

0 comments on commit 1c87693

Please sign in to comment.