|
| 1 | +# Copyright © 2024 Province of British Columbia |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the 'License'); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an 'AS IS' BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""API endpoints for managing Appendix resource.""" |
| 15 | + |
| 16 | +from http import HTTPStatus |
| 17 | + |
| 18 | +from flask import request |
| 19 | +from flask_restx import Namespace, Resource |
| 20 | + |
| 21 | +from compliance_api.auth import auth |
| 22 | +from compliance_api.exceptions import ResourceNotFoundError |
| 23 | +from compliance_api.schemas import AppendixCreateSchema, AppendixSchema |
| 24 | +from compliance_api.services import AppendixService |
| 25 | +from compliance_api.utils.enum import PermissionEnum |
| 26 | +from compliance_api.utils.util import cors_preflight |
| 27 | + |
| 28 | +from .apihelper import Api as ApiHelper |
| 29 | + |
| 30 | + |
| 31 | +API = Namespace("appendices", description="Endpoints for Appendix Management") |
| 32 | + |
| 33 | +appendix_request_model = ApiHelper.convert_ma_schema_to_restx_model( |
| 34 | + API, AppendixCreateSchema(), "Appendix" |
| 35 | +) |
| 36 | +appendix_list_model = ApiHelper.convert_ma_schema_to_restx_model( |
| 37 | + API, AppendixSchema(), "AppendixList" |
| 38 | +) |
| 39 | + |
| 40 | + |
| 41 | +@cors_preflight("GET, OPTIONS, POST") |
| 42 | +@API.route("", methods=["POST", "GET", "OPTIONS"]) |
| 43 | +class Appendices(Resource): |
| 44 | + """Resource for managing appendices.""" |
| 45 | + |
| 46 | + @staticmethod |
| 47 | + @API.response(code=200, description="Success", model=[appendix_list_model]) |
| 48 | + @API.doc( |
| 49 | + params={ |
| 50 | + "inspection_id": { |
| 51 | + "description": "The unique identifier of the inspection", |
| 52 | + "type": "integer", |
| 53 | + "required": False, |
| 54 | + } |
| 55 | + } |
| 56 | + ) |
| 57 | + @ApiHelper.swagger_decorators(API, endpoint_description="Fetch all appendices") |
| 58 | + @auth.require |
| 59 | + def get(): |
| 60 | + """Fetch all appendices.""" |
| 61 | + inspection_id = request.args.get("inspection_id", None) |
| 62 | + if inspection_id: |
| 63 | + appendices = AppendixService.get_by_inspection_id(inspection_id) |
| 64 | + else: |
| 65 | + appendices = AppendixService.get_all() |
| 66 | + appendix_list_schema = AppendixSchema(many=True) |
| 67 | + return appendix_list_schema.dump(appendices), HTTPStatus.OK |
| 68 | + |
| 69 | + @staticmethod |
| 70 | + @auth.require |
| 71 | + @ApiHelper.swagger_decorators(API, endpoint_description="Create an Appendix") |
| 72 | + @API.expect(appendix_request_model) |
| 73 | + @API.response(code=201, model=appendix_list_model, description="AppendixCreated") |
| 74 | + @API.response(400, "Bad Request") |
| 75 | + @auth.has_one_of_roles([PermissionEnum.SUPERUSER, PermissionEnum.ADMIN]) |
| 76 | + def post(): |
| 77 | + """Create a Appendix.""" |
| 78 | + appendix_data = AppendixCreateSchema().load(API.payload) |
| 79 | + created_appendix = AppendixService.create(appendix_data) |
| 80 | + return AppendixSchema().dump(created_appendix), HTTPStatus.CREATED |
| 81 | + |
| 82 | + |
| 83 | +@cors_preflight("GET, OPTIONS, PATCH, DELETE") |
| 84 | +@API.route("/<int:appendix_id>", methods=["PATCH", "GET", "OPTIONS", "DELETE"]) |
| 85 | +@API.doc(params={"appendix_id": "The unique identifier of appendix"}) |
| 86 | +class Appendix(Resource): |
| 87 | + """Resource for managing a single Appendix.""" |
| 88 | + |
| 89 | + @staticmethod |
| 90 | + @auth.require |
| 91 | + @ApiHelper.swagger_decorators(API, endpoint_description="Fetch an appendix by id") |
| 92 | + @API.response(code=200, model=appendix_list_model, description="Success") |
| 93 | + @API.response(404, "Not Found") |
| 94 | + def get(appendix_id): |
| 95 | + """Fetch an appendix by id.""" |
| 96 | + appendix = AppendixService.get_by_id(appendix_id) |
| 97 | + if not appendix: |
| 98 | + raise ResourceNotFoundError(f"Appendix with {appendix_id} not found") |
| 99 | + return AppendixSchema().dump(Appendix), HTTPStatus.OK |
| 100 | + |
| 101 | + @staticmethod |
| 102 | + @auth.require |
| 103 | + @ApiHelper.swagger_decorators(API, endpoint_description="Update an appendix by id") |
| 104 | + @API.expect(appendix_request_model) |
| 105 | + @API.response(code=200, model=appendix_list_model, description="Success") |
| 106 | + @API.response(400, "Bad Request") |
| 107 | + @API.response(404, "Not Found") |
| 108 | + @auth.has_one_of_roles([PermissionEnum.SUPERUSER, PermissionEnum.ADMIN]) |
| 109 | + def patch(appendix_id): |
| 110 | + """Update an Appendix by id.""" |
| 111 | + appendix_data = AppendixCreateSchema().load(API.payload) |
| 112 | + updated_appendix = AppendixService.update(appendix_id, appendix_data) |
| 113 | + if not updated_appendix: |
| 114 | + raise ResourceNotFoundError(f"Appendix with {appendix_id} not found") |
| 115 | + return AppendixSchema().dump(updated_appendix), HTTPStatus.OK |
| 116 | + |
| 117 | + @staticmethod |
| 118 | + @auth.require |
| 119 | + @ApiHelper.swagger_decorators(API, endpoint_description="Delete an appendix by id") |
| 120 | + @API.response(code=200, model=appendix_list_model, description="Deleted") |
| 121 | + @API.response(404, "Not Found") |
| 122 | + @auth.has_one_of_roles([PermissionEnum.SUPERUSER, PermissionEnum.ADMIN]) |
| 123 | + def delete(appendix_id): |
| 124 | + """Delete an appendix by id.""" |
| 125 | + deleted_appendix = AppendixService.delete(appendix_id) |
| 126 | + if not deleted_appendix: |
| 127 | + raise ResourceNotFoundError(f"Appendix with {appendix_id} not found") |
| 128 | + return AppendixSchema().dump(deleted_appendix), HTTPStatus.OK |
0 commit comments