Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(repository): rework *ById methods to throw if id not found #1723

Merged
merged 2 commits into from
Sep 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/site/Controller-generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ export class TodoController {
async updateById(
@param.path.number('id') id: number,
@requestBody() obj: Todo,
): Promise<boolean> {
return await this.todoRepository.updateById(id, obj);
): Promise<void> {
await this.todoRepository.updateById(id, obj);
}

@del('/todos/{id}')
async deleteById(@param.path.number('id') id: number): Promise<boolean> {
return await this.todoRepository.deleteById(id);
async deleteById(@param.path.number('id') id: number): Promise<void> {
await this.todoRepository.deleteById(id);
}
}
```
8 changes: 0 additions & 8 deletions docs/site/Model.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,6 @@ class MyFlexibleModel extends Entity {
}
```

The default response for a delete request to a non-existent resource is a `404`.
You can change this behavior to `200` by setting `strictDelete` to `false`.

```ts
@model({settings: {strictDelete: false}})
class Todo extends Entity { ... }
```

### Model Decorator

The model decorator can be used without any additional parameters, or can be
Expand Down
8 changes: 4 additions & 4 deletions docs/site/todo-tutorial-controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ export class TodoController {
async updateTodo(
@param.path.number('id') id: number,
@requestBody() todo: Todo,
): Promise<boolean> {
return await this.todoRepo.updateById(id, todo);
): Promise<void> {
await this.todoRepo.updateById(id, todo);
}

@del('/todos/{id}')
async deleteTodo(@param.path.number('id') id: number): Promise<boolean> {
return await this.todoRepo.deleteById(id);
async deleteTodo(@param.path.number('id') id: number): Promise<void> {
await this.todoRepo.deleteById(id);
}
}
```
Expand Down
14 changes: 6 additions & 8 deletions examples/todo-list/src/controllers/todo-list.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,26 @@ export class TodoListController {

@patch('/todo-lists/{id}', {
responses: {
'200': {
'204': {
description: 'TodoList PATCH success',
content: {'application/json': {'x-ts-type': Boolean}},
},
},
})
async updateById(
@param.path.number('id') id: number,
@requestBody() obj: TodoList,
): Promise<boolean> {
return await this.todoListRepository.updateById(id, obj);
): Promise<void> {
await this.todoListRepository.updateById(id, obj);
}

@del('/todo-lists/{id}', {
responses: {
'200': {
'204': {
description: 'TodoList DELETE success',
content: {'application/json': {'x-ts-type': Boolean}},
},
},
})
async deleteById(@param.path.number('id') id: number): Promise<boolean> {
return await this.todoListRepository.deleteById(id);
async deleteById(@param.path.number('id') id: number): Promise<void> {
await this.todoListRepository.deleteById(id);
}
}
21 changes: 9 additions & 12 deletions examples/todo-list/src/controllers/todo.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,43 +56,40 @@ export class TodoController {

@put('/todos/{id}', {
responses: {
'200': {
'204': {
description: 'Todo PUT success',
content: {'application/json': {'x-ts-type': Boolean}},
},
},
})
async replaceTodo(
@param.path.number('id') id: number,
@requestBody() todo: Todo,
): Promise<boolean> {
return await this.todoRepo.replaceById(id, todo);
): Promise<void> {
await this.todoRepo.replaceById(id, todo);
}

@patch('/todos/{id}', {
responses: {
'200': {
'204': {
description: 'Todo PATCH success',
content: {'application/json': {'x-ts-type': Boolean}},
},
},
})
async updateTodo(
@param.path.number('id') id: number,
@requestBody() todo: Todo,
): Promise<boolean> {
return await this.todoRepo.updateById(id, todo);
): Promise<void> {
await this.todoRepo.updateById(id, todo);
}

@del('/todos/{id}', {
responses: {
'200': {
'204': {
description: 'Todo DELETE success',
content: {'application/json': {'x-ts-type': Boolean}},
},
},
})
async deleteTodo(@param.path.number('id') id: number): Promise<boolean> {
return await this.todoRepo.deleteById(id);
async deleteTodo(@param.path.number('id') id: number): Promise<void> {
await this.todoRepo.deleteById(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {Todo, TodoList} from '../../src/models/';
import {TodoRepository, TodoListRepository} from '../../src/repositories/';
import {givenTodo, givenTodoList} from '../helpers';

describe('Application', () => {
describe('TodoListApplication', () => {
let app: TodoListApplication;
let client: supertest.SuperTest<supertest.Test>;
let todoRepo: TodoRepository;
Expand Down
15 changes: 11 additions & 4 deletions examples/todo-list/test/acceptance/todo-list.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {TodoList} from '../../src/models/';
import {TodoListRepository} from '../../src/repositories/';
import {givenTodoList} from '../helpers';

describe('Application', () => {
describe('TodoListApplication', () => {
let app: TodoListApplication;
let client: supertest.SuperTest<supertest.Test>;
let todoListRepo: TodoListRepository;
Expand Down Expand Up @@ -92,7 +92,7 @@ describe('Application', () => {
expect(result.body).to.deepEqual(expected);
});

it('returns 404 when a todo does not exist', () => {
it('returns 404 when getting a todo-list that does not exist', () => {
return client.get('/todo-lists/99999').expect(404);
});

Expand All @@ -103,16 +103,23 @@ describe('Application', () => {
await client
.patch(`/todo-lists/${persistedTodoList.id}`)
.send(updatedTodoList)
.expect(200);
.expect(204);
const result = await todoListRepo.findById(persistedTodoList.id);
expect(result).to.containEql(updatedTodoList);
});

it('returns 404 when updating a todo-list that does not exist', () => {
return client
.patch('/todos/99999')
.send(givenTodoList())
.expect(404);
});

it('deletes a todoList by ID', async () => {
await client
.del(`/todo-lists/${persistedTodoList.id}`)
.send()
.expect(200);
.expect(204);
await expect(
todoListRepo.findById(persistedTodoList.id),
).to.be.rejectedWith(EntityNotFoundError);
Expand Down
28 changes: 23 additions & 5 deletions examples/todo-list/test/acceptance/todo.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {Todo} from '../../src/models/';
import {TodoRepository} from '../../src/repositories/';
import {givenTodo} from '../helpers';

describe('Application', () => {
describe('TodoListApplication', () => {
let app: TodoListApplication;
let client: supertest.SuperTest<supertest.Test>;
let todoRepo: TodoRepository;
Expand Down Expand Up @@ -64,7 +64,7 @@ describe('Application', () => {
expect(result.body).to.deepEqual(expected);
});

it('returns 404 when a todo does not exist', () => {
it('returns 404 when getting a todo that does not exist', () => {
return client.get('/todos/99999').expect(404);
});

Expand All @@ -77,11 +77,18 @@ describe('Application', () => {
await client
.put(`/todos/${persistedTodo.id}`)
.send(updatedTodo)
.expect(200);
.expect(204);
const result = await todoRepo.findById(persistedTodo.id);
expect(result).to.containEql(updatedTodo);
});

it('returns 404 when replacing a todo that does not exist', () => {
return client
.put('/todos/99999')
.send(givenTodo())
.expect(404);
});

it('updates the todo by ID ', async () => {
const updatedTodo = givenTodo({
title: 'DO SOMETHING AWESOME',
Expand All @@ -90,20 +97,31 @@ describe('Application', () => {
await client
.patch(`/todos/${persistedTodo.id}`)
.send(updatedTodo)
.expect(200);
.expect(204);
const result = await todoRepo.findById(persistedTodo.id);
expect(result).to.containEql(updatedTodo);
});

it('returns 404 when updating a todo that does not exist', () => {
return client
.patch('/todos/99999')
.send(givenTodo())
.expect(404);
});

it('deletes the todo', async () => {
await client
.del(`/todos/${persistedTodo.id}`)
.send()
.expect(200);
.expect(204);
await expect(todoRepo.findById(persistedTodo.id)).to.be.rejectedWith(
EntityNotFoundError,
);
});

it('returns 404 when deleting a todo that does not exist', async () => {
await client.del(`/todos/99999`).expect(404);
});
});

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,11 @@ describe('TodoController', () => {

describe('updateById', () => {
it('successfully updates existing items', async () => {
updateById.resolves(true);
expect(
await controller.updateById(
aTodoListWithId.id as number,
aTodoListToPatchTo,
),
).to.eql(true);
updateById.resolves();
await controller.updateById(
aTodoListWithId.id as number,
aTodoListToPatchTo,
);
sinon.assert.calledWith(
updateById,
aTodoListWithId.id,
Expand All @@ -117,10 +115,8 @@ describe('TodoController', () => {

describe('deleteById', () => {
it('successfully deletes existing items', async () => {
deleteById.resolves(true);
expect(await controller.deleteById(aTodoListWithId.id as number)).to.eql(
true,
);
deleteById.resolves();
await controller.deleteById(aTodoListWithId.id as number);
sinon.assert.calledWith(deleteById, aTodoListWithId.id);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,30 +82,24 @@ describe('TodoController', () => {

describe('replaceTodo', () => {
it('successfully replaces existing items', async () => {
replaceById.resolves(true);
expect(
await controller.replaceTodo(aTodoWithId.id as number, aChangedTodo),
).to.eql(true);
replaceById.resolves();
await controller.replaceTodo(aTodoWithId.id as number, aChangedTodo);
sinon.assert.calledWith(replaceById, aTodoWithId.id, aChangedTodo);
});
});

describe('updateTodo', () => {
it('successfully updates existing items', async () => {
updateById.resolves(true);
expect(
await controller.updateTodo(aTodoWithId.id as number, aChangedTodo),
).to.eql(true);
updateById.resolves();
await controller.updateTodo(aTodoWithId.id as number, aChangedTodo);
sinon.assert.calledWith(updateById, aTodoWithId.id, aChangedTodo);
});
});

describe('deleteTodo', () => {
it('successfully deletes existing items', async () => {
deleteById.resolves(true);
expect(await controller.deleteTodo(aTodoWithId.id as number)).to.eql(
true,
);
deleteById.resolves();
await controller.deleteTodo(aTodoWithId.id as number);
sinon.assert.calledWith(deleteById, aTodoWithId.id);
});
});
Expand Down
21 changes: 9 additions & 12 deletions examples/todo/src/controllers/todo.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,43 +69,40 @@ export class TodoController {

@put('/todos/{id}', {
responses: {
'200': {
'204': {
description: 'Todo PUT success',
content: {'application/json': {'x-ts-type': Boolean}},
},
},
})
async replaceTodo(
@param.path.number('id') id: number,
@requestBody() todo: Todo,
): Promise<boolean> {
return await this.todoRepo.replaceById(id, todo);
): Promise<void> {
await this.todoRepo.replaceById(id, todo);
}

@patch('/todos/{id}', {
responses: {
'200': {
'204': {
description: 'Todo PATCH success',
content: {'application/json': {'x-ts-type': Boolean}},
},
},
})
async updateTodo(
@param.path.number('id') id: number,
@requestBody() todo: Todo,
): Promise<boolean> {
return await this.todoRepo.updateById(id, todo);
): Promise<void> {
await this.todoRepo.updateById(id, todo);
}

@del('/todos/{id}', {
responses: {
'200': {
'204': {
description: 'Todo DELETE success',
content: {'application/json': {'x-ts-type': Boolean}},
},
},
})
async deleteTodo(@param.path.number('id') id: number): Promise<boolean> {
return await this.todoRepo.deleteById(id);
async deleteTodo(@param.path.number('id') id: number): Promise<void> {
await this.todoRepo.deleteById(id);
}
}
Loading