Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project & Template APIs #3

Merged
merged 12 commits into from
Oct 3, 2022
Next Next commit
feat: Added API to create project
  • Loading branch information
chavda-bhavik committed Oct 3, 2022
commit 720ba1b61b0d34de869235ad73a41cf988a59e5a
4 changes: 2 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ module.exports = {
'@typescript-eslint/explicit-function-return-type': 'off',
'react/jsx-closing-bracket-location': 'off',
'@typescript-eslint/no-var-requires': 'off',
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': ['off'],
'no-unused-vars': 'error',
'@typescript-eslint/no-unused-vars': ['error'],
'mocha/no-mocha-arrows': 'off',
'@typescript-eslint/default-param-last': 'off',
'no-return-await': 'off',
Expand Down
25 changes: 25 additions & 0 deletions apps/api/src/app/project/dtos/create-project-response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IsDefined, IsString } from 'class-validator';

export class CreateProjectResponseDto {
@ApiPropertyOptional({
description: 'Id of the project',
})
@IsString()
@IsDefined()
_id?: string;

@ApiProperty({
description: 'Name of the project',
})
@IsString()
@IsDefined()
name: string;

@ApiProperty({
description: 'Code of the project',
})
@IsString()
@IsDefined()
code: string;
}
22 changes: 22 additions & 0 deletions apps/api/src/app/project/dtos/create-project.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsDefined, IsString, Validate } from 'class-validator';
import { UniqueValidator } from '../../shared/framework/IsUniqueValidator';

export class CreateProjectDto {
@ApiProperty({
description: 'Name of the project',
})
@IsString()
@IsDefined()
name: string;

@ApiProperty({
description: 'Code of the project',
})
@IsString()
@IsDefined()
@Validate(UniqueValidator, ['Project', 'code'], {
message: 'Code is already taken',
})
code: string;
}
26 changes: 23 additions & 3 deletions apps/api/src/app/project/project.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { Controller, Get } from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { Body, Controller, Get, Post } from '@nestjs/common';
import { ApiOperation, ApiTags, ApiOkResponse } from '@nestjs/swagger';
import { CreateProjectDto } from './dtos/create-project.dto';
import { ProjectResponseDto } from './dtos/projects-response.dto';
import { GetProjects } from './usecases/get-projects/get-projects.usecase';
import { CreateProject } from './usecases/create-project/create-project.usecase';
import { CreateProjectCommand } from './usecases/create-project/create-project.command';
import { CreateProjectResponseDto } from './dtos/create-project-response.dto';

@Controller('/project')
@ApiTags('Project')
export class ProjectController {
constructor(private getProjectsUsecase: GetProjects) {}
constructor(private getProjectsUsecase: GetProjects, private createProjectUsecase: CreateProject) {}

@Get('')
@ApiOperation({
Expand All @@ -15,4 +19,20 @@ export class ProjectController {
getProjects(): Promise<ProjectResponseDto[]> {
return this.getProjectsUsecase.execute();
}

@Post('')
@ApiOperation({
summary: 'Create project',
})
@ApiOkResponse({
type: [CreateProjectResponseDto],
})
createProject(@Body() body: CreateProjectDto): Promise<CreateProjectResponseDto> {
return this.createProjectUsecase.execute(
CreateProjectCommand.create({
code: body.code,
name: body.name,
})
);
}
}
5 changes: 3 additions & 2 deletions apps/api/src/app/project/project.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Module } from '@nestjs/common';
import { USE_CASES } from './usecases';
import { ProjectController } from './project.controller';
import { SharedModule } from '../shared/shared.module';
import { ProjectController } from './project.controller';
import { UniqueValidator } from '../shared/framework/IsUniqueValidator';

@Module({
imports: [SharedModule],
providers: [...USE_CASES],
providers: [...USE_CASES, UniqueValidator],
controllers: [ProjectController],
})
export class ProjectModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { IsDefined, IsString } from 'class-validator';
import { BaseCommand } from '../../../shared/commands/base.command';

export class CreateProjectCommand extends BaseCommand {
@IsDefined()
@IsString()
name: string;

@IsDefined()
@IsString()
code: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Injectable } from '@nestjs/common';
import { ProjectRepository } from '@impler/dal';
import { CreateProjectCommand } from './create-project.command';

@Injectable()
export class CreateProject {
constructor(private projectRepository: ProjectRepository) {}

async execute(command: CreateProjectCommand) {
return this.projectRepository.create(command);
}
}
2 changes: 2 additions & 0 deletions apps/api/src/app/project/usecases/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { CreateProject } from './create-project/create-project.usecase';
import { GetProjects } from './get-projects/get-projects.usecase';

export const USE_CASES = [
GetProjects,
CreateProject,
//
];
21 changes: 21 additions & 0 deletions apps/api/src/app/shared/commands/base.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { plainToClass } from 'class-transformer';
import { validateSync } from 'class-validator';
import { BadRequestException, flatten } from '@nestjs/common';

export abstract class BaseCommand {
static create<T extends BaseCommand>(this: new (...args: any[]) => T, data: T): T {
const convertedObject = plainToClass<T, any>(this, {
...data,
});

const errors = validateSync(convertedObject as unknown as object);
if (errors?.length) {
const mappedErrors = flatten(errors.map((item) => Object.values(item.constraints)));

throw new BadRequestException(mappedErrors);
}

return convertedObject;
}
}
21 changes: 21 additions & 0 deletions apps/api/src/app/shared/framework/IsUniqueValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ValidationArguments, ValidatorConstraint, ValidatorConstraintInterface } from 'class-validator';
import { CommonRepository, ProjectEntity } from '@impler/dal';

@ValidatorConstraint({ name: 'IsUniqueUser', async: true })
export class UniqueValidator implements ValidatorConstraintInterface {
private commonRepository: CommonRepository;
constructor() {
this.commonRepository = new CommonRepository();
}

async validate(value: any, args: ValidationArguments) {
const [modelName, field] = args.constraints;
const count = await this.commonRepository.count<ProjectEntity>(modelName, { [field]: value });

return !count;
}

defaultMessage(args: ValidationArguments) {
return `${args.value} is already taken`;
}
}
1 change: 1 addition & 0 deletions libs/dal/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './dal.service';
export * from './repositories/project';
export * from './repositories/common';
14 changes: 14 additions & 0 deletions libs/dal/src/repositories/common/common.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { FilterQuery, models } from 'mongoose';

export class CommonRepository {
async count<T>(name: string, query: FilterQuery<T>): Promise<number> {
const model = models[name];
if (model) {
const count = await model.count(query);

return count;
}

throw new Error(`Model ${name} does not exists`);
}
}
1 change: 1 addition & 0 deletions libs/dal/src/repositories/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './common.repository';