|
| 1 | +<?php namespace App\Http\Controllers; |
| 2 | +/* |
| 3 | + * Copyright 2026 OpenStack Foundation |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + **/ |
| 14 | + |
| 15 | +use App\Models\Foundation\Main\IGroup; |
| 16 | +use App\Models\Foundation\Summit\Repositories\ISummitSponsorshipAddOnTypeRepository; |
| 17 | +use App\ModelSerializers\SerializerUtils; |
| 18 | +use App\Security\SummitScopes; |
| 19 | +use App\Services\Model\ISummitSponsorshipAddOnTypeService; |
| 20 | +use models\oauth2\IResourceServerContext; |
| 21 | +use models\summit\ISummitRepository; |
| 22 | +use ModelSerializers\SerializerRegistry; |
| 23 | +use OpenApi\Attributes as OA; |
| 24 | +use Symfony\Component\HttpFoundation\Response; |
| 25 | + |
| 26 | +/** |
| 27 | + * Class OAuth2SummitSponsorshipAddOnTypesApiController |
| 28 | + * @package App\Http\Controllers |
| 29 | + */ |
| 30 | +final class OAuth2SummitSponsorshipAddOnTypesApiController extends OAuth2ProtectedController |
| 31 | +{ |
| 32 | + use RequestProcessor; |
| 33 | + use GetAll; |
| 34 | + use GetAndValidateJsonPayload; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var ISummitSponsorshipAddOnTypeService |
| 38 | + */ |
| 39 | + private $service; |
| 40 | + |
| 41 | + /** |
| 42 | + * @param ISummitSponsorshipAddOnTypeRepository $repository |
| 43 | + * @param ISummitRepository $summit_repository |
| 44 | + * @param ISummitSponsorshipAddOnTypeService $service |
| 45 | + * @param IResourceServerContext $resource_server_context |
| 46 | + */ |
| 47 | + public function __construct( |
| 48 | + ISummitSponsorshipAddOnTypeRepository $repository, |
| 49 | + ISummitRepository $summit_repository, |
| 50 | + ISummitSponsorshipAddOnTypeService $service, |
| 51 | + IResourceServerContext $resource_server_context |
| 52 | + ) |
| 53 | + { |
| 54 | + parent::__construct($resource_server_context); |
| 55 | + $this->repository = $repository; |
| 56 | + $this->summit_repository = $summit_repository; |
| 57 | + $this->service = $service; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @return array |
| 62 | + */ |
| 63 | + protected function getFilterRules(): array |
| 64 | + { |
| 65 | + return [ |
| 66 | + 'name' => ['==', '=@'], |
| 67 | + ]; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @return array |
| 72 | + */ |
| 73 | + protected function getFilterValidatorRules(): array |
| 74 | + { |
| 75 | + return [ |
| 76 | + 'name' => 'sometimes|required|string', |
| 77 | + ]; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @return array |
| 82 | + */ |
| 83 | + protected function getOrderRules(): array |
| 84 | + { |
| 85 | + return [ |
| 86 | + 'id', |
| 87 | + 'name', |
| 88 | + ]; |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + /** |
| 93 | + * @return ISummitRepository |
| 94 | + */ |
| 95 | + protected function getSummitRepository(): ISummitRepository |
| 96 | + { |
| 97 | + return $this->summit_repository; |
| 98 | + } |
| 99 | + |
| 100 | + #[OA\Get( |
| 101 | + path: "/api/v1/summits/all/add-on-types", |
| 102 | + summary: "Get all sponsorship add-on types", |
| 103 | + operationId: 'getAddOnTypes', |
| 104 | + x: [ |
| 105 | + 'required-groups' => [ |
| 106 | + IGroup::SuperAdmins, |
| 107 | + IGroup::Administrators, |
| 108 | + IGroup::SummitAdministrators, |
| 109 | + ] |
| 110 | + ], |
| 111 | + security: [['summit_sponsorship_oauth2' => [ |
| 112 | + SummitScopes::ReadSummitData, |
| 113 | + SummitScopes::ReadAllSummitData, |
| 114 | + ]]], |
| 115 | + tags: ["Sponsorship Add-On Types"], |
| 116 | + parameters: [ |
| 117 | + new OA\Parameter(name: "page", description: "Page number", in: "query", required: false, schema: new OA\Schema(type: "integer", default: 1)), |
| 118 | + new OA\Parameter(name: "per_page", description: "Items per page", in: "query", required: false, schema: new OA\Schema(type: "integer", default: 10)), |
| 119 | + new OA\Parameter(name: "filter", description: "Filter query (name==value, name=@value)", in: "query", required: false, schema: new OA\Schema(type: "string")), |
| 120 | + new OA\Parameter(name: "order", description: "Order by (+id, -name)", in: "query", required: false, schema: new OA\Schema(type: "string")), |
| 121 | + ], |
| 122 | + responses: [ |
| 123 | + new OA\Response(response: Response::HTTP_OK, description: "OK"), |
| 124 | + new OA\Response(response: Response::HTTP_UNAUTHORIZED, description: "Unauthorized"), |
| 125 | + new OA\Response(response: Response::HTTP_FORBIDDEN, description: "Forbidden"), |
| 126 | + new OA\Response(response: Response::HTTP_INTERNAL_SERVER_ERROR, description: "Server Error"), |
| 127 | + ] |
| 128 | + )] |
| 129 | + |
| 130 | + #[OA\Post( |
| 131 | + path: "/api/v1/summits/all/add-on-types", |
| 132 | + summary: "Add a new sponsorship add-on type", |
| 133 | + operationId: 'addAddOnType', |
| 134 | + x: [ |
| 135 | + 'required-groups' => [ |
| 136 | + IGroup::SuperAdmins, |
| 137 | + IGroup::Administrators, |
| 138 | + IGroup::SummitAdministrators, |
| 139 | + ] |
| 140 | + ], |
| 141 | + security: [['summit_sponsorship_oauth2' => [ |
| 142 | + SummitScopes::WriteSummitData, |
| 143 | + ]]], |
| 144 | + tags: ["Sponsorship Add-On Types"], |
| 145 | + requestBody: new OA\RequestBody( |
| 146 | + required: true, |
| 147 | + content: new OA\JsonContent( |
| 148 | + required: ["name"], |
| 149 | + properties: [ |
| 150 | + new OA\Property(property: "name", type: "string"), |
| 151 | + ] |
| 152 | + ) |
| 153 | + ), |
| 154 | + responses: [ |
| 155 | + new OA\Response(response: Response::HTTP_CREATED, description: "Created"), |
| 156 | + new OA\Response(response: Response::HTTP_UNAUTHORIZED, description: "Unauthorized"), |
| 157 | + new OA\Response(response: Response::HTTP_FORBIDDEN, description: "Forbidden"), |
| 158 | + new OA\Response(response: Response::HTTP_PRECONDITION_FAILED, description: "Validation Error"), |
| 159 | + new OA\Response(response: Response::HTTP_INTERNAL_SERVER_ERROR, description: "Server Error"), |
| 160 | + ] |
| 161 | + )] |
| 162 | + public function add() |
| 163 | + { |
| 164 | + return $this->processRequest(function () { |
| 165 | + $payload = $this->getJsonPayload( |
| 166 | + SummitSponsorshipAddOnTypeValidationRulesFactory::buildForAdd(), |
| 167 | + true |
| 168 | + ); |
| 169 | + |
| 170 | + $type = $this->service->add($payload); |
| 171 | + |
| 172 | + return $this->created(SerializerRegistry::getInstance()->getSerializer($type)->serialize( |
| 173 | + SerializerUtils::getExpand(), |
| 174 | + SerializerUtils::getFields(), |
| 175 | + SerializerUtils::getRelations() |
| 176 | + )); |
| 177 | + }); |
| 178 | + } |
| 179 | + |
| 180 | + #[OA\Get( |
| 181 | + path: "/api/v1/summits/all/add-on-types/{id}", |
| 182 | + summary: "Get a sponsorship add-on type by id", |
| 183 | + operationId: 'getAddOnType', |
| 184 | + security: [['summit_sponsorship_oauth2' => [ |
| 185 | + SummitScopes::ReadSummitData, |
| 186 | + SummitScopes::ReadAllSummitData, |
| 187 | + ]]], |
| 188 | + tags: ["Sponsorship Add-On Types"], |
| 189 | + parameters: [ |
| 190 | + new OA\Parameter(name: "id", description: "Add-on Type ID", in: "path", required: true, schema: new OA\Schema(type: "integer")), |
| 191 | + ], |
| 192 | + responses: [ |
| 193 | + new OA\Response(response: Response::HTTP_OK, description: "OK"), |
| 194 | + new OA\Response(response: Response::HTTP_NOT_FOUND, description: "Not Found"), |
| 195 | + new OA\Response(response: Response::HTTP_UNAUTHORIZED, description: "Unauthorized"), |
| 196 | + new OA\Response(response: Response::HTTP_FORBIDDEN, description: "Forbidden"), |
| 197 | + new OA\Response(response: Response::HTTP_INTERNAL_SERVER_ERROR, description: "Server Error"), |
| 198 | + ] |
| 199 | + )] |
| 200 | + public function get($id) |
| 201 | + { |
| 202 | + return $this->processRequest(function () use ($id) { |
| 203 | + $type = $this->repository->getById(intval($id)); |
| 204 | + if (is_null($type)) |
| 205 | + return $this->error404(); |
| 206 | + |
| 207 | + return $this->ok(SerializerRegistry::getInstance()->getSerializer($type)->serialize( |
| 208 | + SerializerUtils::getExpand(), |
| 209 | + SerializerUtils::getFields(), |
| 210 | + SerializerUtils::getRelations() |
| 211 | + )); |
| 212 | + }); |
| 213 | + } |
| 214 | + |
| 215 | + #[OA\Put( |
| 216 | + path: "/api/v1/summits/all/add-on-types/{id}", |
| 217 | + summary: "Update a sponsorship add-on type", |
| 218 | + operationId: 'updateAddOnType', |
| 219 | + x: [ |
| 220 | + 'required-groups' => [ |
| 221 | + IGroup::SuperAdmins, |
| 222 | + IGroup::Administrators, |
| 223 | + IGroup::SummitAdministrators, |
| 224 | + ] |
| 225 | + ], |
| 226 | + security: [['summit_sponsorship_oauth2' => [ |
| 227 | + SummitScopes::WriteSummitData, |
| 228 | + ]]], |
| 229 | + tags: ["Sponsorship Add-On Types"], |
| 230 | + parameters: [ |
| 231 | + new OA\Parameter(name: "id", description: "Add-on Type ID", in: "path", required: true, schema: new OA\Schema(type: "integer")), |
| 232 | + ], |
| 233 | + requestBody: new OA\RequestBody( |
| 234 | + required: true, |
| 235 | + content: new OA\JsonContent( |
| 236 | + properties: [ |
| 237 | + new OA\Property(property: "name", type: "string"), |
| 238 | + ] |
| 239 | + ) |
| 240 | + ), |
| 241 | + responses: [ |
| 242 | + new OA\Response(response: Response::HTTP_OK, description: "OK"), |
| 243 | + new OA\Response(response: Response::HTTP_NOT_FOUND, description: "Not Found"), |
| 244 | + new OA\Response(response: Response::HTTP_UNAUTHORIZED, description: "Unauthorized"), |
| 245 | + new OA\Response(response: Response::HTTP_FORBIDDEN, description: "Forbidden"), |
| 246 | + new OA\Response(response: Response::HTTP_PRECONDITION_FAILED, description: "Validation Error"), |
| 247 | + new OA\Response(response: Response::HTTP_INTERNAL_SERVER_ERROR, description: "Server Error"), |
| 248 | + ] |
| 249 | + )] |
| 250 | + public function update($id) |
| 251 | + { |
| 252 | + return $this->processRequest(function () use ($id) { |
| 253 | + $payload = $this->getJsonPayload( |
| 254 | + SummitSponsorshipAddOnTypeValidationRulesFactory::buildForUpdate(), |
| 255 | + true |
| 256 | + ); |
| 257 | + |
| 258 | + $type = $this->service->update(intval($id), $payload); |
| 259 | + |
| 260 | + return $this->updated(SerializerRegistry::getInstance()->getSerializer($type)->serialize( |
| 261 | + SerializerUtils::getExpand(), |
| 262 | + SerializerUtils::getFields(), |
| 263 | + SerializerUtils::getRelations() |
| 264 | + )); |
| 265 | + }); |
| 266 | + } |
| 267 | + |
| 268 | + #[OA\Delete( |
| 269 | + path: "/api/v1/summits/all/add-on-types/{id}", |
| 270 | + summary: "Delete a sponsorship add-on type", |
| 271 | + operationId: 'deleteAddOnType', |
| 272 | + x: [ |
| 273 | + 'required-groups' => [ |
| 274 | + IGroup::SuperAdmins, |
| 275 | + IGroup::Administrators, |
| 276 | + IGroup::SummitAdministrators, |
| 277 | + ] |
| 278 | + ], |
| 279 | + security: [['summit_sponsorship_oauth2' => [ |
| 280 | + SummitScopes::WriteSummitData, |
| 281 | + ]]], |
| 282 | + tags: ["Sponsorship Add-On Types"], |
| 283 | + parameters: [ |
| 284 | + new OA\Parameter(name: "id", description: "Add-on Type ID", in: "path", required: true, schema: new OA\Schema(type: "integer")), |
| 285 | + ], |
| 286 | + responses: [ |
| 287 | + new OA\Response(response: Response::HTTP_NO_CONTENT, description: "No Content"), |
| 288 | + new OA\Response(response: Response::HTTP_NOT_FOUND, description: "Not Found"), |
| 289 | + new OA\Response(response: Response::HTTP_UNAUTHORIZED, description: "Unauthorized"), |
| 290 | + new OA\Response(response: Response::HTTP_FORBIDDEN, description: "Forbidden"), |
| 291 | + new OA\Response(response: Response::HTTP_INTERNAL_SERVER_ERROR, description: "Server Error"), |
| 292 | + ] |
| 293 | + )] |
| 294 | + public function delete($id) |
| 295 | + { |
| 296 | + return $this->processRequest(function () use ($id) { |
| 297 | + $this->service->delete(intval($id)); |
| 298 | + return $this->deleted(); |
| 299 | + }); |
| 300 | + } |
| 301 | +} |
0 commit comments