From 76e5b841ebded150fed6fb8534f93945e9516e5a Mon Sep 17 00:00:00 2001 From: Pavel Strunkin Date: Sun, 12 Jul 2020 12:02:59 +0200 Subject: [PATCH 1/3] Project. update endpoint added --- src/projects/dto/create-project.dto.ts | 10 +++------- src/projects/dto/project.dto.ts | 23 +++++++++++++++++++++++ src/projects/projects.controller.ts | 26 ++++++++++++++------------ src/projects/projects.service.ts | 16 ++++++++++++++-- 4 files changed, 54 insertions(+), 21 deletions(-) create mode 100644 src/projects/dto/project.dto.ts diff --git a/src/projects/dto/create-project.dto.ts b/src/projects/dto/create-project.dto.ts index 724e5cc1..7234013a 100644 --- a/src/projects/dto/create-project.dto.ts +++ b/src/projects/dto/create-project.dto.ts @@ -1,8 +1,4 @@ -import { ApiProperty } from '@nestjs/swagger'; -import { IsString } from 'class-validator'; +import { PickType } from '@nestjs/swagger'; +import { ProjectDto } from './project.dto'; -export class CreateProjectDto { - @ApiProperty() - @IsString() - readonly name: string; -} +export class CreateProjectDto extends PickType(ProjectDto, ['name', 'mainBranchName']) {} diff --git a/src/projects/dto/project.dto.ts b/src/projects/dto/project.dto.ts new file mode 100644 index 00000000..301beb64 --- /dev/null +++ b/src/projects/dto/project.dto.ts @@ -0,0 +1,23 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsUUID, IsString } from 'class-validator'; +import { Project } from '@prisma/client'; + +export class ProjectDto implements Project { + @ApiProperty() + @IsUUID() + readonly id: string; + + @ApiProperty() + @IsString() + readonly name: string; + + @ApiProperty() + @IsString() + readonly mainBranchName: string; + + @ApiProperty() + readonly createdAt: Date; + + @ApiProperty() + readonly updatedAt: Date; +} diff --git a/src/projects/projects.controller.ts b/src/projects/projects.controller.ts index 338c91af..b049c2a6 100644 --- a/src/projects/projects.controller.ts +++ b/src/projects/projects.controller.ts @@ -1,18 +1,10 @@ -import { - Controller, - Get, - UseGuards, - Body, - Post, - Param, - ParseUUIDPipe, - Delete, -} from '@nestjs/common'; +import { Controller, Get, UseGuards, Body, Post, Param, ParseUUIDPipe, Delete, Put } from '@nestjs/common'; import { ApiTags, ApiBearerAuth, ApiOkResponse, ApiParam } from '@nestjs/swagger'; import { JwtAuthGuard } from '../auth/guards/auth.guard'; import { ProjectsService } from './projects.service'; import { CreateProjectDto } from './dto/create-project.dto'; import { Project } from '@prisma/client'; +import { ProjectDto } from './dto/project.dto'; @Controller('projects') @ApiTags('projects') @@ -22,6 +14,7 @@ export class ProjectsController { @Get() @ApiBearerAuth() @UseGuards(JwtAuthGuard) + @ApiOkResponse({ type: [ProjectDto] }) getAll(): Promise { return this.projectsService.findAll(); } @@ -29,15 +22,24 @@ export class ProjectsController { @Post() @ApiBearerAuth() @UseGuards(JwtAuthGuard) + @ApiOkResponse({ type: ProjectDto }) create(@Body() createProjectDto: CreateProjectDto): Promise { return this.projectsService.create(createProjectDto); } + @Put() + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @ApiOkResponse({ type: ProjectDto }) + update(@Body() projectDto: ProjectDto): Promise { + return this.projectsService.update(projectDto); + } + @Delete(':id') - @ApiParam({ name: 'id', required: true }) @ApiBearerAuth() @UseGuards(JwtAuthGuard) - @ApiOkResponse({ type: Number }) + @ApiOkResponse({ type: ProjectDto }) + @ApiParam({ name: 'id', required: true }) remove(@Param('id', new ParseUUIDPipe()) id: string): Promise { return this.projectsService.remove(id); } diff --git a/src/projects/projects.service.ts b/src/projects/projects.service.ts index 23d2f99e..7cf65699 100644 --- a/src/projects/projects.service.ts +++ b/src/projects/projects.service.ts @@ -4,6 +4,7 @@ import { BuildsService } from '../builds/builds.service'; import { TestVariationsService } from '../test-variations/test-variations.service'; import { PrismaService } from '../prisma/prisma.service'; import { Project } from '@prisma/client'; +import { ProjectDto } from './dto/project.dto'; @Injectable() export class ProjectsService { @@ -21,6 +22,17 @@ export class ProjectsService { return this.prismaService.project.create({ data: { name: createProjectDto.name, + mainBranchName: createProjectDto.mainBranchName, + }, + }); + } + + async update(projectDto: ProjectDto): Promise { + return this.prismaService.project.update({ + where: { id: projectDto.id }, + data: { + name: projectDto.name, + mainBranchName: projectDto.mainBranchName, }, }); } @@ -35,9 +47,9 @@ export class ProjectsService { }); try { - await Promise.all(project.builds.map((build) => this.buildsService.remove(build.id))); + await Promise.all(project.builds.map(build => this.buildsService.remove(build.id))); await Promise.all( - project.testVariations.map((testVariation) => this.testVariationsService.remove(testVariation.id)) + project.testVariations.map(testVariation => this.testVariationsService.remove(testVariation.id)) ); } catch (err) { console.log(err); From 2656064a2aa15e53050591fbb03efab812f162c4 Mon Sep 17 00:00:00 2001 From: Pavel Strunkin Date: Sun, 12 Jul 2020 16:24:22 +0200 Subject: [PATCH 2/3] Update seed.ts --- prisma/seed.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/prisma/seed.ts b/prisma/seed.ts index 1ca933fc..47d26c33 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -60,8 +60,9 @@ async function createDefaultProject() { console.log('## CREATING DEFAULT PROJECT ##'); console.log('##############################'); console.log(''); - console.log(`Project name ${project.name}`); console.log(`Project key: ${project.id}`); + console.log(`Project name ${project.name}`); + console.log(`Project name ${project.mainBranchName}`); }); } } From a3ca2bf86e1b4863a23c0db992363f25552f2b85 Mon Sep 17 00:00:00 2001 From: Pavel Strunkin Date: Sun, 12 Jul 2020 16:32:21 +0200 Subject: [PATCH 3/3] project. e2e tests updated --- test/builds.e2e-spec.ts | 2 +- test/projects.e2e-spec.ts | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/test/builds.e2e-spec.ts b/test/builds.e2e-spec.ts index 7c3d806d..4ad10846 100644 --- a/test/builds.e2e-spec.ts +++ b/test/builds.e2e-spec.ts @@ -32,7 +32,7 @@ describe('Builds (e2e)', () => { beforeEach(async () => { user = await haveUserLogged(usersService); - project = await projecstService.create({ name: 'E2E test' }); + project = await projecstService.create({ name: 'E2E test', mainBranchName: 'master' }); }); afterEach(async () => { diff --git a/test/projects.e2e-spec.ts b/test/projects.e2e-spec.ts index 78708431..5b3766e4 100644 --- a/test/projects.e2e-spec.ts +++ b/test/projects.e2e-spec.ts @@ -16,6 +16,7 @@ const projectServiceMock = { findAll: () => ['test'], create: () => project, remove: () => project, + update: () => project, }; describe('Projects (e2e)', () => { @@ -91,4 +92,16 @@ describe('Projects (e2e)', () => { await requestWithAuth(app, 'delete', `/projects/${project.id}`, {}, 'asd').expect(401); }); }); + + describe('PUT /', () => { + it('can edit', async () => { + const res = await requestWithAuth(app, 'put', `/projects`, project, loggedUser.token).expect(200); + + expect(res.body).toStrictEqual(projectServiceMock.update()); + }); + + it('not valid token', async () => { + await requestWithAuth(app, 'put', `/projects`, project, 'asd').expect(401); + }); + }); });