Skip to content

Commit

Permalink
Add update delete endpoints to warehouses
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBartusek committed Dec 2, 2023
1 parent dc492c6 commit 95a4946
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
38 changes: 34 additions & 4 deletions apps/api/src/models/warehouses/warehouses.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { Controller, Get, NotFoundException, Param } from '@nestjs/common';
import { WarehouseDto } from 'shared-types';
import {
Body,
Controller,
Delete,
Get,
NotFoundException,
Param,
Post,
ValidationPipe,
} from '@nestjs/common';
import { Types } from 'mongoose';
import { UpdateWarehouseDto, WarehouseDto } from 'shared-types';
import { ParseObjectIdPipe } from '../../pipes/prase-object-id.pipe';
import { Warehouse } from './schemas/warehouse.schema';
import { WarehousesService } from './warehouses.service';
import { ParseObjectIdPipe } from '../../pipes/prase-object-id.pipe';
import { Types } from 'mongoose';

@Controller('warehouses')
export class WarehousesController {
Expand All @@ -17,4 +26,25 @@ export class WarehousesController {
}
return Warehouse.toDto(warehouse);
}

@Post(':id')
async update(
@Param('id', ParseObjectIdPipe) id: Types.ObjectId,
@Body(ValidationPipe) dto: UpdateWarehouseDto,
): Promise<WarehouseDto> {
const warehouse = await this.warehousesService.update(id, dto);
if (!warehouse) {
throw new NotFoundException();
}
return Warehouse.toDto(warehouse);
}

@Delete(':id')
async delete(@Param('id', ParseObjectIdPipe) id: Types.ObjectId): Promise<WarehouseDto> {
const warehouse = await this.warehousesService.delete(id);
if (!warehouse) {
throw new NotFoundException();
}
return Warehouse.toDto(warehouse);
}
}
10 changes: 9 additions & 1 deletion apps/api/src/models/warehouses/warehouses.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common';
import { CreateWarehouseDto } from 'shared-types';
import { CreateWarehouseDto, UpdateWarehouseDto } from 'shared-types';
import { WarehouseDocument } from './schemas/warehouse.schema';
import { WarehouseRepository } from './warehouse.repository';
import { Types } from 'mongoose';
Expand All @@ -15,6 +15,14 @@ export class WarehousesService {
});
}

update(id: Types.ObjectId, dto: UpdateWarehouseDto): Promise<WarehouseDocument | undefined> {
return this.warehouseRepository.findOneByIdAndUpdate(id, dto);
}

delete(id: Types.ObjectId): Promise<WarehouseDocument | undefined> {
return this.warehouseRepository.deleteOneById(id);
}

exist(id: Types.ObjectId) {
return this.warehouseRepository.exist({ _id: id });
}
Expand Down
4 changes: 3 additions & 1 deletion packages/shared-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { InventoryItemDto } from './inventory/InventoryItemDto'
import { AddInventoryItemDto } from './inventory/AddInventoryItemDto'
import { PatchOrganizationSettingsDto } from './organizations/PatchOrganizationSettingsDto'
import { UpdateProductDto } from './product/UpdateProductDto'
import { UpdateWarehouseDto } from './warehouse/UpdateWarehouseDto'

export {
BasicProductDto,
Expand All @@ -37,5 +38,6 @@ export {
InventoryItemDto,
AddInventoryItemDto,
PatchOrganizationSettingsDto,
UpdateProductDto
UpdateProductDto,
UpdateWarehouseDto
}
3 changes: 3 additions & 0 deletions packages/shared-types/src/warehouse/UpdateWarehouseDto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { CreateWarehouseDto } from "./CreateWarehouseDto"

export class UpdateWarehouseDto extends CreateWarehouseDto { }

0 comments on commit 95a4946

Please sign in to comment.