Skip to content

Commit

Permalink
Merge pull request #9 from dtolb/delete-pet
Browse files Browse the repository at this point in the history
delete pet no annotations
  • Loading branch information
dtolb authored Sep 8, 2023
2 parents 62f4be9 + 84e428a commit e0e3606
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 1 deletion.
120 changes: 120 additions & 0 deletions spec/petStore.json
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,126 @@
"tags": [
"Write"
]
},
"delete": {
"operationId": "DeletePet",
"description": "Remove a pet when its adopted",
"parameters": [
{
"name": "id",
"in": "path",
"description": "id of the pet to update",
"required": true,
"schema": {
"type": "number",
"format": "double"
}
}
],
"responses": {
"204": {
"description": "No Content",
"headers": {
"CommonResponseHeader": {
"schema": {
"$ref": "#/components/schemas/CommonResponseHeader"
},
"description": "Common Headers Returned"
}
},
"content": {
"application/json": {
"schema": {
"allOf": [
{
"$ref": "#/components/schemas/Pet"
}
],
"nullable": true
}
}
}
},
"401": {
"description": "Unauthorized",
"headers": {
"CommonResponseHeader": {
"schema": {
"$ref": "#/components/schemas/CommonResponseHeader"
},
"description": "Common Headers Returned"
}
},
"content": {
"application/json": {
"schema": {
"allOf": [
{
"$ref": "#/components/schemas/ErrorBody"
},
{
"properties": {
"message": {
"type": "string",
"enum": [
"User is not authorized"
],
"nullable": false
}
},
"required": [
"message"
],
"type": "object"
}
]
}
}
}
},
"403": {
"description": "Forbidden",
"headers": {
"CommonResponseHeader": {
"schema": {
"$ref": "#/components/schemas/CommonResponseHeader"
},
"description": "Common Headers Returned"
}
},
"content": {
"application/json": {
"schema": {
"allOf": [
{
"$ref": "#/components/schemas/ErrorBody"
},
{
"properties": {
"message": {
"type": "string",
"enum": [
"User is forbidden to access the resource"
],
"nullable": false
}
},
"required": [
"message"
],
"type": "object"
}
]
}
}
}
}
},
"security": [
{
"api_key": []
}
]
}
}
},
Expand Down
18 changes: 17 additions & 1 deletion src/controllers/PetController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Route, Put, Get, Post, Path, Body, OperationId, Tags, SuccessResponse, Response, Security, Query } from 'tsoa';
import { Route, Put, Get, Post, Delete, Path, Body, OperationId, Tags, SuccessResponse, Response, Security, Query } from 'tsoa';
import {
Pet,
createPet,
Expand All @@ -12,6 +12,7 @@ import {
CommonResponseHeader,
ErrorBody,
ListPetsHeaders,
deletePetById,
} from '../models/Pet';
import { Controller } from '@tsoa/runtime';

Expand Down Expand Up @@ -86,4 +87,19 @@ export class PetController extends Controller {
}
return updatedPet;
}

/**
* Remove a pet when its adopted
* @param id id of the pet to update
*/
@Delete('{id}')
@SuccessResponse<CommonResponseHeader>('204', 'No Content')
public async deletePet(@Path() id: number): Promise<Pet | null> {
const deletedPet = deletePetById(id);
if (deletedPet === null) {
this.setStatus(404);
return null;
}
return deletedPet;
}
}
12 changes: 12 additions & 0 deletions src/models/Pet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,15 @@ export function getPets(pagination: PaginationRequest): PetsList {
items: pets,
};
}

export function deletePetById(id: number): Pet | null {
const index = pets.findIndex((p: Pet) => p.id === id);
if (index >= 0 && index < pets.length) {
const pet = pets[index];
pets.splice(index, 1);
return pet;
} else {
console.error(`Invalid index: ${index}. Cannot remove pet.`);
return null;
}
}
27 changes: 27 additions & 0 deletions src/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,33 @@ export function RegisterRoutes(app: Router) {
},
);
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
app.delete(
'/v1/pets/:id',
authenticateMiddleware([{ api_key: [] }]),
...fetchMiddlewares<RequestHandler>(PetController),
...fetchMiddlewares<RequestHandler>(PetController.prototype.deletePet),

function PetController_deletePet(request: any, response: any, next: any) {
const args = {
id: { in: 'path', name: 'id', required: true, dataType: 'double' },
};

// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa

let validatedArgs: any[] = [];
try {
validatedArgs = getValidatedArgs(args, request, response);

const controller = new PetController();

const promise = controller.deletePet.apply(controller, validatedArgs as any);
promiseHandler(controller, promise, response, 204, next);
} catch (err) {
return next(err);
}
},
);
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa

// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa

Expand Down

0 comments on commit e0e3606

Please sign in to comment.