Skip to content

Commit

Permalink
Merge branch 'feature/SNP-70' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Sungho95 committed Dec 20, 2023
2 parents fda010f + cc5e76b commit c450ddf
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 73 deletions.
4 changes: 4 additions & 0 deletions src/answers/answer.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Controller } from '@nestjs/common';

@Controller('answers')
export class AnswerController {}
10 changes: 10 additions & 0 deletions src/answers/answer.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { AnswerService } from './answer.service';
import { AnswerController } from './answer.controller';
import { AnswerRepository } from './answer.repository';

@Module({
providers: [AnswerService, AnswerRepository],
controllers: [AnswerController],
})
export class AnswerModule {}
1 change: 1 addition & 0 deletions src/answers/answer.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class AnswerRepository {}
4 changes: 4 additions & 0 deletions src/answers/answer.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class AnswerService {}
23 changes: 23 additions & 0 deletions src/answers/dtos/answer.request.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString, IsUUID } from 'class-validator';

export class CreateAnswerDto {
@ApiProperty({ description: '보내는 사용자 닉네임', default: '홍길동' })
@IsString()
senderNickname: string;

@ApiProperty({ description: '받는 사용자 아이디', default: '{uuid}' })
@IsUUID()
receiverId: string;

@ApiProperty({ description: '받는 사용자 이름', default: '덕배' })
@IsString()
receiverNickname: string;

@ApiProperty({
description: '보낼 편지의 내용',
default: '이 편지는 영국에서부터 시작되어..',
})
@IsString()
content: string;
}
1 change: 1 addition & 0 deletions src/answers/dtos/answer.response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class AnswerResponseDto {}
36 changes: 36 additions & 0 deletions src/entities/answer.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Column, Entity } from 'typeorm';
import { BaseEntity } from '../common/entities/base.entity';

@Entity({ name: 'answers' })
export class Answer extends BaseEntity {
@Column({
name: 'sender_id',
comment: '답장하는 사용자 아이디',
nullable: true,
})
senderId: string;

@Column({
name: 'receiver_id',
comment: '답장받는 사용자 아이디',
nullable: false,
})
receiverId: string;

@Column({
name: 'sender_nickname',
comment: '답장하는 사용자 닉네임',
nullable: false,
})
senderNickname: string;

@Column({
name: 'receiver_nickname',
comment: '답장받는 사용자 닉네임',
nullable: false,
})
receiverNickname: string;

@Column({ name: 'content', comment: '답장 내용', nullable: false })
content: string;
}
21 changes: 16 additions & 5 deletions src/entities/letter.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,28 @@ import { Column, Entity } from 'typeorm';
export class Letter extends BaseEntity {
@Column({
name: 'sender_id',
comment: '보낸 사용자 아이디',
comment: '편지 쓰는 사용자 아이디',
nullable: false,
})
senderId: string;

@Column({ name: 'sender_nickname', nullable: false })
@Column({
name: 'sender_nickname',
comment: '편지 쓰는 사용자 닉네임',
nullable: false,
})
senderNickname: string;

@Column({ name: 'receiver_name', nullable: false })
receiverName: string;
@Column({
name: 'receiver_nickname',
comment: '편지 받는 사용자 닉네임',
nullable: false,
})
receiverNickname: string;

@Column({ name: 'content', nullable: false })
@Column({ name: 'content', comment: '편지 내용', nullable: false })
content: string;

@Column( { name: 'cat_type', comment: '고양이 타입', nullable: false })
catType: string;
}
40 changes: 18 additions & 22 deletions src/letters/dtos/letter.request.dto.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@
import { ApiProperty, PickType } from '@nestjs/swagger';
import { IsString, IsUUID } from 'class-validator';
import { LetterResponseDto } from './letter.response.dto';
import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';
import { Letter } from '../../entities/letter.entity';

