Skip to content

Commit

Permalink
feat(pmp): edit repository in settings - backend
Browse files Browse the repository at this point in the history
refs #107
  • Loading branch information
ABartoszko committed May 20, 2020
1 parent 02cdbdc commit 26f5187
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, Controller, Delete, Get, Param, Post, UseGuards } from '@nestjs/common';
import { Body, Controller, Delete, Get, Param, Post, Put, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import {
AuthGuard,
Expand All @@ -10,12 +10,14 @@ import {
import {
AddRepositoryCommand,
DeleteRepositoryCommand,
EditRepositoryCommand,
RepositoryFacade
} from '@pimp-my-pr/server/repository/core/application-services';
import { RepositoryEntity } from '@pimp-my-pr/server/repository/core/domain';
import { extractFullName } from '@pimp-my-pr/server/shared/util-repository';
import { AddRepositoryDto } from '../dtos/add-repository.dto';
import { UserRepositoryGuard } from '../guards/user-repository.guard';
import { EditRepositoryDto } from '../dtos/edit-repository.dto';

@ApiTags('repository')
@ApiBearerAuth()
Expand Down Expand Up @@ -52,4 +54,19 @@ export class RepositoryController {
delete(@Param('repositoryId') repositoryId: string): Promise<void> {
return this.repositoryFacade.deleteRepository(new DeleteRepositoryCommand(repositoryId));
}

@UseGuards(UserRepositoryGuard)
@Put(':repositoryId')
edit(
@Param('repositoryId') repositoryId: string,
@Body() editRepositoryDto: EditRepositoryDto
): Promise<void> {
return this.repositoryFacade.editRepository(
new EditRepositoryCommand(
repositoryId,
editRepositoryDto.maxLines,
editRepositoryDto.maxWaitingTime
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNumber, IsOptional } from 'class-validator';

export class EditRepositoryDto {
@ApiProperty()
@IsNumber()
@IsOptional()
maxLines?: number;

@ApiProperty()
@IsNumber()
@IsOptional()
maxWaitingTime?: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from './lib/queries/list-repositories-statistics/repositories-statistic
export * from './lib/read-models/reviewer-model-with-pr.interface';
export * from './lib/commands/add-repository/add-repository.command';
export * from './lib/commands/delete-repository/delete-repository.command';
export * from './lib/commands/edit-repository/edit-repository.command';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class EditRepositoryCommand {
constructor(
public repositoryId: string,
public maxLines?: number,
public maxWaitingTime?: number
) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { EditRepositoryHandler } from './edit-repository.handler';
import { EditRepositoryCommand } from './edit-repository.command';
import { RepositoryRepository } from '@pimp-my-pr/server/repository/core/domain-services';
import { Test } from '@nestjs/testing';
import { RepositoryEntity } from '@pimp-my-pr/server/repository/core/domain';
import Mocked = jest.Mocked;

describe('Edit Repository Handler', () => {
let editRepositoryHandler: EditRepositoryHandler;
let repositoryRepository: Mocked<RepositoryRepository>;
const repository = new RepositoryEntity('123', 'test', 'test', 'test', '1');

beforeEach(async () => {
const module = await Test.createTestingModule({
providers: [
{
provide: RepositoryRepository,
useValue: {}
},
EditRepositoryHandler
]
}).compile();

repositoryRepository = module.get(RepositoryRepository);
editRepositoryHandler = module.get<EditRepositoryHandler>(EditRepositoryHandler);
});

describe('update', () => {
it('execute save repository', async () => {
const command: EditRepositoryCommand = {
repositoryId: '123',
maxLines: 2,
maxWaitingTime: 3
};

repositoryRepository.getById = jest.fn();
repositoryRepository.getById.mockResolvedValue(repository);

repository.edit = jest.fn();
repositoryRepository.save = jest.fn();

await editRepositoryHandler.execute(command);
expect(repository.edit).toHaveBeenCalledWith({
maxLines: command.maxLines,
maxWaitingTime: command.maxWaitingTime
});

expect(repositoryRepository.save).toHaveBeenCalledWith(repository);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { RepositoryRepository } from '@pimp-my-pr/server/repository/core/domain-services';
import { EditRepositoryCommand } from './edit-repository.command';

@CommandHandler(EditRepositoryCommand)
export class EditRepositoryHandler implements ICommandHandler<EditRepositoryCommand> {
constructor(private repositoryRepository: RepositoryRepository) {}

async execute(command: EditRepositoryCommand): Promise<void> {
const { repositoryId, maxLines, maxWaitingTime } = command;

const repository = await this.repositoryRepository.getById(repositoryId);

repository.edit({ maxLines, maxWaitingTime });

return this.repositoryRepository.save(repository);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CommandBus, QueryBus } from '@nestjs/cqrs';

import {
DeleteRepositoryCommand,
EditRepositoryCommand,
RepositoriesStatisticsItemReadModel,
ReviewersStatisticsItemReadModel,
ReviewerStatisticsReadModel
Expand All @@ -28,6 +29,10 @@ export class RepositoryFacade {
return this.commandBus.execute(command);
}

editRepository(command: EditRepositoryCommand): Promise<void> {
return this.commandBus.execute(command);
}

getRepositoryStatistics(
repositoryId: string,
token: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import { ListRepositoriesHandler } from './queries/list-repositories/list-reposi
import { ListReviewersStatisticsHandler } from './queries/list-reviewers-statistics/list-reviewers-statistics.handler';
import { RepositoryFacade } from './repository.facade';
import { DeleteRepositoryHandler } from './commands/delete-repository/delete-repository.handler';
import { EditRepositoryHandler } from './commands/edit-repository/edit-repository.handler';

const QueryHandlers = [
AddRepositoryHandler,
DeleteRepositoryHandler,
EditRepositoryHandler,
GetRepositoryStatisticsHandler,
GetReviewerStatisticsHandler,
ListRepositoriesStatisticsHandler,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { f } from '@marcj/marshal';
import { PrEntity } from './pr.entity';
import { RepositoryEditWriteModel } from '@pimp-my-pr/shared/domain';

export class RepositoryEntity {
@f.primary()
Expand Down Expand Up @@ -50,4 +51,9 @@ export class RepositoryEntity {
this.maxWaitingTime = maxWaitingTime;
this.maxLines = maxLines;
}

edit(writeModel: RepositoryEditWriteModel): void {
this.maxLines = writeModel.maxLines;
this.maxWaitingTime = writeModel.maxWaitingTime;
}
}
1 change: 1 addition & 0 deletions libs/shared/domain/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from './lib/interfaces/pr-author.interface';
export * from './lib/interfaces/pr-reviewer.interface';
export * from './lib/interfaces/pr.statistics';
export * from './lib/interfaces/repository.model';
export * from './lib/interfaces/repository-edit-write.model';
export * from './lib/interfaces/repository-statistics.interface';
export * from './lib/read-models/user.read-model';
export * from './lib/responses/api-repository-statistics.response';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface RepositoryEditWriteModel {
maxLines?: number;
maxWaitingTime?: number;
}

0 comments on commit 26f5187

Please sign in to comment.