flask-demo-api/flask_demo_api/routers/key.py

85 lines
2.3 KiB
Python
Raw Normal View History

2024-04-15 09:32:17 +03:00
from dishka.integrations.flask import FromDishka, inject
2024-04-16 01:03:02 +03:00
from flask import Blueprint, abort, jsonify, request
from flask_demo_api.protocols.models import KeyDTO
from flask_demo_api.usecase.add import PostKey
from flask_demo_api.usecase.delete import DelKey
from flask_demo_api.usecase.get import GetKey
from flask_demo_api.usecase.put import PutKey
2024-04-15 02:16:07 +03:00
key_bp = Blueprint("key_bp", __name__)
@key_bp.route("/", methods=["POST"])
2024-04-16 01:03:02 +03:00
@inject
2024-04-15 09:32:17 +03:00
def past_key(usecase: FromDishka[PostKey]):
2024-04-15 02:16:07 +03:00
json_data = request.get_json()
2024-04-16 04:48:10 +03:00
if not json_data.get("key"):
abort(400, "'key' required")
elif not json_data.get("val"):
abort(400, "'val' required")
2024-04-15 02:16:07 +03:00
if json_data:
2024-04-16 04:48:10 +03:00
result = usecase(request=KeyDTO(key=json_data.get("key"), val=json_data.get("val")))
if result is None:
abort(400, "Key alredy exist")
return jsonify({"message": "Ok", "data": result}), 201
2024-04-15 02:16:07 +03:00
else:
2024-04-16 04:48:10 +03:00
abort(400, "Invalid json data")
2024-04-15 02:16:07 +03:00
@key_bp.route("/", methods=["GET"])
2024-04-15 09:32:17 +03:00
@inject
def get_key(usecase: FromDishka[GetKey]):
request_data = request.args.get("key")
2024-04-15 02:16:07 +03:00
2024-04-15 09:32:17 +03:00
if request_data:
result = usecase(request=KeyDTO(key=request_data))
2024-04-16 01:03:02 +03:00
if result is None:
abort(404, "Key not found")
2024-04-15 02:16:07 +03:00
return (
2024-04-16 01:03:02 +03:00
jsonify({"message": "Ok", "data": result}),
2024-04-15 02:16:07 +03:00
200,
)
2024-04-15 09:32:17 +03:00
2024-04-15 02:16:07 +03:00
else:
2024-04-16 01:03:02 +03:00
abort(400, "Invalid GET parameters")
2024-04-15 02:16:07 +03:00
@key_bp.route("/", methods=["PUT"])
2024-04-16 01:03:02 +03:00
@inject
2024-04-15 09:32:17 +03:00
def put_key(usecase: FromDishka[PutKey]):
2024-04-15 02:16:07 +03:00
json_data = request.get_json()
2024-04-16 04:48:10 +03:00
if not json_data.get("key"):
abort(400, "'key' required")
elif not json_data.get("val"):
abort(400, "'val' required")
2024-04-15 02:16:07 +03:00
if json_data:
2024-04-16 04:48:10 +03:00
result = usecase(request=KeyDTO(key=json_data.get("key"), val=json_data.get("val")))
if result is None:
abort(400, "No item for update")
2024-04-16 01:03:02 +03:00
return (
jsonify({"message": "Updated", "data": result}),
200,
)
else:
2024-04-16 04:48:10 +03:00
abort(400, "Inalid JSON data")
2024-04-16 01:03:02 +03:00
@key_bp.route("/", methods=["DELETE"])
@inject
def delete_key(usecase: FromDishka[DelKey]):
json_data = request.get_json()
if json_data:
usecase(request=KeyDTO(key=json_data.get("key"), val=json_data.get("val")))
2024-04-15 02:16:07 +03:00
return (
2024-04-16 01:03:02 +03:00
jsonify({"message": "Deleted"}),
2024-04-15 02:16:07 +03:00
200,
)
else:
2024-04-16 04:48:10 +03:00
abort(400, "Inalid JSON data")