ajout de CRUD total pour toute les routes

This commit is contained in:
toine 2025-10-04 17:48:13 +02:00
parent fec74d9f83
commit da45beba93
5 changed files with 166 additions and 25 deletions

View file

@ -1,5 +1,5 @@
from flask import Blueprint, jsonify, request, current_app
from utils.json_crud import load_json, save_json, add_entry, delete_entry, update_entry
from utils.json_crud import load_json, save_json
from utils.data_loader import load_data
from models.contact_model import ContactModel
import os
@ -11,19 +11,46 @@ contact_bp = Blueprint('contact', __name__, url_prefix='/api/contact')
@contact_bp.route('/', methods=['GET'])
def get_contact():
contacts = load_data('contact.json')
return jsonify(contacts)
contact = load_data('contact.json')
return jsonify(contact)
@contact_bp.route('/', methods=['POST'])
def create_or_replace_contact():
key = request.headers.get('x-api-key')
if key != current_app.config['API_KEY']:
return jsonify({"error": "Unauthorized"}), 401
data = request.json or {}
model = ContactModel(data)
if not model.is_valid():
return jsonify({"error": "Invalid contact data"}), 400
new_entry = model.to_dict()
save_json(CONTACTS_FILE, new_entry)
return jsonify(new_entry), 201
@contact_bp.route('/', methods=['PUT', 'PATCH'])
def update_contact():
key = request.headers.get('x-api-key')
if key != current_app.config['API_KEY']:
return jsonify({
"error": "Unauthorized"
}), 401
return jsonify({"error": "Unauthorized"}), 401
data = request.json
new_entry = ContactModel(data).to_dict()
contact = [new_entry]
save_json(CONTACTS_FILE, contact)
return new_entry
current = load_json(CONTACTS_FILE) or {}
payload = request.json or {}
updated = {**current, **payload}
model = ContactModel(updated)
if not model.is_valid():
return jsonify({"error": "Invalid contact data"}), 400
save_json(CONTACTS_FILE, model.to_dict())
return jsonify(model.to_dict()), 200
@contact_bp.route('/', methods=['DELETE'])
def delete_contact():
key = request.headers.get('x-api-key')
if key != current_app.config['API_KEY']:
return jsonify({"error": "Unauthorized"}), 401
# Clear contact info
save_json(CONTACTS_FILE, {})
return jsonify({"status": "success"}), 200