Initial commit

This commit is contained in:
Thastertyn committed 2024-03-05 16:02:13 +01:00
commit b4ecbeaa37
43 files changed
+494

No files matched your search

+8
View File
@@ -0,0 +1,8 @@
from flask import Blueprint
bp_errors = Blueprint('errors', __name__)
bp = Blueprint('api', __name__)
bp_product = Blueprint('product', __name__, url_prefix="/product")
bp_user = Blueprint('user', __name__, url_prefix="/user")
from . import routes
Binary file not shown.
Binary file not shown.
+1
View File
@@ -0,0 +1 @@
from . import main_routes,error_routes, product_routes, user_routes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+29
View File
@@ -0,0 +1,29 @@
from .. import bp_errors
@bp_errors.app_errorhandler(400)
def bad_request(e):
return {"Bad Request": "The request was incorrectly formatted, or contained invalid data"}, 400
@bp_errors.app_errorhandler(401)
def unauthorized(e):
return {"Unauthorized": "Failed to authorize the request"}, 401
@bp_errors.app_errorhandler(403)
def forbidden(e):
return {"Forbidden": "Forbidden from accessing this resource. Try logging in"}, 403
@bp_errors.app_errorhandler(404)
def not_found(e):
return {"Not Found": "The requested resource was not found"}, 404
@bp_errors.app_errorhandler(405)
def method_not_allowed(e):
return {"Method Not Allowed": "The method used is not allowed in current context"}, 405
@bp_errors.app_errorhandler(500)
def internal_error(e):
return {"Internal Server Error": "An error occured on he server"}, 500
@bp_errors.app_errorhandler(501)
def internal_error(e):
return {"Not Implemented": "This function has not been implemented yet. Check back soon!"}, 501
+7
View File
@@ -0,0 +1,7 @@
from flask import jsonify, abort
from .. import bp
@bp.route('/')
def hello():
return jsonify({'message': 'Hello, Flask!'})
+63
View File
@@ -0,0 +1,63 @@
from flask import jsonify, abort, request
from app.api import bp_product
from app.services.product_service import ProductService
@bp_product.route('/<int:id>', methods=['GET'])
def all_product_info(id: int):
result = ProductService.get_all_info(id)
if result is not None:
return jsonify(result)
else:
abort(404)
@bp_product.route('/<int:id>/name', methods=['GET'])
def get_name(id: int):
result = ProductService.get_name(id)
if result is not None:
return jsonify({"name": result})
else:
return abort(404)
@bp_product.route('/<int:id>/manufacturer', methods=['GET'])
def get_manufacturer(id: int):
result = ProductService.get_manufacturer(id)
if result is not None:
return jsonify({"name": result})
else:
return abort(404)
@bp_product.route('/<int:id>/price', methods=['GET'])
def get_price(id: int):
result = ProductService.get_price(id)
if result is not None:
return jsonify({"price": result})
else:
return abort(404)
@bp_product.route('/<int:id>/image', methods=['GET'])
def get_image(id: int):
result = ProductService.get_image(id)
if result is not None:
return jsonify({"image": result})
else:
return abort(404)
@bp_product.route('/<int:id>/image_name', methods=['GET'])
def get_image_name(id: int):
result = ProductService.get_image_name(id)
if result is not None:
return jsonify({"image_name": result})
else:
return abort(404)
@bp_product.route('/create', methods=['POST'])
def create_product_listing():
return abort(501)
+79
View File
@@ -0,0 +1,79 @@
from app.api import bp_user
from flask_jwt_extended import jwt_required, get_jwt_identity, get_jwt
from flask import request, abort, jsonify
from datetime import timedelta
from app.services.user_service import UserService
from app.extensions import jwt_redis_blocklist
@bp_user.route('/login', methods=['POST'])
def login():
username = request.json.get('username')
password = request.json.get('password')
if username is None or password is None:
return abort(400)
result, status_code = UserService.login(username, password)
return jsonify(**result), status_code
@bp_user.route('/logout', methods=['DELETE'])
@jwt_required()
def logout():
jti = get_jwt()["jti"]
jwt_redis_blocklist.set(jti, "", ex=timedelta(days=1))
return {"Success": "Successfully logged out"}, 200
@bp_user.route('/create', methods=['POST'])
def create_user():
username = request.json.get('username')
email = request.json.get('email')
password = request.json.get('password')
if username is None or email is None or password is None:
return abort(400)
result, status_code = UserService.create_user(username, email, password)
return jsonify(**result), status_code
@bp_user.route('/update/email', methods=['POST'])
@jwt_required()
def update_email():
username = get_jwt_identity()
new_mail = request.json.get('new_email')
if new_mail is None:
return abort(400)
result, status_code = UserService.update_email(username, new_mail)
return jsonify(**result), status_code
@bp_user.route('/update/username', methods=['POST'])
@jwt_required()
def update_username():
username = get_jwt_identity()
new_username = request.json.get('new_username')
if new_username is None:
return abort(400)
result, status_code = UserService.update_username(username, new_username)
return jsonify(**result), status_code
@bp_user.route('/update/password', methods=['POST'])
@jwt_required()
def update_password():
username = get_jwt_identity()
new_password = request.json.get('new_password')
if new_password is None:
return abort(400)
result, status_code = UserService.update_password(username, new_password)
return jsonify(**result), status_code