Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and fix borrower API #50

Merged
merged 1 commit into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified HeliumDB.db
Binary file not shown.
25 changes: 25 additions & 0 deletions borrower.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import sqlite3 as sql
from flask import request
from flask_restful import Resource

from consts import DB_FILE, SEARCH_PAGE_SIZE, CHECKOUT_LIMIT

class BorrowerCreate(Resource):

def post(self):
args = {'ssn':request.args.get('ssn', ''), 'bname':request.args.get('name', ''), 'address':request.args.get('address', ''), 'phone':request.args.get('phone', 'NULL')}

with sql.connect(DB_FILE) as conn:
conn.row_factory = sql.Row
c = conn.cursor()

# Check for existing SSN and return useful error if it already exists
if c.execute("SELECT * FROM BORROWER WHERE Ssn = :ssn", args).fetchone():
return {"message": "Only one borrower card per person. Ssn must be unique."}, 409

# Create borrower
command = "INSERT INTO BORROWER (Ssn, Bname, Address, Phone) VALUES (:ssn, :bname, :address, :phone);"
c.execute(command, args)

card_id = c.execute("SELECT Card_id FROM BORROWER WHERE Ssn = :ssn", args).fetchone()["Card_id"]
return {"message": "Borrower Successfully Created. Card ID: " + 'ID{:0>6}'.format(str(card_id)) + ".", "card_id_str": 'ID{:0>6}'.format(str(card_id)), "card_id_num": card_id}, 200
24 changes: 3 additions & 21 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from consts import DB_FILE
from book import Search, Checkout, Checkin
from fines import FinesAll, FinesUpdate, FinesPayment
from borrower import BorrowerCreate

app = Flask(__name__)
api = Api(app)
Expand All @@ -24,27 +25,6 @@ def style(path):
def scripts(path):
return send_from_directory('./public/scripts', path)

# @TODO: Move to Own File
@app.route('/borrower/create', methods=['POST'])
def create_borrower():

args = {'ssn':request.args.get('ssn', ''), 'bname':request.args.get('bname', ''), 'address':request.args.get('address', ''), 'phone':request.args.get('phone', 'NULL')}

with sql.connect(DB_FILE) as conn:
conn.row_factory = sql.Row
c = conn.cursor()

# Check for existing SSN and return useful error if it already exists
if c.execute("SELECT * FROM BORROWER WHERE Ssn = :ssn", args).fetchone():
return {"message": "Only one borrower card per person. Ssn must be unique."}, 409

# Create borrower
command = "INSERT INTO BORROWER (Ssn, Bname, Address, Phone) VALUES (:ssn, :bname, :address, :phone);"
c.execute(command, args)

card_id = c.execute("SELECT Card_id FROM BORROWER WHERE Ssn = :ssn", args).fetchone()["Card_id"]
return {"message": "Borrower Successfully Created. Card ID: " + 'ID{:0>6}'.format(str(card_id)) + ".", "card_id_str": 'ID{:0>6}'.format(str(card_id)), "card_id_num": card_id}, 200


# Endpoints
api.add_resource(Search, '/book/search', endpoint='search')
Expand All @@ -59,6 +39,8 @@ def create_borrower():

api.add_resource(FinesPayment, '/fines/payment', endpoint='fines_payment')

api.add_resource(BorrowerCreate, '/borrower/create', endpoint='borrower_create')

if __name__ == '__main__':

# DB reset command line argument
Expand Down