-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added base question module and create method
- Loading branch information
1 parent
d991601
commit 5583737
Showing
32 changed files
with
245 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './database.module'; | ||
export * from './repository/question.repositoy'; | ||
export * from './repository/user.repositoy'; | ||
export * from './prisma/prisma.service'; |
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,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { UsersModule } from './users/users.module'; | ||
import { UserModule } from './user/user.module'; | ||
import { QuestionModule } from './question/question.module'; | ||
|
||
@Module({ | ||
imports: [ | ||
UsersModule | ||
UserModule, | ||
QuestionModule | ||
], | ||
}) | ||
export class Modules { } |
34 changes: 34 additions & 0 deletions
34
src/modules/question/controllers/create-question.controller.ts
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,34 @@ | ||
import { | ||
Body, | ||
Controller, | ||
HttpCode, | ||
HttpException, | ||
HttpStatus, | ||
NotFoundException, | ||
Post | ||
} from '@nestjs/common'; | ||
import { ApiOkResponse, ApiTags } from '@nestjs/swagger'; | ||
import { CreateQuestionDto } from '../dto/create-question.dto'; | ||
import { QuestionDto } from '../dto/question.dto'; | ||
import { CreateQuestionUseCase } from '../use-case/create-question'; | ||
|
||
@Controller('/question') | ||
@ApiTags('question') | ||
export class CreateQuestionController { | ||
constructor(private createQuestionUseCase: CreateQuestionUseCase) { } | ||
|
||
@ApiOkResponse({ type: QuestionDto }) | ||
@HttpCode(HttpStatus.CREATED) | ||
@Post('') | ||
handle(@Body() createQuestionDto: CreateQuestionDto) { | ||
try { | ||
const question = this.createQuestionUseCase.execute(createQuestionDto); | ||
return question; | ||
} catch (e) { | ||
if (e instanceof NotFoundException) { | ||
throw new HttpException('Author not found', HttpStatus.CONFLICT); | ||
} | ||
throw new HttpException('Was not possible to register', HttpStatus.BAD_REQUEST); | ||
} | ||
} | ||
} |
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,17 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { | ||
IsNotEmpty, | ||
IsString | ||
} from 'class-validator'; | ||
|
||
export class CreateQuestionDto { | ||
@ApiProperty() | ||
@IsString() | ||
@IsNotEmpty() | ||
content: string; | ||
|
||
@ApiProperty() | ||
@IsString() | ||
@IsNotEmpty() | ||
authorId: string; | ||
} |
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,19 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { Question } from '@prisma/client'; | ||
|
||
export class QuestionDto implements Question { | ||
@ApiProperty() | ||
id: string; | ||
|
||
@ApiProperty() | ||
content: string; | ||
|
||
@ApiProperty() | ||
createdAt: Date; | ||
|
||
@ApiProperty() | ||
updatedAt: Date; | ||
|
||
@ApiProperty() | ||
authorId: string; | ||
} |
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,12 @@ | ||
import { DatabaseModule } from '@app/common'; | ||
import { Module } from '@nestjs/common'; | ||
import { CreateQuestionController } from './controllers/create-question.controller'; | ||
import { CreateQuestionUseCase } from './use-case/create-question'; | ||
|
||
@Module({ | ||
controllers: [CreateQuestionController], | ||
imports: [DatabaseModule], | ||
providers: [CreateQuestionUseCase], | ||
exports: [], | ||
}) | ||
export class QuestionModule { } |
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,118 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { QuestionRepository, UserRepository } from '@app/common'; | ||
import { CreateQuestionDto } from '../dto/create-question.dto'; | ||
import { Question, User } from '@prisma/client'; | ||
import { CreateQuestionUseCase } from './create-question'; | ||
|
||
class QuestionRepositoryMock { | ||
create = jest.fn(); | ||
} | ||
|
||
class UserRepositoryMock { | ||
findById = jest.fn(); | ||
} | ||
|
||
describe('CreateQuestionUseCase', () => { | ||
let createQuestionUseCase: CreateQuestionUseCase; | ||
let questionRepository: QuestionRepository; | ||
let userRepository: UserRepository; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
CreateQuestionUseCase, | ||
{ | ||
provide: QuestionRepository, | ||
useClass: QuestionRepositoryMock, | ||
}, | ||
{ | ||
provide: UserRepository, | ||
useClass: UserRepositoryMock, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
createQuestionUseCase = module.get<CreateQuestionUseCase>(CreateQuestionUseCase); | ||
questionRepository = module.get<QuestionRepository>(QuestionRepository); | ||
userRepository = module.get<UserRepository>(UserRepository); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(createQuestionUseCase).toBeDefined(); | ||
}); | ||
|
||
it('should create a question when the author is found', async () => { | ||
const createQuestionDto: CreateQuestionDto = { | ||
content: 'What is your favorite color?', | ||
authorId: 'fsdfsd-sdfsdfsd-sdfsdfsd-sdfsdf', | ||
}; | ||
|
||
const question: Question = { | ||
id: 'aaaaa-sdfsdfsd-sdfsdfsd-bbbbb', | ||
content: createQuestionDto.content, | ||
authorId: createQuestionDto.authorId, | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
}; | ||
|
||
const user: Partial<User> = { | ||
id: 'fsdfsd-sdfsdfsd-sdfsdfsd-sdfsdf', | ||
username: 'test', | ||
}; | ||
|
||
userRepository.findById = jest.fn().mockResolvedValue(user); | ||
questionRepository.create = jest.fn().mockResolvedValue(question); | ||
|
||
const result = await createQuestionUseCase.execute(createQuestionDto); | ||
|
||
expect(result).toEqual(question); | ||
expect(userRepository.findById).toHaveBeenCalledWith(createQuestionDto.authorId); | ||
expect(questionRepository.create).toHaveBeenCalledWith({ | ||
content: createQuestionDto.content, | ||
author: { | ||
connect: { | ||
id: createQuestionDto.authorId | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
it('should throw a not found exception when the author is not found', async () => { | ||
const createQuestionDto: CreateQuestionDto = { | ||
content: 'What is your favorite color?', | ||
authorId: 'fsdfsd-sdfsdfsd-sdfsdfsd-sdfsdf', | ||
}; | ||
userRepository.findById = jest.fn().mockResolvedValue(null); | ||
|
||
await expect(createQuestionUseCase.execute(createQuestionDto)).rejects.toThrow('Author not found'); | ||
|
||
expect(userRepository.findById).toHaveBeenCalledWith(createQuestionDto.authorId); | ||
}); | ||
|
||
it('should throw a bad request exception when it was not possible to register', async () => { | ||
const createQuestionDto: CreateQuestionDto = { | ||
content: 'What is your favorite color?', | ||
authorId: 'fsdfsd-sdfsdfsd-sdfsdfsd-sdfsdf', | ||
}; | ||
|
||
const user: Partial<User> = { | ||
id: 'fsdfsd-sdfsdfsd-sdfsdfsd-sdfsdf', | ||
username: 'test', | ||
}; | ||
|
||
userRepository.findById = jest.fn().mockResolvedValue(user); | ||
questionRepository.create = jest.fn().mockRejectedValue(new Error()); | ||
|
||
await expect(createQuestionUseCase.execute(createQuestionDto)).rejects.toThrow('Was not possible to register'); | ||
|
||
expect(userRepository.findById).toHaveBeenCalledWith(createQuestionDto.authorId); | ||
expect(questionRepository.create).toHaveBeenCalledWith({ | ||
content: createQuestionDto.content, | ||
author: { | ||
connect: { | ||
id: createQuestionDto.authorId | ||
} | ||
} | ||
}); | ||
}); | ||
}); |
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,34 @@ | ||
|
||
import { QuestionRepository, UserRepository } from '@app/common'; | ||
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common'; | ||
import { CreateQuestionDto } from '../dto/create-question.dto'; | ||
|
||
@Injectable() | ||
export class CreateQuestionUseCase { | ||
constructor( | ||
private readonly questionRepository: QuestionRepository, | ||
private readonly userRepository: UserRepository | ||
) { } | ||
|
||
async execute(createQuestionDto: CreateQuestionDto) { | ||
const user = await this.userRepository.findById(createQuestionDto.authorId); | ||
|
||
if (!user) { | ||
throw new NotFoundException('Author not found'); | ||
} | ||
|
||
try { | ||
const question = await this.questionRepository.create({ | ||
content: createQuestionDto.content, | ||
author: { | ||
connect: { | ||
id: user.id | ||
} | ||
} | ||
}); | ||
return question; | ||
} catch (e) { | ||
throw new BadRequestException('Was not possible to register'); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...ollers/create-user.controller.e2e-spec.ts → ...ollers/create-user.controller.e2e-spec.ts
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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...ollers/delete-user.controller.e2e-spec.ts → ...ollers/delete-user.controller.e2e-spec.ts
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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...ers/get-many-users.controller.e2e-spec.ts → ...ers/get-many-users.controller.e2e-spec.ts
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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...ers/get-user-by-id.controller.e2e-spec.ts → ...ers/get-user-by-id.controller.e2e-spec.ts
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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...ollers/update-user.controller.e2e-spec.ts → ...ollers/update-user.controller.e2e-spec.ts
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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