forked from DanaY326/BridgeTO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.py
More file actions
96 lines (83 loc) · 2.7 KB
/
events.py
File metadata and controls
96 lines (83 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy import create_engine, Column, Integer, String, Boolean, DateTime
import sqlalchemy
from sqlalchemy.orm import sessionmaker, Session
from sqlalchemy.ext.mutable import MutableList
from sqlalchemy.types import TypeDecorator, String as StringType
from pydantic import BaseModel
from typing import Optional, List
import datetime
import json
DATABASE_URL = "sqlite:///./test.db"
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = sqlalchemy.orm.declarative_base()
class JSONList(TypeDecorator):
"""Stores and retrieves a Python list as JSON-encoded text."""
impl = StringType
cache_ok = True
def process_bind_param(self, value, dialect):
if value is not None:
return json.dumps(value)
return None
def process_result_value(self, value, dialect):
if value is not None:
return json.loads(value)
return None
class Event(Base):
__tablename__ = "events"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, index=True)
description = Column(String)
datetime = Column(String)
groups = Column(MutableList.as_mutable(JSONList))
approved = Column(Boolean)
category = Column(String)
organization = Column(String)
class ItemCreate(BaseModel):
name: str
description: str
datetime: str
groups: list[str]
approved: bool
category: str
organization: str
Base.metadata.create_all(bind=engine)
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
router = APIRouter(prefix="/events", tags=["events"])
class ItemResponse(BaseModel):
id: int
name: str
description: str
datetime: str
groups: list[str]
approved: bool
category: str
organization: str
@router.get("/")
async def get_all_items(db: Session = Depends(get_db)):
db_items = db.query(Event).all()
if db_items is None:
raise HTTPException(status_code=404, detail="No events found")
return db_items
@router.get("/{event_id}")
async def read_item(item_id: int, db: Session = Depends(get_db)):
db_item = db.query(Event).filter(Event.id == item_id).first()
if db_item is None:
raise HTTPException(status_code=404, detail="Item not found")
return db_item
@router.post("/", response_model=ItemResponse)
async def create_event(params: ItemCreate, db: Session = Depends(get_db)):
try:
db_item = Event(**params.model_dump())
db.add(db_item)
db.commit()
db.refresh(db_item)
return db_item
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))