-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
70 lines (53 loc) · 2.01 KB
/
Copy pathmain.py
File metadata and controls
70 lines (53 loc) · 2.01 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
import uvicorn
from fastapi import FastAPI, HTTPException, Depends
from schemas import Motorbike, MotorbikeResponse
import models
from database import engine, SessionLocal
models.Base.metadata.create_all(bind=engine)
from sqlalchemy.orm import Session
import crud
from typing import List
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.get("/status")
async def status():
return {"status": "operational", "version": "1.0.0"}
@app.post("/motorbikes")
async def create_motorbike(motorbike: Motorbike, db: Session = Depends(get_db)):
return crud.create_motorbike(db, motorbike)
@app.get("/motorbikes", response_model=List[MotorbikeResponse])
async def get_motorbikes(db: Session = Depends(get_db)):
return crud.get_motorbikes(db)
@app.get("/motorbikes/{motorbike_id}", response_model=MotorbikeResponse)
async def get_motorbike(motorbike_id: int, db: Session = Depends(get_db)):
if not crud.get_motorbike(db, motorbike_id):
raise HTTPException(status_code=404, detail="Motorbike not found")
return crud.get_motorbike(db, motorbike_id)
@app.put("/motorbikes/{motorbike_id}")
async def put_motorbike(
motorbike: Motorbike,
motorbike_id: int,
db: Session = Depends(get_db)
):
db_motorbike = crud.get_motorbike(db, motorbike_id)
if not db_motorbike:
raise HTTPException(status_code=404, detail="Motorbike not found")
return crud.update_motorbike(db, db_motorbike, motorbike)
@app.delete("/motorbikes/{motorbike_id}")
async def delete_motorbike(motorbike_id: int, db: Session = Depends(get_db)):
db_motorbike = crud.get_motorbike(db, motorbike_id)
if not db_motorbike:
raise HTTPException(status_code=404, detail="Motorbike not found")
crud.delete_motorbike(db, db_motorbike)
return {"message": "Motorbike deleted"}
if __name__ == "__main__":
import uvicorn
uvicorn.run("main:app", host="localhost", port=8000)