Skip to content

Commit

Permalink
dto, entity 리팩토링 (#278)
Browse files Browse the repository at this point in the history
* Refactor : album dto, entity 리팩토링

* Refactor : 앨범 응답 dto 수정

* Refactor : group dto, entity 리팩토링

* Refactor : group dto, entity 수정 및 controller async 추가

* Refactor : user dto, entity 리팩토링

* Refactor : post dto, entity 리팩토링

* Refactor : 게시글 불러오는 쿼리에서 postId 추가

* Refactor : docker-compose 수정

* Feat : 앨범 쿼리 최적화

* Feat : group 쿼리 최적화

* Refactor : auth controller service 주입 삭제
  • Loading branch information
soosungp33 authored and gofeel8 committed Dec 5, 2021
1 parent 2218d4f commit 13e217d
Show file tree
Hide file tree
Showing 30 changed files with 369 additions and 263 deletions.
15 changes: 8 additions & 7 deletions backend/src/domain/album/album.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@ import { UpdateAlbumInfoRequestDto } from "src/domain/album/dto/updateAlbumInfoR
import { CreateAlbumResponseDto } from "src/domain/album/dto/createAlbumResponse.dto";
import { SwaggerCreateAlbum, SwaggerUpdateAlbumInfo, SwaggerDeleteAlbum } from "./swagger";
import { CustomController } from "src/custom/decorator/controller.decorator";
import { UpdateResult } from "typeorm";

@CustomController("albums", "앨범 API")
export class AlbumController {
constructor(private readonly albumService: AlbumService) {}

@Post()
@SwaggerCreateAlbum()
CreateAlbum(@Body() createAlbumRequestDto: CreateAlbumRequestDto): Promise<CreateAlbumResponseDto> {
return this.albumService.createAlbum(createAlbumRequestDto);
async CreateAlbum(@Body() createAlbumRequestDto: CreateAlbumRequestDto): Promise<CreateAlbumResponseDto> {
return await this.albumService.createAlbum(createAlbumRequestDto);
}

@Put("/:albumId")
@SwaggerUpdateAlbumInfo()
UpdateAlbumInfo(
async UpdateAlbumInfo(
@Param("albumId") albumId: number,
@Body() updateAlbumInfoRequestDto: UpdateAlbumInfoRequestDto,
): Promise<string> {
return this.albumService.updateAlbumInfo(albumId, updateAlbumInfoRequestDto);
): Promise<UpdateResult> {
return await this.albumService.updateAlbumInfo(albumId, updateAlbumInfoRequestDto);
}

@Delete("/:albumId")
@SwaggerDeleteAlbum()
DeleteAlbum(@Param("albumId") albumId: number): Promise<string> {
return this.albumService.deleteAlbum(albumId);
async DeleteAlbum(@Param("albumId") albumId: number): Promise<boolean> {
return await this.albumService.deleteAlbum(albumId);
}
}
9 changes: 9 additions & 0 deletions backend/src/domain/album/album.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ export class Album extends TimeStampEntity {

@OneToMany(() => Post, post => post.album)
posts: Post[];

static toEntity(albumName: string, base: boolean, group: Group) {
const album = new Album();
album.albumName = albumName;
album.base = base;
album.group = group;

return album;
}
}
28 changes: 11 additions & 17 deletions backend/src/domain/album/album.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CreateAlbumRequestDto } from "src/domain/album/dto/createAlbumRequest.d
import { UpdateAlbumInfoRequestDto } from "src/domain/album/dto/updateAlbumInfoRequest.dto";
import { CreateAlbumResponseDto } from "src/domain/album/dto/createAlbumResponse.dto";
import { Album } from "./album.entity";
import { Connection, QueryRunner } from "typeorm";
import { Connection, QueryRunner, UpdateResult } from "typeorm";
import { Group } from "src/domain/group/group.entity";
import { Post } from "src/domain/post/post.entity";
import { deleteOrder } from "src/util/changeObject";
Expand All @@ -30,20 +30,16 @@ export class AlbumService {
queryRunner.startTransaction();

try {
const album = await queryRunner.manager.getRepository(Album).save({
albumName: albumName,
base: false,
group: group,
});
const album = await queryRunner.manager.getRepository(Album).save(Album.toEntity(albumName, false, group));
const { albumId } = album;

const { albumOrder } = group;
const newAlbumOrder = `${albumId},${albumOrder}`;
await queryRunner.manager.getRepository(Group).update(groupId, { albumOrder: newAlbumOrder });
queryRunner.manager.getRepository(Group).update(groupId, { albumOrder: newAlbumOrder });

await queryRunner.commitTransaction();

return { albumId };
return CreateAlbumResponseDto.returnDto(album);
} catch (error) {
await queryRunner.rollbackTransaction();
throw new BadRequestException(error);
Expand All @@ -52,17 +48,15 @@ export class AlbumService {
}
}

async updateAlbumInfo(albumId: number, updateAlbumInfoRequestDto: UpdateAlbumInfoRequestDto): Promise<string> {
async updateAlbumInfo(albumId: number, updateAlbumInfoRequestDto: UpdateAlbumInfoRequestDto): Promise<UpdateResult> {
const { albumName } = updateAlbumInfoRequestDto;
const album = await this.albumRepository.findOne({ albumId });
if (!album) throw new NotFoundException(`Not found album with the id ${albumId}`);

await this.albumRepository.update(albumId, { albumName });

return "AlbumInfo update success!!";
return await this.albumRepository.update(albumId, { albumName });
}

async deleteAlbum(albumId: number): Promise<string> {
async deleteAlbum(albumId: number): Promise<boolean> {
const album = await this.albumRepository.findOne(albumId, { relations: ["group"] });
if (!album) throw new NotFoundException(`Not found album with the id ${albumId}`);

Expand All @@ -82,11 +76,11 @@ export class AlbumService {
const { albumOrder } = group;
const reArrangedOrder = deleteOrder(albumOrder, albumId);

await queryRunner.manager.getRepository(Group).update(groupId, { albumOrder: reArrangedOrder });
queryRunner.manager.getRepository(Group).update(groupId, { albumOrder: reArrangedOrder });

await queryRunner.commitTransaction();

return "Album delete success!!";
return true;
} catch (error) {
await queryRunner.rollbackTransaction();
throw new BadRequestException(error);
Expand All @@ -106,9 +100,9 @@ export class AlbumService {
async movePosts(albumId: number, baseAlbum: Album, queryRunner: QueryRunner): Promise<void> {
const { posts } = await this.getMovePosts(albumId, queryRunner);

posts.forEach(async post => {
posts.forEach(post => {
const postId = post.postId;
await queryRunner.manager.getRepository(Post).update(postId, { album: baseAlbum });
queryRunner.manager.getRepository(Post).update(postId, { album: baseAlbum });
});
}

Expand Down
13 changes: 12 additions & 1 deletion backend/src/domain/album/dto/createAlbumResponse.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { ApiProperty } from "@nestjs/swagger";
import { Exclude } from "class-transformer";
import { Album } from "../album.entity";

export class CreateAlbumResponseDto {
@Exclude()
@ApiProperty()
albumId: number;
private readonly albumId: number;

constructor(album: Album) {
this.albumId = album.albumId;
}

static returnDto(album: Album) {
return new CreateAlbumResponseDto(album);
}
}
2 changes: 1 addition & 1 deletion backend/src/domain/album/spec/album.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe("AlbumService", () => {

const result = await albumService.updateAlbumInfo(1, { albumName: "updateName" });

expect(result).toBe("AlbumInfo update success!!");
expect(result).toStrictEqual(updateAlbum);
});
});

Expand Down
3 changes: 1 addition & 2 deletions backend/src/domain/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import { NaverOauthGuard } from "./guard/naver-auth.guard";
import { JwtAuthGuard } from "./guard/jwt-auth-guard";
import { CustomRequest } from "src/custom/myRequest/customRequest";
import { UserService } from "src/domain/user/user.service";
import { AuthService } from "./auth.service";
import { NaverFilter } from "src/filter/naver.filter";
import { JwtRefreshTokenAuthGuard } from "./guard/jwt-refreshToken-auth-guard";

@ApiTags("auth API")
@ApiBearerAuth()
@Controller("/auth")
export class AuthController {
constructor(private readonly userService: UserService, private readonly authService: AuthService) {}
constructor(private readonly userService: UserService) {}

@Get("/login")
@UseGuards(NaverOauthGuard)
Expand Down
18 changes: 0 additions & 18 deletions backend/src/domain/auth/spec/auth.service.spec.ts

This file was deleted.

17 changes: 15 additions & 2 deletions backend/src/domain/group/dto/createGroupResponse.dto.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import { ApiProperty } from "@nestjs/swagger";
import { Exclude } from "class-transformer";
import { Group } from "../group.entity";

export class CreateGroupResponseDto {
@Exclude()
@ApiProperty()
groupImage: string;
private readonly groupId: number;

@Exclude()
@ApiProperty()
groupId: number;
private readonly groupImage: string;

constructor(group: Group) {
this.groupId = group.groupId;
this.groupImage = group.groupImage;
}

static returnDto(group: Group) {
return new CreateGroupResponseDto(group);
}
}
36 changes: 27 additions & 9 deletions backend/src/domain/group/dto/getAlbumsResponse.dto.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,52 @@
import { ApiProperty } from "@nestjs/swagger";
import { Exclude } from "class-transformer";

class postList {
@Exclude()
@ApiProperty()
postId: number;
private readonly postId: number;

@Exclude()
@ApiProperty()
postTitle: string;
private readonly postTitle: string;

@Exclude()
@ApiProperty()
postLatitude: number;
private readonly postLatitude: number;

@Exclude()
@ApiProperty()
postLongitude: number;
private readonly postLongitude: number;
}

class albumList {
@Exclude()
@ApiProperty()
albumId: number;
private readonly albumId: number;

@Exclude()
@ApiProperty()
albumName: string;
private readonly albumName: string;

@Exclude()
@ApiProperty()
base: boolean;
private readonly base: boolean;

@Exclude()
@ApiProperty({ type: [postList] })
posts: postList[];
private readonly posts: postList[];
}

export class GetAlbumsResponseDto {
@Exclude()
@ApiProperty({ type: [albumList] })
albums: albumList[];
private readonly albums: albumList[];

constructor(array: any[]) {
this.albums = array;
}

static returnDto(array: any[]) {
return new GetAlbumsResponseDto(array);
}
}
17 changes: 15 additions & 2 deletions backend/src/domain/group/dto/getGroupInfoResponse.dto.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import { ApiProperty } from "@nestjs/swagger";
import { Exclude } from "class-transformer";
import { UserInfoResponseDto } from "../../user/dto/userInfoResponse.dto";
import { Group } from "../group.entity";

export class GetGroupInfoResponseDto {
@Exclude()
@ApiProperty()
groupCode: string;
private readonly groupCode: string;

@Exclude()
@ApiProperty({ type: [UserInfoResponseDto] })
users: UserInfoResponseDto[];
private readonly users: UserInfoResponseDto[];

constructor(group: Group) {
this.groupCode = group.groupCode;
this.users = group.users;
}

static returnDto(group: Group) {
return new GetGroupInfoResponseDto(group);
}
}
12 changes: 11 additions & 1 deletion backend/src/domain/group/dto/updateGroupInfoResponse.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { ApiProperty } from "@nestjs/swagger";
import { Exclude } from "class-transformer";

export class UpdateGroupInfoResponseDto {
@Exclude()
@ApiProperty()
groupImage: string;
private readonly groupImage: string;

constructor(groupImage: string) {
this.groupImage = groupImage;
}

static returnDto(groupImage: string) {
return new UpdateGroupInfoResponseDto(groupImage);
}
}
Loading

0 comments on commit 13e217d

Please sign in to comment.