transfer from private server/git server
This commit is contained in:
62
web/routes/annonces.py
Normal file
62
web/routes/annonces.py
Normal file
@@ -0,0 +1,62 @@
|
||||
"""routes/annonces.py — API CRUD du module Annonces (EXF-01 à EXF-04)."""
|
||||
|
||||
from flask import Blueprint, current_app, jsonify, request
|
||||
|
||||
from models.annonce_model import AnnonceNotFoundError
|
||||
from services import annonce_service
|
||||
from services.auth_service import client_ip, current_user, require_admin, require_login
|
||||
|
||||
bp = Blueprint("annonces", __name__, url_prefix="/api/annonces")
|
||||
|
||||
|
||||
def _db():
|
||||
return current_app.config["DB"]
|
||||
|
||||
|
||||
@bp.route("", methods=["GET"])
|
||||
@require_login
|
||||
def list_annonces():
|
||||
return jsonify(annonce_service.list_annonces(_db()))
|
||||
|
||||
|
||||
@bp.route("", methods=["POST"])
|
||||
@require_admin
|
||||
def create_annonce():
|
||||
data = request.json or {}
|
||||
user = current_user()
|
||||
try:
|
||||
new_id = annonce_service.create_annonce(
|
||||
_db(), data.get("titre"), data.get("contenu"), user["sub"],
|
||||
data.get("epinglee", False), client_ip(),
|
||||
)
|
||||
except annonce_service.ValidationError as exc:
|
||||
return jsonify({"error": str(exc)}), 400
|
||||
return jsonify({"id": new_id}), 201
|
||||
|
||||
|
||||
@bp.route("/<int:annonce_id>", methods=["PUT"])
|
||||
@require_admin
|
||||
def update_annonce(annonce_id):
|
||||
data = request.json or {}
|
||||
user = current_user()
|
||||
try:
|
||||
annonce_service.update_annonce(
|
||||
_db(), annonce_id, data.get("titre"), data.get("contenu"), user["sub"],
|
||||
data.get("epinglee", False), client_ip(),
|
||||
)
|
||||
except annonce_service.ValidationError as exc:
|
||||
return jsonify({"error": str(exc)}), 400
|
||||
except AnnonceNotFoundError as exc:
|
||||
return jsonify({"error": str(exc)}), 404
|
||||
return jsonify({"status": "updated"})
|
||||
|
||||
|
||||
@bp.route("/<int:annonce_id>", methods=["DELETE"])
|
||||
@require_admin
|
||||
def delete_annonce(annonce_id):
|
||||
user = current_user()
|
||||
try:
|
||||
annonce_service.delete_annonce(_db(), annonce_id, user["sub"], client_ip())
|
||||
except AnnonceNotFoundError as exc:
|
||||
return jsonify({"error": str(exc)}), 404
|
||||
return jsonify({"status": "deleted"})
|
||||
Reference in New Issue
Block a user