-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
65 lines (55 loc) · 2.01 KB
/
main.py
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
from typing import Union
from fastapi import FastAPI, Depends,HTTPException,status
from fastapi.middleware.cors import CORSMiddleware
from sqlalchemy.orm import Session
from database import get_db
from models import Event
from schemas import EventSchema
# INITIATE IT
app = FastAPI()
origins = ["http://localhost:3000"]
app.add_middleware(CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# define route
@app.get('/')
def index():
return {"message": "Welcome to my first API"}
# Return a list of events (you can add sample data)
@app.get('/events')
def events(db: Session = Depends(get_db)):
events =db.query(Event).all()
return events
# Include the event_id parameter in the function
@app.get('/events/{event_id}')
def event(event_id: int, db: Session = Depends(get_db)):
event = db .query(Event).filter(Event.id ==event_id).first()
return event
@app.post('/events')
def create_event(event:EventSchema,db: Session = Depends(get_db) ):
#unpacks a dict and passes it as key value pairs
new_event =Event(**event.model_dumpI())
#adds the events to the transaction
db.add(new_event)
#commit the transaction
db.commit()
#get event from the db again
db.refresh(new_event)
return {"message": "Event created successfully","event": new_event}
@app.patch('/events/{event_id}')
def update_event(event_id: int):
return {"message": f"Event {event_id} updated successfully"}
@app.delete('/events/{event_id}')
def delete_event(event_id: int, db:Session=Depends(get_db)):
delete_event=db.query(Event).filter(Event.id ==event_id).first()
if delete_event==None:
raise HttpException(status_code=status.HTTP_404_NOT_FOUND,
detail=f"Event{event_id}does not exist")
else:
delete_event.delete()
#running transactions with db
db.commit()
return response(status_code=status.HTTP_204_NO_CONTENT)