Skip to content
Snippets Groups Projects
Commit eca07e4b authored by Chude, Chiamaka A (PG/T - Comp Sci & Elec Eng)'s avatar Chude, Chiamaka A (PG/T - Comp Sci & Elec Eng)
Browse files

First commit for product microservice. It consists of 3 pages and 4 functions:...

First commit for product microservice. It consists of 3 pages and 4 functions: The firt function displays products by category on the home page. The second function displays a product on another page after it has been clicked on. It displays the product name, price, image, description and reviews and the product id is sent through a get request. The third function communicates with the user microservice to get a users username when they want to add a product. And the fourth function inserts the product reviews and username gotten from the user microservice into the product reviews table
parent c2e7406b
No related branches found
No related tags found
1 merge request!2First commit for product microservice. It consists of 3 pages and 4 functions:...
Showing
with 230 additions and 0 deletions
runtime: python312
entrypoint: gunicorn -b :$PORT index:app
\ No newline at end of file
from flask import Flask
from app.models import login
app = Flask(__name__)
from app import routes
if __name__ == '__main__':
app.run(debug=True)
\ No newline at end of file
File added
import os
DEBUG = True
SECRET_KEY = "Group3"
PORT = 5001
\ No newline at end of file
from flask import Flask
from flask import Blueprint
File added
File added
File added
File added
File added
File added
from flask import Blueprint, jsonify, request, json, session
from models.addReview import add_user_review
import requests
add_review_bp = Blueprint("addReview",__name__)
@add_review_bp.route("/product/<int:productID>/addReview", methods=["GET"])
def get_username_from_user_microservice(productID):
user_id = session.get("user_id")
if user_id:
if request.method == 'GET':
product_id = productID
response = requests.post('http://localhost:5000/user/getUsername', json={'id': user_id})
if response.status_code == 200:
username = response.json()['username']
userID = user_id
session['username'] = username
session['productID'] = product_id
rating_info = {
"UserID" : userID,
"ProductID" : product_id,
"Username" : username
}
return rating_info
else:
return {"Error" : "Failed to retrieve username"}
#return "You can review the product with ID: {}".format(product_id)
else:
return {"error" : "null"}
else:
return {"error" : "You need to be logged in to add a review"}
@add_review_bp.route("/product/<int:productID>/addReview", methods=["POST"])
def add_review(productID):
user_id = session.get("user_id")
if user_id:
if request.method == 'POST':
data = request.get_json()
review = data.get("review")
rating = data.get("rating")
product_id = productID
username = session.get('username')
product_id = session.get('productID')
if review.strip() != "":
if username is None:
return {"error": "Username is not available"}
# Check if product_id is available
if product_id is None:
return {"error": "Product ID is not available"}
if isinstance(rating, int) and 1 <= rating <= 5:
review_info = {
"UserID" : user_id,
"ProductID" : product_id,
"Review" : review,
"Rating" : rating,
"Username" : username
}
user_review_message = add_user_review(review_info)
return user_review_message
else:
return {"error" : "Rating must be an integer between 1 and 5"}
else:
return {"error" : "Review cannot be empty"}
else:
return {"error" : "null"}
else:
return {"error" : "You need to be logged in to add a review"}
\ No newline at end of file
from flask import Blueprint, jsonify, request, json, session
from models.getProduct import get_product
display_product_bp = Blueprint("product",__name__)
@display_product_bp.route("/product/<int:productID>", methods=["GET"])
def display_product(productID):
user_id = session.get("user_id")
if request.method == 'GET':
product_id = productID
# Convert to JSON
#json_user_data = json.dumps(user_data)
product, images, reviews = get_product(product_id) #Send user info to database
customers = [review_data["CustomerID"] for review_data in reviews]
return jsonify(customers,{"product" : product, "images" : images, "reviews" : reviews, "session" : user_id})
else:
return {"error" : "null"}
\ No newline at end of file
from flask import Blueprint, jsonify, request, json, session
from models.productHome import get_product_by_section
product_home_bp = Blueprint("home",__name__)
@product_home_bp.route("/product/home", methods=["POST"])
def product_section():
user_id = session.get("user_id")
if request.method == 'POST':
data = request.get_json()
category_id = data.get("category_id")
# Convert to JSON
#json_user_data = json.dumps(user_data)
products = get_product_by_section(category_id) #Send user info to database
#customers = [review_data["CustomerID"] for review_data in reviews]
return jsonify({"products" : products, "session" : user_id})
else:
return {"error" : "null"}
\ No newline at end of file
from flask import Flask, redirect, url_for, request, render_template, make_response, session, abort
from flask_cors import CORS
from flask import jsonify
from config import DEBUG, SECRET_KEY, PORT
import os
import requests
from controllers.getProductController import display_product_bp
from controllers.addReviewController import add_review_bp
from controllers.productHomeController import product_home_bp
app = Flask(__name__)
CORS(app)
app.secret_key = SECRET_KEY
# Read user microservice URL from environment variable
USER_MICROSERVICE_URL = os.getenv('USER_MICROSERVICE_URL', 'http://127.0.0.1:5000')
@app.route('/product', methods=['POST'])
def get_session_id():
session_id = session.get('user_id')
if session_id:
return jsonify({'session_id': session_id})
else:
return jsonify({'message': 'Session ID not found'})
@app.route('/')
def index():
return render_template("index.html")
app.register_blueprint(display_product_bp)
app.register_blueprint(add_review_bp)
app.register_blueprint(product_home_bp)
if __name__ == '__main__':
app.run(debug=DEBUG, port=PORT)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment