Skip to content

Commit

Permalink
Merge pull request #12 from dtolb/delete-human
Browse files Browse the repository at this point in the history
add the ability delete humans
  • Loading branch information
dtolb authored Oct 6, 2023
2 parents 1021b5c + cdc6582 commit 7f38e85
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/controllers/HumanController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Route, Get, Post, Path, Body, OperationId, Tags, SuccessResponse, Response, Security } from 'tsoa';
import {Route, Get, Post, Path, Body, OperationId, Tags, SuccessResponse, Response, Security, Delete} from 'tsoa';

import { Controller } from '@tsoa/runtime';
import { HumansList, getHumans, getHumanById, Human, CreateHumanRequest, ErrorBody, createHuman } from '../models/Human';
import { HumansList, getHumans, deleteHumanById, getHumanById, Human, CreateHumanRequest, ErrorBody, createHuman } from '../models/Human';
import {CommonResponseHeader} from "../models/Pet";

/**
* @tsoaModel
Expand Down Expand Up @@ -62,4 +63,20 @@ export class HumanController extends Controller {
this.setStatus(201);
return createHuman(createHumanRequest);
}

/**
* Remove a human when after they adopt
* @param id id of the pet to update
*/
@Delete('{id}')
@SuccessResponse<CommonResponseHeader>('204', 'No Content')
public async deletePet(@Path() id: number): Promise<Human | null> {
const deletedHuman = deleteHumanById(id);
if (deletedHuman === null) {
this.setStatus(404);
return null;
}
return deletedHuman;
}

}
15 changes: 15 additions & 0 deletions src/models/Human.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


/**
* @tsoaModel
* @example
Expand Down Expand Up @@ -80,3 +82,16 @@ export function getHumans(): HumansList {
items: humans,
};
}

export function deleteHumanById(id: number): Human | null {
const index = humans.findIndex((p: Human) => p.id === id);
if (index >= 0 && index < humans.length) {
const pet = humans[index];
humans.splice(index, 1);
return pet;
} else {
console.error(`Invalid index: ${index}. Cannot remove pet.`);
return null;
}
}

0 comments on commit 7f38e85

Please sign in to comment.