Skip to content

Commit dc492c6

Browse files
committed
Add product delete endpoint
1 parent bc523bc commit dc492c6

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

apps/api/src/models/products/products.controller.ts

+19-9
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@ import {
66
NotFoundException,
77
Param,
88
Post,
9+
Put,
910
UseGuards,
1011
ValidationPipe,
11-
Put,
12+
Delete,
1213
} from '@nestjs/common';
1314
import { ApiTags } from '@nestjs/swagger';
14-
import mongoose, { Types } from 'mongoose';
15+
import { Types } from 'mongoose';
1516
import { BasicProductDto, CreateProductDto, ProductDto, UpdateProductDto } from 'shared-types';
1617
import { AuthenticatedGuard } from '../../auth/guards/authenticated.guard';
18+
import { ParseObjectIdPipe } from '../../pipes/prase-object-id.pipe';
1719
import { OrganizationsStatsService } from '../organizations/organizations-stats.service';
1820
import { ProductsService } from './products.service';
1921
import { Product } from './schemas/product.schema';
20-
import { ParseObjectIdPipe } from '../../pipes/prase-object-id.pipe';
2122

2223
@ApiTags('products')
2324
@Controller('products')
@@ -45,12 +46,6 @@ export class ProductsController {
4546
return Product.toDto(product);
4647
}
4748

48-
@Get('by-org/:id')
49-
async findAll(@Param('id', ParseObjectIdPipe) id: Types.ObjectId): Promise<BasicProductDto[]> {
50-
const products = await this.productsService.findAll(id);
51-
return products.map((product) => Product.toBasicDto(product));
52-
}
53-
5449
@Put(':id')
5550
async update(
5651
@Param('id', ParseObjectIdPipe) id: Types.ObjectId,
@@ -63,6 +58,21 @@ export class ProductsController {
6358
return Product.toDto(product);
6459
}
6560

61+
@Delete(':id')
62+
async delete(@Param('id', ParseObjectIdPipe) id: Types.ObjectId): Promise<ProductDto> {
63+
const product = await this.productsService.delete(id);
64+
if (!product) {
65+
throw new NotFoundException();
66+
}
67+
return Product.toDto(product);
68+
}
69+
70+
@Get('by-org/:id')
71+
async findAll(@Param('id', ParseObjectIdPipe) id: Types.ObjectId): Promise<BasicProductDto[]> {
72+
const products = await this.productsService.findAll(id);
73+
return products.map((product) => Product.toBasicDto(product));
74+
}
75+
6676
async updateTotalProductsCount(organizationId: Types.ObjectId) {
6777
const newCount = await this.productsService.countAll(organizationId);
6878
await this.organizationStatsService.updateProductsCount(organizationId, newCount);

apps/api/src/models/products/products.service.ts

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export class ProductsService {
1919
return this.productsRepository.findOneByIdAndUpdate(id, dto);
2020
}
2121

22+
delete(id: mongoose.Types.ObjectId): Promise<ProductDocument> {
23+
return this.productsRepository.deleteOneById(id);
24+
}
25+
2226
exist(id: Types.ObjectId) {
2327
return this.productsRepository.exist({ _id: id });
2428
}

0 commit comments

Comments
 (0)