Skip to content

Commit 74d59a0

Browse files
WIP Projects DTO
1 parent 562905b commit 74d59a0

File tree

2 files changed

+55
-13
lines changed

2 files changed

+55
-13
lines changed
Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
1-
import { PartialType } from "@nestjs/swagger"
1+
import { IsString, IsOptional, IsDate, IsObject, IsMongoId } from 'class-validator'
2+
import { ApiProperty } from '@nestjs/swagger'
3+
import { AbstractCustomFieldsDto } from '~/_common/abstracts/dto/abstract.custom-fields.dto'
24

3-
export class ProjectCreateDto {
5+
export class ProjectCreateDto extends AbstractCustomFieldsDto {
6+
@IsString()
7+
@ApiProperty()
8+
public name: string
49

10+
@IsDate()
11+
@IsOptional()
12+
@ApiProperty()
13+
public startDate?: Date
14+
15+
@IsDate()
16+
@IsOptional()
17+
@ApiProperty()
18+
public endDate?: Date
19+
20+
@IsObject()
21+
@IsOptional()
22+
@ApiProperty()
23+
public rules?: { [key: string]: any }
24+
25+
@IsString()
26+
@IsOptional()
27+
@ApiProperty()
28+
public description?: string
29+
}
30+
31+
export class ProjectDto extends ProjectCreateDto {
32+
@IsMongoId()
33+
@ApiProperty()
34+
public _id: string
535
}
636

7-
export class ProjectUpdateDto extends PartialType(ProjectCreateDto) {}
37+
export class ProjectUpdateDto extends ProjectCreateDto {}

service/src/core/projects/project.controller.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1-
import { Body, Controller, Delete, Get, HttpStatus, Param, Patch, Post, Req, Res } from '@nestjs/common'
2-
import { ProjectCreateDto, ProjectUpdateDto } from './_dto/project.dto'
1+
import { Body, Controller, Delete, Get, HttpStatus, Param, Patch, Post, Res } from '@nestjs/common'
2+
import { ProjectCreateDto, ProjectDto, ProjectUpdateDto } from './_dto/project.dto'
33
import { ProjectService } from './project.service'
44
import { AbstractController } from '~/_common/abstracts/abstract.controller'
55
import { ApiParam, ApiTags } from '@nestjs/swagger'
66
import { SearchFilterSchema, FilterSchema, SearchFilterOptions, FilterOptions, ObjectIdValidationPipe } from '@streamkits/nestjs_module_scrud'
77
import { Types } from 'mongoose'
8-
import { Request, Response } from 'express'
8+
import { Response } from 'express'
9+
import { ApiCreateDecorator } from '~/_common/decorators/api-create.decorator'
10+
import { ApiDeletedResponseDecorator } from '~/_common/decorators/api-deleted-response.decorator'
11+
import { ApiPaginatedDecorator } from '~/_common/decorators/api-paginated.decorator'
12+
import { ApiReadResponseDecorator } from '~/_common/decorators/api-read-response.decorator'
13+
import { ApiUpdateDecorator } from '~/_common/decorators/api-update.decorator'
14+
import { PickProjectionHelper } from '~/_common/helpers/pick-projection.helper'
15+
import { PartialProjectionType } from '~/_common/types/partial-projection.type'
916

1017
@ApiTags('core')
1118
@Controller('project')
1219
export class ProjectController extends AbstractController {
13-
protected readonly projection = {
20+
protected static readonly projection: PartialProjectionType<ProjectDto> = {
1421
name: 1,
1522
startDate: 1,
1623
endDate: 1,
@@ -22,7 +29,8 @@ export class ProjectController extends AbstractController {
2229
}
2330

2431
@Post()
25-
public async create(@Req() req: Request, @Res() res: Response, @Body() body: ProjectCreateDto) {
32+
@ApiCreateDecorator(ProjectCreateDto, ProjectDto)
33+
public async create(@Res() res: Response, @Body() body: ProjectCreateDto): Promise<Response> {
2634
const data = await this._service.create(body)
2735
return res.status(HttpStatus.CREATED).json({
2836
statusCode: HttpStatus.CREATED,
@@ -31,8 +39,9 @@ export class ProjectController extends AbstractController {
3139
}
3240

3341
@Get()
34-
public async search(@Res() res: Response, @SearchFilterSchema() searchFilterSchema: FilterSchema, @SearchFilterOptions() searchFilterOptions: FilterOptions) {
35-
const [data, total] = await this._service.findAndCount(searchFilterSchema, this.projection, searchFilterOptions)
42+
@ApiPaginatedDecorator(PickProjectionHelper(ProjectDto, ProjectController.projection))
43+
public async search(@Res() res: Response, @SearchFilterSchema() searchFilterSchema: FilterSchema, @SearchFilterOptions() searchFilterOptions: FilterOptions): Promise<Response> {
44+
const [data, total] = await this._service.findAndCount(searchFilterSchema, ProjectController.projection, searchFilterOptions)
3645
return res.status(HttpStatus.OK).json({
3746
statusCode: HttpStatus.OK,
3847
total,
@@ -42,7 +51,8 @@ export class ProjectController extends AbstractController {
4251

4352
@Get(':_id([0-9a-fA-F]{24})')
4453
@ApiParam({ name: '_id', type: String })
45-
public async read(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Res() res: Response) {
54+
@ApiReadResponseDecorator(ProjectDto)
55+
public async read(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Res() res: Response): Promise<Response> {
4656
const data = await this._service.findById(_id)
4757
return res.status(HttpStatus.OK).json({
4858
statusCode: HttpStatus.OK,
@@ -52,7 +62,8 @@ export class ProjectController extends AbstractController {
5262

5363
@Patch(':_id([0-9a-fA-F]{24})')
5464
@ApiParam({ name: '_id', type: String })
55-
public async update(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Body() body: ProjectUpdateDto, @Res() res: Response) {
65+
@ApiUpdateDecorator(ProjectUpdateDto, ProjectDto)
66+
public async update(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Body() body: ProjectUpdateDto, @Res() res: Response): Promise<Response> {
5667
const data = await this._service.update(_id, body)
5768
return res.status(HttpStatus.OK).json({
5869
statusCode: HttpStatus.OK,
@@ -62,7 +73,8 @@ export class ProjectController extends AbstractController {
6273

6374
@Delete(':_id([0-9a-fA-F]{24})')
6475
@ApiParam({ name: '_id', type: String })
65-
public async remove(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Res() res: Response) {
76+
@ApiDeletedResponseDecorator(ProjectDto)
77+
public async remove(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Res() res: Response): Promise<Response> {
6678
const data = await this._service.delete(_id)
6779
return res.status(HttpStatus.OK).json({
6880
statusCode: HttpStatus.OK,

0 commit comments

Comments
 (0)