export class PostLetterRequestDto {
@ApiProperty({ description: '보내는 사용자 아이디' })
@IsUUID()
senderId: string;

@ApiProperty({ description: '보내는 사용자 닉네임' })
export class CreateLetterDto {
@ApiProperty({ description: '보내는 사용자 닉네임', default: '홍길동' })
@IsString()
senderNickname: string;

@ApiProperty({ description: '받는 사용자 이름', default: '덕배' })
@IsString()
receiverName: string;
receiverNickname: string;

@ApiProperty({
description: '보낼 편지의 내용',
default: '이 편지는 영국에서부터 시작되어...',
})
@IsString()
content: string;

@ApiProperty( { description: '고양이 타입', default: 'umu' })
@IsString()
catType: string;
}

export function toEntity(postLetterRequestDto: PostLetterRequestDto): Letter {
export function toEntity(
userId: string,
createLetterDto: CreateLetterDto,
): Letter {
const letter = new Letter();
letter.senderId = postLetterRequestDto.senderId;
letter.senderNickname = postLetterRequestDto.senderNickname;
letter.receiverName = postLetterRequestDto.receiverName;
letter.content = postLetterRequestDto.content;
letter.senderId = userId;
letter.senderNickname = createLetterDto.senderNickname;
letter.receiverNickname = createLetterDto.receiverNickname;
letter.content = createLetterDto.content;
letter.catType = createLetterDto.catType;
return letter;
}

export class PostLetterResponseDto extends PickType(LetterResponseDto, ['id']) {
constructor(id: string) {
super();
this.id = id;
}
}
37 changes: 21 additions & 16 deletions src/letters/dtos/letter.response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { ApiProperty } from '@nestjs/swagger';
import { IsDate, IsOptional, IsString, IsUUID } from 'class-validator';
import { Letter } from '../../entities/letter.entity';

export class LetterResponseDto {
@ApiProperty({ description: '편지 아이디' })
export class LetterDetailDto {
@ApiProperty({ description: '편지 아이디', default: '{letterId}' })
@IsUUID('all')
id: string;

@ApiProperty({ description: '보낸 사용자 아이디', default: '1' })
@ApiProperty({ description: '보낸 사용자 아이디', default: '{senderId}' })
@IsUUID('all')
senderId: string;

Expand All @@ -17,7 +17,7 @@ export class LetterResponseDto {

@ApiProperty({ description: '받는 사용자 이름', default: '덕배' })
@IsString()
receiverName: string;
receiverNickname: string;

@ApiProperty({
description: '내용',
Expand All @@ -26,6 +26,13 @@ export class LetterResponseDto {
@IsString()
content: string;

@ApiProperty({
description: '고양이 타입',
default: 'amu'
})
@IsString()
catType: string;

@ApiProperty({ description: '생성일자' })
@IsDate()
createdAt: Date;
Expand All @@ -38,18 +45,16 @@ export class LetterResponseDto {
@IsOptional()
@IsDate()
deletedAt: Date | null;
}

export class GetLettersResponseDto extends LetterResponseDto {
constructor(lettersEntity: Letter) {
super();
this.id = lettersEntity.id;
this.receiverName = lettersEntity.receiverName;
this.content = lettersEntity.content;
this.senderId = lettersEntity.senderId;
this.senderNickname = lettersEntity.senderNickname;
this.createdAt = lettersEntity.createdAt;
this.updatedAt = lettersEntity.updatedAt;
this.deletedAt = lettersEntity.deletedAt;
constructor(letter: Letter) {
this.id = letter.id;
this.receiverNickname = letter.receiverNickname;
this.content = letter.content;
this.senderId = letter.senderId;
this.senderNickname = letter.senderNickname;
this.catType = letter.catType;
this.createdAt = letter.createdAt;
this.updatedAt = letter.updatedAt;
this.deletedAt = letter.deletedAt;
}
}
23 changes: 12 additions & 11 deletions src/letters/letter.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@ import {
Post,
UseGuards,
} from '@nestjs/common';
import { GetLettersResponseDto } from './dtos/letter.response.dto';
import { LetterDetailDto } from './dtos/letter.response.dto';
import { Response } from 'src/common/interface';
import {
PostLetterRequestDto,
PostLetterResponseDto,
} from './dtos/letter.request.dto';
import { CreateLetterDto } from './dtos/letter.request.dto';
import { AccessGuard } from 'src/auth/guards/acess.guard';
import { AuthUser } from '../auth/decorators/auth-user.decorator';

@Controller('letters')
@ApiTags('Letters API')
@ApiExtraModels(GetLettersResponseDto, PostLetterResponseDto)
@ApiExtraModels(LetterDetailDto, LetterDetailDto)
@ApiBearerAuth()
export class LetterController {
constructor(private readonly lettersService: LetterService) {}
Expand All @@ -39,13 +37,16 @@ export class LetterController {
@ApiCreatedResponse({
description: '편지 생성 완료',
schema: {
$ref: getSchemaPath(PostLetterResponseDto),
$ref: getSchemaPath(LetterDetailDto),
},
})
@Post()
@UseGuards(AccessGuard)
async postLetter(@Body() postLetterRequestDto: PostLetterRequestDto) {
return this.lettersService.createLetter(postLetterRequestDto);
async postLetter(
@AuthUser() { id },
@Body() createLetterDto: CreateLetterDto,
) {
return this.lettersService.createLetter(id, createLetterDto);
}

@ApiOperation({
Expand All @@ -55,13 +56,13 @@ export class LetterController {
@ApiOkResponse({
description: '편지 상세 조회',
schema: {
$ref: getSchemaPath(GetLettersResponseDto),
$ref: getSchemaPath(LetterDetailDto),
},
})
@Get(':letterId')
async getLetterDetail(
@Param('letterId', ParseUUIDPipe) letterId: string,
): Promise<Response<GetLettersResponseDto>> {
): Promise<Response<LetterDetailDto>> {
return this.lettersService.getLetterDetail(letterId);
}
}
6 changes: 6 additions & 0 deletions src/letters/letter.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ export class LetterRepository extends Repository<Letter> {
async createLetter(letter: Letter) {
return this.save(letter);
}

async getLetter(letterId: string) {
return this.findOne({
where: { id: letterId },
});
}
}
32 changes: 13 additions & 19 deletions src/letters/letter.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { LetterRepository } from './letter.repository';
import { Injectable } from '@nestjs/common';
import { createResponse } from 'src/utils/response.utils';
import { GetLettersResponseDto } from './dtos/letter.response.dto';
import { LetterDetailDto } from './dtos/letter.response.dto';
import { Response } from 'src/common/interface';
import {
PostLetterRequestDto,
PostLetterResponseDto,
toEntity,
} from './dtos/letter.request.dto';
import { CreateLetterDto, toEntity } from './dtos/letter.request.dto';

@Injectable()
export class LetterService {
Expand All @@ -19,23 +15,21 @@ export class LetterService {
* @issue SNP-39
* @link https://www.notion.so/raymondanything/SNP-39-26f72c15dd354ee9b870899c3be0bc40?pvs=4
* @author raymondanything
* @param postLetterRequestDto
* @returns {Promise<Response <PostLetterResponseDto>>} PostLetterResponseDto
* @returns {Promise<Response <LetterDetailDto>>} PostLetterResponseDto
* @param userId
* @param createLetterDto
*/
async createLetter(
postLetterRequestDto: PostLetterRequestDto,
): Promise<Response<PostLetterResponseDto>> {
const letter = toEntity(postLetterRequestDto);
userId: string,
createLetterDto: CreateLetterDto,
): Promise<Response<LetterDetailDto>> {
const letter = toEntity(userId, createLetterDto);
const newLetter = await this.lettersRepository.createLetter(letter);
return createResponse(new PostLetterResponseDto(newLetter['id']));
return createResponse(new LetterDetailDto(newLetter));
}

async getLetterDetail(
letterId: string,
): Promise<Response<GetLettersResponseDto>> {
const letter = await this.lettersRepository.findOne({
where: { id: letterId },
});
return createResponse(new GetLettersResponseDto(letter));
async getLetterDetail(letterId: string): Promise<Response<LetterDetailDto>> {
const findLetter = await this.lettersRepository.getLetter(letterId);
return createResponse(new LetterDetailDto(findLetter));
}
}

0 comments on commit c450ddf

Please sign in to comment.