-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create actions controller and documentations
- Loading branch information
1 parent
8d3385d
commit ee6e0fc
Showing
6 changed files
with
184 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,61 @@ | ||
import { Controller } from '@nestjs/common'; | ||
import { | ||
Body, | ||
Controller, | ||
HttpCode, | ||
HttpStatus, | ||
Param, | ||
Post, | ||
UseInterceptors, | ||
} from '@nestjs/common'; | ||
import { | ||
ApiBody, | ||
ApiNotFoundResponse, | ||
ApiOkResponse, | ||
ApiOperation, | ||
ApiTags, | ||
} from '@nestjs/swagger'; | ||
import { createSchema } from '../utils/createSchema'; | ||
import { ErrorInterceptor } from '../middleware/errorInterceptor.middleware'; | ||
import { ActionsService } from './actions.service'; | ||
import AddDTO from './dto/add.dto'; | ||
import BatchDTO from './dto/batch.dto'; | ||
import { ADD_BATCH_EXAMPLE, ADD_EXAMPLE } from './examples'; | ||
|
||
@Controller('actions') | ||
export class ActionsController {} | ||
@Controller('storage') | ||
@ApiTags('Actions') | ||
export class ActionsController { | ||
constructor(private readonly actions: ActionsService) {} | ||
|
||
@Post('/:storageId/add') | ||
@UseInterceptors(ErrorInterceptor) | ||
@ApiBody({ type: AddDTO }) | ||
@ApiOkResponse({ | ||
description: 'Added ingredient with updated values.', | ||
schema: createSchema(ADD_EXAMPLE), | ||
}) | ||
@ApiNotFoundResponse() | ||
@ApiOperation({ summary: 'Add single ingredient to storage' }) | ||
@HttpCode(HttpStatus.OK) | ||
async add(@Param('storageId') storageId: string, @Body() body: AddDTO) { | ||
const ingredient = await this.actions.addIngredient(body, storageId); | ||
return { success: true, data: ingredient }; | ||
} | ||
|
||
@Post('/:storageId/batch') | ||
@UseInterceptors(ErrorInterceptor) | ||
@ApiBody({ type: BatchDTO }) | ||
@ApiOkResponse({ | ||
description: 'Array of added ingredients with updated values.', | ||
schema: createSchema(ADD_BATCH_EXAMPLE), | ||
}) | ||
@ApiNotFoundResponse() | ||
@ApiOperation({ summary: 'Add batch of ingredients to storage' }) | ||
@HttpCode(HttpStatus.OK) | ||
async addBatch( | ||
@Param('storageId') storageId: string, | ||
@Body() body: BatchDTO, | ||
) { | ||
const batch = await this.actions.addIngredientsBatch(body, storageId); | ||
return { success: true, data: batch }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsArray } from 'class-validator'; | ||
import { Type } from 'class-transformer'; | ||
import { ArrayMinSize, IsArray, ValidateNested } from 'class-validator'; | ||
import AddDTO from './add.dto'; | ||
|
||
export default class BatchDTO { | ||
@ApiProperty({ description: 'Array of ingredients' }) | ||
@IsArray({ each: true }) | ||
@ApiProperty({ description: 'Array of ingredients', type: () => [AddDTO] }) | ||
@IsArray() | ||
@ValidateNested({ each: true }) | ||
@ArrayMinSize(1) | ||
@Type(() => AddDTO) | ||
ingredients: AddDTO[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export const ADD_EXAMPLE = { | ||
_id: '631f11ec91ff9645f0690fa3', | ||
name: 'Kiwi', | ||
unit: 'KG', | ||
amount: 50, | ||
storage: '631d9d675237167ab2c1b75e', | ||
createdAt: '2022-09-12T11:03:08.441Z', | ||
}; | ||
|
||
export const ADD_BATCH_EXAMPLE = [ | ||
{ | ||
_id: '631f11ec91ff9645f0690fa3', | ||
name: 'Kiwi', | ||
unit: 'KG', | ||
amount: 50, | ||
storage: '631d9d675237167ab2c1b75e', | ||
createdAt: '2022-09-12T11:03:08.441Z', | ||
}, | ||
{ | ||
_id: '631f1373d5e4af4a1f3fb1aa', | ||
name: 'Cucumber', | ||
unit: 'G', | ||
amount: 500, | ||
storage: '631d9d675237167ab2c1b75e', | ||
createdAt: '2022-09-12T11:09:39.615Z', | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters