CICD/app/other_api/doodad/controller.py

58 lines
1.6 KiB
Python
Raw Normal View History

2019-05-18 19:47:47 +02:00
from flask import request
from flask_accepts import accepts, responds
from flask_restplus import Namespace, Resource
from flask.wrappers import Response
from typing import List
from .schema import DoodadSchema
from .service import DoodadService
from .model import Doodad
from .interface import DoodadInterface
2019-08-03 18:55:38 +02:00
api = Namespace("Doodad", description="A modular namespace within Other API") # noqa
2019-05-18 19:47:47 +02:00
2019-08-03 18:55:38 +02:00
@api.route("/")
2019-05-18 19:47:47 +02:00
class DoodadResource(Resource):
2019-08-03 18:55:38 +02:00
"""Doodads"""
2019-05-18 19:47:47 +02:00
@responds(schema=DoodadSchema, many=True)
def get(self) -> List[Doodad]:
2019-08-03 18:55:38 +02:00
"""Get all Doodads"""
2019-05-18 19:47:47 +02:00
return DoodadService.get_all()
@accepts(schema=DoodadSchema, api=api)
@responds(schema=DoodadSchema)
def post(self) -> Doodad:
2019-08-03 18:55:38 +02:00
"""Create a Single Doodad"""
2019-05-18 19:47:47 +02:00
return DoodadService.create(request.parsed_obj)
2019-08-03 18:55:38 +02:00
@api.route("/<int:doodadId>")
@api.param("doodadId", "Doodad database ID")
2019-05-18 19:47:47 +02:00
class DoodadIdResource(Resource):
@responds(schema=DoodadSchema)
def get(self, doodadId: int) -> Doodad:
2019-08-03 18:55:38 +02:00
"""Get Single Doodad"""
2019-05-18 19:47:47 +02:00
return DoodadService.get_by_id(doodadId)
def delete(self, doodadId: int) -> Response:
2019-08-03 18:55:38 +02:00
"""Delete Single Doodad"""
2019-05-18 19:47:47 +02:00
from flask import jsonify
2019-08-03 18:55:38 +02:00
print("doodadId = ", doodadId)
2019-05-18 19:47:47 +02:00
id = DoodadService.delete_by_id(doodadId)
2019-08-03 18:55:38 +02:00
return jsonify(dict(status="Success", id=id))
2019-05-18 19:47:47 +02:00
@accepts(schema=DoodadSchema, api=api)
@responds(schema=DoodadSchema)
def put(self, doodadId: int) -> Doodad:
2019-08-03 18:55:38 +02:00
"""Update Single Doodad"""
2019-05-18 19:47:47 +02:00
changes: DoodadInterface = request.parsed_obj
Doodad = DoodadService.get_by_id(doodadId)
return DoodadService.update(Doodad, changes)