Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {TodoListRepository} from '../repositories';
import {repository} from '@loopback/repository';
import {param, post, requestBody, get} from '@loopback/rest';
import {param, post, requestBody, get, HttpErrors} from '@loopback/rest';
import {TodoListImage} from '../models';

export class TodoListImageController {
Expand Down Expand Up @@ -32,6 +32,10 @@ export class TodoListImageController {
},
})
async find(@param.path.number('id') id: number): Promise<TodoListImage> {
return await this.todoListRepo.image(id).get();
const foundImage = await this.todoListRepo.image(id).get();
if (!foundImage) {
throw new HttpErrors.NotFound();
}
return foundImage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
repository,
RepositoryMixin,
Filter,
EntityNotFoundError,
} from '../..';
import {Address} from '../fixtures/models';
import {CustomerRepository, AddressRepository} from '../fixtures/repositories';
Expand Down Expand Up @@ -75,8 +74,9 @@ describe('hasOne relation', () => {
const foundAddress = await controller.findCustomerAddress(
existingCustomerId,
);
expect(foundAddress).to.containEql(address);
expect(toJSON(foundAddress)).to.deepEqual(toJSON(address));
expect(foundAddress).to.not.be.null();
expect(foundAddress!).to.containEql(address);
expect(toJSON(foundAddress!)).to.deepEqual(toJSON(address));

const persisted = await addressRepo.find({
where: {customerId: existingCustomerId},
Expand Down Expand Up @@ -104,15 +104,16 @@ describe('hasOne relation', () => {
expect(persisted[0]).to.deepEqual(foundAddress);
});

it('reports EntityNotFound error when related model is deleted', async () => {
it('returns null when related model is deleted', async () => {
const address = await controller.createCustomerAddress(existingCustomerId, {
street: '123 test avenue',
});
await addressRepo.deleteById(address.zipcode);

await expect(
controller.findCustomerAddress(existingCustomerId),
).to.be.rejectedWith(EntityNotFoundError);
const foundAddress = await controller.findCustomerAddress(
existingCustomerId,
);
expect(foundAddress).to.be.null();
});

/*---------------- HELPERS -----------------*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
constrainFilter,
EntityCrudRepository,
} from '../../repositories';
import {EntityNotFoundError} from '../../errors';

/**
* CRUD operations for a target repository of a HasMany relation
Expand All @@ -38,7 +37,7 @@ export interface HasOneRepository<Target extends Entity> {
get(
filter?: Pick<Filter<Target>, Exclude<keyof Filter<Target>, 'where'>>,
options?: Options,
): Promise<Target>;
): Promise<Target | null>;
}

export class DefaultHasOneRepository<
Expand Down Expand Up @@ -74,16 +73,14 @@ export class DefaultHasOneRepository<
Exclude<keyof Filter<TargetEntity>, 'where'>
>,
options?: Options,
): Promise<TargetEntity> {
): Promise<TargetEntity | null> {
const targetRepository = await this.getTargetRepository();
const found = await targetRepository.find(
Object.assign({limit: 1}, constrainFilter(filter, this.constraint)),
options,
);
if (found.length < 1) {
// We don't have a direct access to the foreign key value here :(
const id = 'constraint ' + JSON.stringify(this.constraint);
throw new EntityNotFoundError(targetRepository.entityClass, id);
return null;
}
return found[0];
}
Expand Down