-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
60 lines (42 loc) · 1.42 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
import uvicorn
from fastapi import FastAPI, Depends
from fastapi_sqlalchemy import DBSessionMiddleware, db
from schema import Book as SchemaBook
from schema import Author as SchemaAuthor
from schema import Book
from schema import Author
from models import Book as ModelBook
from models import Author as ModelAuthor
from auth import JWTBearer
import os
from dotenv import load_dotenv
load_dotenv('.env')
app = FastAPI()
# to avoid csrftokenError
app.add_middleware(DBSessionMiddleware, db_url=os.environ['DATABASE_URL'])
@app.get("/")
async def root():
return {"message": "hello world"}
@app.post('/book/',dependencies=[Depends(JWTBearer())], response_model=SchemaBook)
async def book(book: SchemaBook):
db_book = ModelBook(title=book.title, rating=book.rating, author_id = book.author_id)
db.session.add(db_book)
db.session.commit()
return db_book
@app.get('/book/')
async def book():
book = db.session.query(ModelBook).all()
return book
@app.post('/author/', dependencies=[Depends(JWTBearer())], response_model=SchemaAuthor)
async def author(author:SchemaAuthor):
db_author = ModelAuthor(name=author.name, age=author.age)
db.session.add(db_author)
db.session.commit()
return db_author
@app.get('/author/')
async def author():
author = db.session.query(ModelAuthor).all()
return author
# To run locally
if __name__ == '__main__':
uvicorn.run(app, host='0.0.0.0', port=8000)