Skip to content

Commit f46c67d

Browse files
authored
Merge pull request #43 from nicobytes/master
New
2 parents 3a13b2e + d249a6a commit f46c67d

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

.eslintrc.js eslint.config.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
module.exports = {
1+
const eslint = require('@eslint/js');
2+
const tseslint = require('typescript-eslint');
3+
4+
module.exports = tseslint.config({
25
parser: '@typescript-eslint/parser',
36
parserOptions: {
47
project: 'tsconfig.json',
@@ -22,4 +25,4 @@ module.exports = {
2225
'@typescript-eslint/explicit-module-boundary-types': 'off',
2326
'@typescript-eslint/no-explicit-any': 'off',
2427
},
25-
};
28+
});

src/controllers/products.controller.ts

+10
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,21 @@ export class ProductsController {
3131
return this.productsService.findById(id);
3232
}
3333

34+
@Get(':id/related')
35+
getRelatedProducts(@Param('id', ParseIntPipe) id: number) {
36+
return this.productsService.getRelatedProducts(id);
37+
}
38+
3439
@Get('slug/:slug')
3540
getProductBySlug(@Param('slug') slug: string) {
3641
return this.productsService.findBySlug(slug);
3742
}
3843

44+
@Get('slug/:slug/related')
45+
getRelatedProductsBySlug(@Param('slug') slug: string) {
46+
return this.productsService.getRelatedProductsBySlug(slug);
47+
}
48+
3949
@Post()
4050
create(@Body() product: CreateProductDto) {
4151
return this.productsService.create(product);

src/services/products.service.ts

+18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
FindManyOptions,
88
Like,
99
And,
10+
Not,
1011
} from 'typeorm';
1112

1213
import { Product } from '@db/entities/product.entity';
@@ -95,13 +96,30 @@ export class ProductsService {
9596
});
9697
}
9798

99+
async getRelatedProducts(id: number) {
100+
const product = await this.findById(id);
101+
102+
return this.productsRepo.find({
103+
relations: ['category'],
104+
where: {
105+
category: { id: product.category.id },
106+
id: Not(id),
107+
},
108+
});
109+
}
110+
98111
findBySlug(slug: string) {
99112
return this.productsRepo.findOneOrFail({
100113
relations: ['category'],
101114
where: { slug },
102115
});
103116
}
104117

118+
async getRelatedProductsBySlug(slug: string) {
119+
const product = await this.findBySlug(slug);
120+
return this.getRelatedProducts(product.id);
121+
}
122+
105123
async update(id: Product['id'], changes: UpdateProductDto) {
106124
const product = await this.findById(id);
107125
const images = changes.images.join(',') || product.images;

0 commit comments

Comments
 (0)