from flask import Blueprint, jsonify, request, current_app from utils.data_loader import load_data from utils.json_crud import load_json, save_json from models.cv_model import CVModel import os DATA_DIR = os.path.join(os.path.dirname(__file__), '../data') CV_FILE = os.path.join(DATA_DIR, 'cv.json') cv_bp = Blueprint('cv', __name__, url_prefix='/api/cv') @cv_bp.route('/', methods=['GET']) def get_cv(): cv = load_data('cv.json') return jsonify(cv) @cv_bp.route('/', methods=['POST']) def create_or_replace_cv(): key = request.headers.get('x-api-key') if key != current_app.config['API_KEY']: return jsonify({"error": "Unauthorized"}), 401 data = request.json or {} new_entry = CVModel(data).to_dict() cv = [new_entry] save_json(CV_FILE, cv) return jsonify(new_entry), 201 @cv_bp.route('/', methods=['PUT', 'PATCH']) def update_cv(): key = request.headers.get('x-api-key') if key != current_app.config['API_KEY']: return jsonify({"error": "Unauthorized"}), 401 current_list = load_json(CV_FILE) or [] current = current_list[0] if current_list else {} payload = request.json or {} updated = {**current, **payload} new_entry = CVModel(updated).to_dict() save_json(CV_FILE, [new_entry]) return jsonify(new_entry), 200 @cv_bp.route('/', methods=['DELETE']) def delete_cv(): key = request.headers.get('x-api-key') if key != current_app.config['API_KEY']: return jsonify({"error": "Unauthorized"}), 401 save_json(CV_FILE, []) return jsonify({"status": "success"}), 200 @cv_bp.route('/skills/', methods=['GET']) def get_skills(): cv = load_data('cv.json') if not cv: return jsonify([]) skills = cv[0].get('my_skills') return jsonify(skills) @cv_bp.route('/about/', methods=['GET']) def get_about(): cv = load_data('cv.json') if not cv: return jsonify("") about = cv[0].get('about_text') return jsonify(about)