main
Сергей Ванюшкин 2024-04-15 06:32:17 +00:00
parent 2b626b189f
commit 3e57c393c2
7 changed files with 57 additions and 17 deletions

View File

@ -4,7 +4,9 @@ from dishka import Container, Provider, Scope, make_container, provide
from protocols.repository import Repository from protocols.repository import Repository
from repository.config import RedisSettings from repository.config import RedisSettings
from repository.redis import RedisRepository from repository.redis import RedisRepository
from usecase.add import PostKey
from usecase.get import GetKey from usecase.get import GetKey
from usecase.put import PutKey
class RedisSettingsProvider(Provider): class RedisSettingsProvider(Provider):
@ -35,6 +37,8 @@ class UseCasesProvider(Provider):
scope = Scope.REQUEST scope = Scope.REQUEST
get = provide(GetKey) get = provide(GetKey)
post = provide(PostKey)
put = provide(PutKey)
def create_container() -> Container: def create_container() -> Container:

View File

@ -1,7 +1,7 @@
from dataclasses import dataclass from dataclasses import dataclass
@dataclass() @dataclass(frozen=True)
class KeyDTO: class KeyDTO:
key: str key: str
val: str val: str | None = None

View File

@ -8,10 +8,10 @@ class RedisRepository(Repository):
self.pool = redis_pool self.pool = redis_pool
def get_key(self, obj: KeyDTO) -> KeyDTO | None: def get_key(self, obj: KeyDTO) -> KeyDTO | None:
data = self.pool.get(obj.key) # data = self.pool.get(obj.key)
if not data: # if not data:
return None # return None
return KeyDTO(key=obj.key, val=str(data)) return KeyDTO(key="10", val="1")
def add_key(self, obj: KeyDTO) -> KeyDTO: def add_key(self, obj: KeyDTO) -> KeyDTO:
self.pool.set(obj.key, obj.val) self.pool.set(obj.key, obj.val)

View File

@ -1,15 +1,21 @@
from dishka.integrations.flask import FromDishka, inject
from flask import Blueprint, jsonify, request from flask import Blueprint, jsonify, request
from protocols.models import KeyDTO
from usecase.add import PostKey
from usecase.get import GetKey
from usecase.put import PutKey
key_bp = Blueprint("key_bp", __name__) key_bp = Blueprint("key_bp", __name__)
@key_bp.route("/", methods=["POST"]) @key_bp.route("/", methods=["POST"])
def past_key(): def past_key(usecase: FromDishka[PostKey]):
json_data = request.get_json() json_data = request.get_json()
if json_data: if json_data:
result = usecase(request=KeyDTO(key=json_data))
return ( return (
jsonify({"message": "Received JSON data successfully", "data": json_data}), jsonify({"message": "Received JSON data successfully", "data": result}),
200, 200,
) )
else: else:
@ -17,25 +23,29 @@ def past_key():
@key_bp.route("/", methods=["GET"]) @key_bp.route("/", methods=["GET"])
def get_key(): @inject
json_data = request.args.get("key") def get_key(usecase: FromDishka[GetKey]):
request_data = request.args.get("key")
if json_data: if request_data:
result = usecase(request=KeyDTO(key=request_data))
return ( return (
jsonify({"message": "Received JSON data successfully", "data": json_data}), jsonify({"message": "Received JSON data successfully", "data": result}),
200, 200,
) )
else: else:
return jsonify({"message": "No JSON data received"}), 400 return jsonify({"message": "No GET parameters received"}), 400
@key_bp.route("/", methods=["PUT"]) @key_bp.route("/", methods=["PUT"])
def put_key(): def put_key(usecase: FromDishka[PutKey]):
json_data = request.get_json() json_data = request.get_json()
if json_data: if json_data:
result = usecase(request=KeyDTO(key=json_data))
return ( return (
jsonify({"message": "Received JSON data successfully", "data": json_data}), jsonify({"message": "Received JSON data successfully", "data": result}),
200, 200,
) )
else: else:

View File

@ -0,0 +1,13 @@
from protocols.models import KeyDTO
from protocols.repository import Repository
class PostKey:
def __init__(
self,
repository: Repository,
) -> None:
self.__repository = repository
def __call__(self, request: KeyDTO) -> KeyDTO:
return self.__repository.add_key(obj=request)

View File

@ -7,7 +7,7 @@ class GetKey:
self, self,
repository: Repository, repository: Repository,
) -> None: ) -> None:
self.repository = repository self.__repository = repository
def __call__(self, request: KeyDTO) -> KeyDTO | None: def __call__(self, request: KeyDTO) -> KeyDTO | None:
return self.repository.get_key(obj=request) return self.__repository.get_key(obj=request)

View File

@ -0,0 +1,13 @@
from protocols.models import KeyDTO
from protocols.repository import Repository
class PutKey:
def __init__(
self,
repository: Repository,
) -> None:
self.__repository = repository
def __call__(self, request: KeyDTO) -> KeyDTO:
return self.__repository.put_key(obj=request)