CICD/app/widget/controller.py

57 lines
1.6 KiB
Python
Raw Normal View History

2019-05-18 19:00:13 +02:00
from flask import request
from flask_accepts import accepts, responds
2020-01-22 18:05:43 +01:00
from flask_restx import Namespace, Resource
2019-05-18 19:00:13 +02:00
from flask.wrappers import Response
from typing import List
from .schema import WidgetSchema
from .service import WidgetService
from .model import Widget
from .interface import WidgetInterface
2019-08-03 18:55:38 +02:00
api = Namespace("Widget", description="Single namespace, single entity") # noqa
2019-05-18 19:00:13 +02:00
2019-08-03 18:55:38 +02:00
@api.route("/")
2019-05-18 19:00:13 +02:00
class WidgetResource(Resource):
2019-08-03 18:55:38 +02:00
"""Widgets"""
2019-05-18 19:00:13 +02:00
@responds(schema=WidgetSchema, many=True)
def get(self) -> List[Widget]:
2019-08-03 18:55:38 +02:00
"""Get all Widgets"""
2019-05-18 19:00:13 +02:00
return WidgetService.get_all()
@accepts(schema=WidgetSchema, api=api)
@responds(schema=WidgetSchema)
def post(self) -> Widget:
2019-08-03 18:55:38 +02:00
"""Create a Single Widget"""
2019-05-18 19:00:13 +02:00
return WidgetService.create(request.parsed_obj)
2019-08-03 18:55:38 +02:00
@api.route("/<int:widgetId>")
@api.param("widgetId", "Widget database ID")
2019-05-18 19:00:13 +02:00
class WidgetIdResource(Resource):
@responds(schema=WidgetSchema)
def get(self, widgetId: int) -> Widget:
2019-08-03 18:55:38 +02:00
"""Get Single Widget"""
2019-05-18 19:00:13 +02:00
return WidgetService.get_by_id(widgetId)
def delete(self, widgetId: int) -> Response:
2019-08-03 18:55:38 +02:00
"""Delete Single Widget"""
2019-05-18 19:00:13 +02:00
from flask import jsonify
id = WidgetService.delete_by_id(widgetId)
2019-08-03 18:55:38 +02:00
return jsonify(dict(status="Success", id=id))
2019-05-18 19:00:13 +02:00
@accepts(schema=WidgetSchema, api=api)
@responds(schema=WidgetSchema)
def put(self, widgetId: int) -> Widget:
2019-08-03 18:55:38 +02:00
"""Update Single Widget"""
2019-05-18 19:00:13 +02:00
changes: WidgetInterface = request.parsed_obj
Widget = WidgetService.get_by_id(widgetId)
return WidgetService.update(Widget, changes)