routing and model __init__ updates
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
from flask import Blueprint
|
||||
from flask_jwt_extended import jwt_required
|
||||
from flask_apispec import use_kwargs, marshal_with, doc
|
||||
from flask_jwt_extended import jwt_required
|
||||
from marshmallow import fields
|
||||
|
||||
from api.exceptions import NotFoundException
|
||||
from api.exceptions import NotFoundException, BadRequestException
|
||||
from api.target_exchange.models import TargetExchange
|
||||
|
||||
from .models import Target
|
||||
from .serializers import target_schema, targets_schema
|
||||
|
||||
@@ -29,18 +28,21 @@ def get_list(exchange=None):
|
||||
@jwt_required()
|
||||
@use_kwargs(target_schema)
|
||||
@marshal_with(target_schema)
|
||||
def create(name):
|
||||
target = Target(name=name)
|
||||
def create(name, routing_key, exchange):
|
||||
xchange = TargetExchange.get_by_id(exchange.id)
|
||||
if xchange is None:
|
||||
raise BadRequestException(f"the exchange {exchange.name}({exchange.id}) could not be found")
|
||||
target = Target(name=name, routing_key=routing_key, target_exchange_id=xchange.id)
|
||||
target.save()
|
||||
return target
|
||||
|
||||
|
||||
@doc(tags=['Target'])
|
||||
@blueprint.route('/<id>', methods=['GET'])
|
||||
@blueprint.route('/<target_id>', methods=['GET'])
|
||||
@jwt_required()
|
||||
@marshal_with(target_schema)
|
||||
def get_by_id(id):
|
||||
target_exchange = Target.get_by_id(id)
|
||||
def get_by_id(target_id: int):
|
||||
target_exchange = Target.get_by_id(target_id)
|
||||
if target_exchange is not None:
|
||||
return target_exchange
|
||||
else:
|
||||
@@ -48,12 +50,12 @@ def get_by_id(id):
|
||||
|
||||
|
||||
@doc(tags=['Target'])
|
||||
@blueprint.route('/<id>', methods=['PUT'])
|
||||
@blueprint.route('/<target_id>', methods=['PUT'])
|
||||
@jwt_required()
|
||||
@use_kwargs(target_schema)
|
||||
@marshal_with(target_schema)
|
||||
def update(id, **kwargs):
|
||||
target_exchange = Target.get_by_id(id)
|
||||
def update(target_id, **kwargs):
|
||||
target_exchange = Target.get_by_id(target_id)
|
||||
if target_exchange is not None:
|
||||
return target_exchange.update(**kwargs)
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user