Started adding database logic. Can now add book to shelf and get list of books that are checked in.
This commit is contained in:
37
index.html
37
index.html
@@ -1,5 +1,34 @@
|
|||||||
<form action="http://127.0.0.1:8000/lookup" method="POST">
|
<div>
|
||||||
<label for="isbn"> </label>
|
<b>Local Testing</b>
|
||||||
<input name="isbn" id="isbn" value=" ">
|
<br>
|
||||||
|
<form action="http://127.0.0.1:8000/lookup" method="POST">
|
||||||
|
<label for="isbn">ISBN Title Lookup<br></label>
|
||||||
|
<input name="isbn" id="isbn" value=" ">
|
||||||
|
<button>Submit</button>
|
||||||
|
</form>
|
||||||
|
<form action="http://127.0.0.1:8000/checkin" method="POST">
|
||||||
|
<label for="isbn">Add Book<br></label>
|
||||||
|
<input name="isbn" id="isbn" value=" ">
|
||||||
<button>Submit</button>
|
<button>Submit</button>
|
||||||
</form>
|
</form>
|
||||||
|
<form action="http://127.0.0.1:8000/getbooks/1" method="GET">
|
||||||
|
<button>Get Books</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<b>Remote Testing</b>
|
||||||
|
<form action="http://library.dandhost.com/lookup" method="POST">
|
||||||
|
<label for="isbn">ISBN Title Lookup<br></label>
|
||||||
|
<input name="isbn" id="isbn" value=" ">
|
||||||
|
<button>Submit</button>
|
||||||
|
</form>
|
||||||
|
<form action="http://library.dandhost.com/checkin" method="POST">
|
||||||
|
<label for="isbn">Add Book<br></label>
|
||||||
|
<input name="isbn" id="isbn" value=" ">
|
||||||
|
<button>Submit</button>
|
||||||
|
</form>
|
||||||
|
<form action="http://library.dandhost.com/getbooks/1" method="GET">
|
||||||
|
<button>Get Books</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
63
main.py
63
main.py
@@ -1,6 +1,38 @@
|
|||||||
from fastapi import FastAPI, Form
|
from fastapi import FastAPI, Form
|
||||||
from isbnlib import meta
|
from isbnlib import meta
|
||||||
|
from sqlalchemy import create_engine, Column, Integer, String, TEXT
|
||||||
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
|
# Declare Base and setup tables
|
||||||
|
Base = declarative_base()
|
||||||
|
|
||||||
|
|
||||||
|
class User(Base):
|
||||||
|
__tablename__ = 'User'
|
||||||
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
|
Username = Column(String(50))
|
||||||
|
FirstName = Column(String(50))
|
||||||
|
LastName = Column(String(50))
|
||||||
|
Email = Column(String(50))
|
||||||
|
|
||||||
|
|
||||||
|
class Book(Base):
|
||||||
|
__tablename__ = 'Book'
|
||||||
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
|
userid = Column(Integer)
|
||||||
|
Title = Column(TEXT)
|
||||||
|
Author = Column(TEXT)
|
||||||
|
|
||||||
|
|
||||||
|
# Connect to database and establish a session
|
||||||
|
engine = create_engine('mysql+pymysql://library:jdSCAd6KEqfbpzh9NT4x@db.dandembinski.com:3306/Library')
|
||||||
|
engine.connect()
|
||||||
|
|
||||||
|
Session = sessionmaker(bind=engine)
|
||||||
|
session = Session()
|
||||||
|
|
||||||
|
# Initialize FastAPI and setup routes
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
@@ -9,17 +41,36 @@ async def root():
|
|||||||
return {"message": "Hello World"}
|
return {"message": "Hello World"}
|
||||||
|
|
||||||
|
|
||||||
@app.post('/checkin/{isbn}')
|
@app.post('/checkin/')
|
||||||
async def checkin(isbn):
|
async def checkin(isbn: str = Form(...)):
|
||||||
return{"Checked in": isbn}
|
info = await getMeta(isbn)
|
||||||
|
session.add(Book(userid=1, Title=info['Title'], Author=info['Authors']))
|
||||||
|
session.commit()
|
||||||
|
return {"Checked in": info['Title']}
|
||||||
|
|
||||||
|
|
||||||
@app.get('/checkout/{isbn}')
|
@app.get('/checkout/{isbn}')
|
||||||
async def checkout(isbn):
|
async def checkout(isbn):
|
||||||
return{"Checked Out": isbn}
|
return {"Checked Out": isbn}
|
||||||
|
|
||||||
|
|
||||||
|
@app.get('/getbooks/{userid}')
|
||||||
|
async def getbooks(userid):
|
||||||
|
books = []
|
||||||
|
result = session.query(Book).filter(Book.userid == userid)
|
||||||
|
|
||||||
|
for each in result:
|
||||||
|
books.append(each)
|
||||||
|
|
||||||
|
return books
|
||||||
|
|
||||||
|
|
||||||
@app.post('/lookup')
|
@app.post('/lookup')
|
||||||
async def lookup(isbn: str = Form(...)):
|
async def lookup(isbn: str = Form(...)):
|
||||||
|
info = await getMeta(isbn)
|
||||||
|
return {"Title": info['Title']}
|
||||||
|
|
||||||
|
|
||||||
|
async def getMeta(isbn):
|
||||||
info = meta(isbn, service='wiki')
|
info = meta(isbn, service='wiki')
|
||||||
return{"Title": info['Title']}
|
return info
|
||||||
|
|||||||
@@ -2,15 +2,18 @@ asgiref==3.3.4
|
|||||||
click==8.0.1
|
click==8.0.1
|
||||||
colorama==0.4.4
|
colorama==0.4.4
|
||||||
fastapi==0.65.1
|
fastapi==0.65.1
|
||||||
|
greenlet==1.1.0
|
||||||
gunicorn==20.1.0
|
gunicorn==20.1.0
|
||||||
h11==0.12.0
|
h11==0.12.0
|
||||||
httptools==0.2.0
|
httptools==0.2.0
|
||||||
isbnlib==3.10.8
|
isbnlib==3.10.8
|
||||||
pydantic==1.8.2
|
pydantic==1.8.2
|
||||||
|
PyMySQL==1.0.2
|
||||||
python-dotenv==0.17.1
|
python-dotenv==0.17.1
|
||||||
python-multipart==0.0.5
|
python-multipart==0.0.5
|
||||||
PyYAML==5.4.1
|
PyYAML==5.4.1
|
||||||
six==1.16.0
|
six==1.16.0
|
||||||
|
SQLAlchemy==1.4.17
|
||||||
starlette==0.14.2
|
starlette==0.14.2
|
||||||
typing-extensions==3.10.0.0
|
typing-extensions==3.10.0.0
|
||||||
uvicorn==0.14.0
|
uvicorn==0.14.0
|
||||||
|
|||||||
Reference in New Issue
Block a user