import crud from flask import jsonify, request from helpers import init_db_connection, CORS_HEADERS def main(): """ ```fission { "name": "profile-users-get-insert-put", "http_triggers": { "profile-users-get-insert-put-http": { "url": "/ailbl/users/profiles", "methods": ["POST", "PUT", "GET"] } } } ``` """ try: if request.method == "PUT": return make_update_request() elif request.method == "POST": return make_insert_request() elif request.method == "GET": return make_get_request() else: return {"error": "Method not allow"}, 405 except Exception as ex: return jsonify({"error": str(ex)}), 500 def make_insert_request(): try: user_id = request.headers.get("X-UserId") if not user_id: return jsonify({"errorCode": "USER_ID_REQUIRED"}), 400, CORS_HEADERS # Lấy dữ liệu từ body của request (thông tin hồ sơ người dùng) data = request.get_json() response, status = crud.insert_profile(data) return jsonify(response), status except Exception as e: return jsonify({"error": str(e)}), 500 def make_update_request(): try: data = request.get_json() # Lay du lieu json tu request body user_id = data.get("user_id") if not user_id: return jsonify({"error": "user_id is required"}), 400 # Call CRUD function to update profile response, status = crud.update_profile return jsonify(response), status except Exception as e: return jsonify({"error": str(e)}), 500 def make_get_request(): try: user_id = request.headers.get("X-User") if not user_id: return jsonify({"error": "user_id is required"}), 400 response, status = crud.get_profile(user_id) return jsonify(response), status except Exception as e: return jsonify({"error": str(e)}), 500