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

[feat] 문의하기 API 생성 #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/user/dto/reqeust/question-request.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsByteLength, IsNotEmpty, IsString } from 'class-validator';

export class QuestionRequest {
@ApiProperty({ description: '문의 제목' })
@IsString()
@IsNotEmpty({ message: '제목은 필수입니다.' })
readonly title: string;

@ApiProperty({ description: '답변 받을 이메일' })
@IsString()
@IsNotEmpty({ message: '이메일은 필수입니다.' })
readonly email: string;

@ApiProperty({ description: '문의 내용' })
@IsString()
@IsNotEmpty({ message: '문의 내용은 필수입니다.' })
@IsByteLength(20, 10000, { message: '문의 내용은 10글자 이상이어야 합니다.' })
readonly contents: string;
}
6 changes: 6 additions & 0 deletions src/user/dto/response/question-response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';

export class QuestionResponse {
@ApiProperty({ description: '문의 전송 여부', example: true })
isSended: boolean;
}
17 changes: 17 additions & 0 deletions src/user/entity/question.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { BaseEntity } from 'src/global/base/base.entity';
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity('questions')
export class Question extends BaseEntity {
@PrimaryGeneratedColumn()
questionId: number;

@Column()
title: string;

@Column()
reporter: string;

@Column()
contents: string;
}
14 changes: 14 additions & 0 deletions src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import { ReportUserResponse } from './dto/response/report-user-response.dto';
import { SignUpResponse } from './dto/response/sign-up-response.dto';
import { UserConverter } from './user.converter';
import { UserService } from './user.service';
import { QuestionResponse } from './dto/response/question-response.dto';
import { QuestionRequest } from './dto/reqeust/question-request.dto';

@Controller('api/users')
@ApiTags('users')
Expand Down Expand Up @@ -105,4 +107,16 @@ export class UserController {
GlobalResponseCode.OK,
);
}

@Post('question')
@UseGuards(JwtAuthGuard)
@ApiOperation({
summary: '문의 API',
})
@ApiBearerAuth()
@ApiCreatedResponse({ type: QuestionResponse, description: '문의 전송 성공' })
async question(@Body() request: QuestionRequest): Promise<BaseResponse<QuestionResponse>> {
const question = await this.userService.question(request);
return BaseResponse.of(UserConverter.toQuestionResponse(question));
}
}
8 changes: 8 additions & 0 deletions src/user/user.converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { Password } from './entity/password';
import { UserReport } from './entity/user-report.entity';
import { User } from './entity/user.entity';
import { UserReportReason } from './enum/user-report-reason';
import { QuestionResponse } from './dto/response/question-response.dto';
import { Question } from './entity/question.entity';

@Injectable()
export class UserConverter implements OnModuleInit {
Expand Down Expand Up @@ -57,4 +59,10 @@ export class UserConverter implements OnModuleInit {
public static toFindingPasswordResponse(isPasswordSended: boolean): FindingPasswordResponse {
return Builder(FindingPasswordResponse).isPasswordSended(isPasswordSended).build();
}

public static toQuestionResponse(question: Question): QuestionResponse {
return Builder(QuestionResponse)
.isSended(question ? true : false)
.build();
}
}
8 changes: 8 additions & 0 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { UserReport } from './entity/user-report.entity';
import { User } from './entity/user.entity';
import { UserResponseCode } from './exception/user-response-code';
import { UserConverter } from './user.converter';
import { QuestionRequest } from './dto/reqeust/question-request.dto';
import { Question } from './entity/question.entity';

@Injectable()
export class UserService {
Expand All @@ -22,6 +24,8 @@ export class UserService {
@InjectRepository(UserReport)
private readonly userReportRepository: Repository<UserReport>,
private readonly mailService: MailService,
@InjectRepository(Question)
private readonly questionRepository: Repository<Question>,
) {}

async isUserExists(userId: number): Promise<boolean> {
Expand Down Expand Up @@ -87,4 +91,8 @@ export class UserService {
async sendEmail(request: FindingPasswordRequest, password: string): Promise<void> {
await this.mailService.sendMail(request.email, '[BookJam] 임시 비밀번호 안내', password);
}

async question(request: QuestionRequest) {
return await this.questionRepository.save(request);
}
}