Compare commits
No commits in common. "f259aa3993c78470cfde44cd67c40f221037ba36" and "dfccda02b835e7e1c32632c4cbf4dc505c63eec0" have entirely different histories.
f259aa3993
...
dfccda02b8
@ -2,13 +2,13 @@ from flask import Flask
|
|||||||
from flask_jwt_extended import JWTManager
|
from flask_jwt_extended import JWTManager
|
||||||
from flask_mail import Mail
|
from flask_mail import Mail
|
||||||
from flasgger import Swagger
|
from flasgger import Swagger
|
||||||
|
from flask_cors import CORS
|
||||||
from app.doc.main_swag import main_swagger
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
jwt_manager = JWTManager(app)
|
jwt_manager = JWTManager(app)
|
||||||
mail = Mail(app)
|
mail = Mail(app)
|
||||||
swag = Swagger(app, template=main_swagger)
|
swag = Swagger(app)
|
||||||
|
cors = CORS(app)
|
||||||
|
|
||||||
def create_app():
|
def create_app():
|
||||||
from app.api import bp, bp_errors, bp_product, bp_user, bp_cart
|
from app.api import bp, bp_errors, bp_product, bp_user, bp_cart
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
from flask import jsonify, abort, request
|
from flask import jsonify, abort, request
|
||||||
from flask_jwt_extended import jwt_required, get_jwt_identity
|
from flask_jwt_extended import jwt_required, get_jwt_identity
|
||||||
|
|
||||||
from app.doc.cart_swag import show_cart_swagger, add_to_cart_swagger
|
from app.doc.cart import show_cart_swagger, add_to_cart_swagger
|
||||||
|
|
||||||
from flasgger import swag_from
|
from flasgger import swag_from
|
||||||
|
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
from flask import jsonify, abort
|
from flask import jsonify, abort
|
||||||
from flasgger import swag_from
|
|
||||||
|
|
||||||
from app.doc.root_swag import root_swagger
|
from app.doc.main import main_swagger
|
||||||
|
|
||||||
|
from flasgger import swag_from
|
||||||
|
|
||||||
from app.api import bp
|
from app.api import bp
|
||||||
|
|
||||||
@bp.route('/')
|
@bp.route('/')
|
||||||
@swag_from(root_swagger)
|
@swag_from(main_swagger)
|
||||||
def hello():
|
def hello():
|
||||||
return jsonify({'message': 'Hello, Flask!'})
|
return jsonify({'message': 'Hello, Flask!'})
|
@ -1,7 +1,7 @@
|
|||||||
from flask import jsonify, abort, request
|
from flask import jsonify, abort, request
|
||||||
from flask_jwt_extended import jwt_required, get_jwt_identity
|
from flask_jwt_extended import jwt_required, get_jwt_identity
|
||||||
|
|
||||||
from app.doc.product_swag import get_products_swagger
|
from app.doc.product import get_products_swagger
|
||||||
|
|
||||||
from flasgger import swag_from
|
from flasgger import swag_from
|
||||||
|
|
||||||
|
@ -2,10 +2,6 @@ from app.api import bp_user
|
|||||||
from flask_jwt_extended import jwt_required, get_jwt_identity, get_jwt
|
from flask_jwt_extended import jwt_required, get_jwt_identity, get_jwt
|
||||||
from flask import request, abort
|
from flask import request, abort
|
||||||
|
|
||||||
from flasgger import swag_from
|
|
||||||
|
|
||||||
from app.doc.user_swag import login_swagger
|
|
||||||
|
|
||||||
from app.services.user_service import UserService
|
from app.services.user_service import UserService
|
||||||
|
|
||||||
@bp_user.route('/register', methods=['POST'])
|
@bp_user.route('/register', methods=['POST'])
|
||||||
@ -23,7 +19,6 @@ def register():
|
|||||||
return result, status_code
|
return result, status_code
|
||||||
|
|
||||||
@bp_user.route('/login', methods=['POST'])
|
@bp_user.route('/login', methods=['POST'])
|
||||||
@swag_from(login_swagger)
|
|
||||||
def login():
|
def login():
|
||||||
username = request.json.get('username')
|
username = request.json.get('username')
|
||||||
password = request.json.get('password')
|
password = request.json.get('password')
|
||||||
|
@ -1,8 +1,13 @@
|
|||||||
show_cart_swagger = {
|
show_cart_swagger = {
|
||||||
"tags": ["Cart"],
|
"tags": ["Cart"],
|
||||||
"security":
|
"parameters":
|
||||||
[
|
[
|
||||||
{"JWT": []}
|
{
|
||||||
|
"name": "Authorization",
|
||||||
|
"in": "header",
|
||||||
|
"type": "string",
|
||||||
|
"required": True
|
||||||
|
}
|
||||||
],
|
],
|
||||||
"responses":
|
"responses":
|
||||||
{
|
{
|
||||||
@ -40,25 +45,24 @@ show_cart_swagger = {
|
|||||||
|
|
||||||
add_to_cart_swagger ={
|
add_to_cart_swagger ={
|
||||||
"tags": ["Cart"],
|
"tags": ["Cart"],
|
||||||
"security":
|
|
||||||
[
|
|
||||||
{"JWT": []}
|
|
||||||
],
|
|
||||||
"parameters":
|
"parameters":
|
||||||
[
|
[
|
||||||
|
{
|
||||||
|
"name": "Authorization",
|
||||||
|
"in": "header",
|
||||||
|
"type": "string",
|
||||||
|
"required": True
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "product_id",
|
"name": "product_id",
|
||||||
"description": "ID of product to add to cart.",
|
|
||||||
"in": "path",
|
"in": "path",
|
||||||
"type": "int",
|
"type": "int",
|
||||||
|
"required": True
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "count",
|
"name": "count",
|
||||||
"description": "Count of the products. If not provided, defaults to 1",
|
|
||||||
"in": "query",
|
"in": "query",
|
||||||
"type": "int",
|
"type": "int",
|
||||||
"default": 1,
|
|
||||||
"minimum": 1,
|
|
||||||
"required": False
|
"required": False
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@ -70,7 +74,7 @@ add_to_cart_swagger ={
|
|||||||
},
|
},
|
||||||
"400":
|
"400":
|
||||||
{
|
{
|
||||||
"description": "Causes:\n- Count is < 1"
|
"description": "Incorrect usage. For example id of product not found or product count < 1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,5 +1,8 @@
|
|||||||
root_swagger = {
|
main_swagger = {
|
||||||
"methods": ["GET"],
|
"methods": ["GET"],
|
||||||
|
"parameters": [
|
||||||
|
|
||||||
|
],
|
||||||
"responses":
|
"responses":
|
||||||
{
|
{
|
||||||
"200":
|
"200":
|
||||||
@ -8,8 +11,7 @@ root_swagger = {
|
|||||||
"schema":
|
"schema":
|
||||||
{
|
{
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties":
|
"properties": {
|
||||||
{
|
|
||||||
"message": {"type": "string", "example": "Hello, Flask!"}
|
"message": {"type": "string", "example": "Hello, Flask!"}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,18 +0,0 @@
|
|||||||
main_swagger = {
|
|
||||||
"info": {
|
|
||||||
"title": "Shop API",
|
|
||||||
"version": "0.1",
|
|
||||||
"description": "Simple shop API using flask and co.\nFeatures include:\n- Not working\n- Successful registration of users\n- Adding items to cart\n- I don't know",
|
|
||||||
},
|
|
||||||
"host": "localhost:1236",
|
|
||||||
"schemes": "http",
|
|
||||||
"securityDefinitions": {
|
|
||||||
"JWT": {
|
|
||||||
"type": "apiKey",
|
|
||||||
"scheme": "bearer",
|
|
||||||
"name": "Authorization",
|
|
||||||
"in": "header",
|
|
||||||
"description": "JWT Authorization header using the Bearer scheme.\n*Make sure to prefix the token with **Bearer**!*"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
23
app/doc/product.py
Normal file
23
app/doc/product.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
get_products_swagger = {
|
||||||
|
"paths": {
|
||||||
|
"/get": {
|
||||||
|
"get": {
|
||||||
|
"summary": "Get products",
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "Successfully retrieved products",
|
||||||
|
"schema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"products": {"type": "array", "items": {"type": "object", "properties": {"id": {"type": "int"}, "name": {"type": "string"}, "price": {"type": "float"}}}}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"400": {
|
||||||
|
"description": "Bad request"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,21 +0,0 @@
|
|||||||
get_products_swagger = {
|
|
||||||
"methods": ["GET"],
|
|
||||||
"tags": ["Products"],
|
|
||||||
"parameters": [
|
|
||||||
|
|
||||||
],
|
|
||||||
"responses":
|
|
||||||
{
|
|
||||||
"200":
|
|
||||||
{
|
|
||||||
"description": "Get a page of products",
|
|
||||||
"schema":
|
|
||||||
{
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"message": {"type": "string", "example": "Hello, Flask!"}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
0
app/doc/user.py
Normal file
0
app/doc/user.py
Normal file
@ -1,45 +0,0 @@
|
|||||||
login_swagger = {
|
|
||||||
"methods": ["POST"],
|
|
||||||
"tags": ["User"],
|
|
||||||
"description": "Logs in using username and password and returns a JWT token for further authorization of requests.\n**The token is valid for 1 hour**",
|
|
||||||
"parameters":
|
|
||||||
[
|
|
||||||
{
|
|
||||||
"in": "body",
|
|
||||||
"name": "body",
|
|
||||||
"description": "Username and password payload",
|
|
||||||
"required": True,
|
|
||||||
"schema":
|
|
||||||
{
|
|
||||||
"type": "object",
|
|
||||||
"properties":
|
|
||||||
{
|
|
||||||
"username": {"type": "string", "example": "mycoolusername"},
|
|
||||||
"password": {"type": "string", "example": "MyStrongPassword123"}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"responses":
|
|
||||||
{
|
|
||||||
"200":
|
|
||||||
{
|
|
||||||
"description": "Returns a fresh token",
|
|
||||||
"schema":
|
|
||||||
{
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"token": {"type": "string", "example": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6ZmFsc2UsImlhdCI6MTcxMDMyMjkyOCwianRpIjoiZDFhYzQxZDktZjA4NC00MmYzLThlMWUtZWFmZjJiNGU1MDAyIiwidHlwZSI6ImFjY2VzcyIsInN1YiI6MjMwMDEsIm5iZiI6MTcxMDMyMjkyOCwiZXhwIjoxNzEwMzI2NTI4fQ.SW7LAi1j5vDOEIvzeN-sy0eHPP9PFJFkXYY029O35w0"}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"400":
|
|
||||||
{
|
|
||||||
"description": "Possible causes:\n- Missing username or password from request.\n- Nonexistent username"
|
|
||||||
},
|
|
||||||
"401":
|
|
||||||
{
|
|
||||||
"description": "Password is incorrect"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -23,18 +23,17 @@ class CartService:
|
|||||||
cursor.execute("select count from cart_item where cart_id = %s and product_id = %s", (user_id, product_id))
|
cursor.execute("select count from cart_item where cart_id = %s and product_id = %s", (user_id, product_id))
|
||||||
result = cursor.fetchone()
|
result = cursor.fetchone()
|
||||||
|
|
||||||
if cursor.rowcount == 1:
|
if cursor.rowcount != 0:
|
||||||
cursor.execute("update cart_item set count = count + %s where cart_id = %s and product_id = %s", (count, user_id, product_id))
|
cursor.execute("update cart_item set count = count + %s where cart_id = %s and product_id = %s", (count, user_id, product_id))
|
||||||
else:
|
else:
|
||||||
cursor.execute("insert into cart_item(cart_id, product_id, count) values (%s, %s, %s)", (user_id, product_id, count))
|
cursor.execute("insert into cart_item(cart_id, product_id, count) values (%s, %s, %s)", (user_id, product_id, count))
|
||||||
|
|
||||||
db_connection.commit()
|
db_connection.commit()
|
||||||
|
|
||||||
|
return {"Success": "Successfully added to cart"}, 200
|
||||||
|
|
||||||
except Error as e:
|
except Error as e:
|
||||||
return {"Failed": f"Failed to add item to cart. Reason: {e}"}, 500
|
return {"Failed": f"Failed to add item to cart. Reason: {e}"}, 400
|
||||||
|
|
||||||
return {"Success": "Successfully added to cart"}, 200
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def update_count(user_id: str, product_id: int, count: int) -> Tuple[Union[dict, str], int]:
|
def update_count(user_id: str, product_id: int, count: int) -> Tuple[Union[dict, str], int]:
|
||||||
|
@ -3,8 +3,7 @@ gunicorn==20.1.0
|
|||||||
mysql-connector-python==8.3.0
|
mysql-connector-python==8.3.0
|
||||||
python-dotenv==1.0.1
|
python-dotenv==1.0.1
|
||||||
Flask-JWT-Extended==4.5.3
|
Flask-JWT-Extended==4.5.3
|
||||||
flasgger==0.9.7.1
|
|
||||||
Flask-Mail==0.9.1
|
|
||||||
PyJWT==2.8.0
|
PyJWT==2.8.0
|
||||||
|
Flask-Mail==0.9.1
|
||||||
redis==4.5.4
|
redis==4.5.4
|
||||||
bcrypt==4.1.2
|
bcrypt==4.1.2
|
Loading…
x
Reference in New Issue
Block a user