|
1 | 1 | import { Injectable } from '@nestjs/common';
|
| 2 | +import { InjectRepository } from '@nestjs/typeorm'; |
| 3 | +import { ClothingService } from 'src/clothing/clothing.service'; |
| 4 | +import { Clothing } from 'src/common/entities/clothing.entity'; |
| 5 | +import { PostClothing } from 'src/common/entities/post-clothing.entity'; |
| 6 | +import { Post } from 'src/common/entities/post.entity'; |
| 7 | +import { UploadClothingDto } from 'src/post/dtos/create-post.dto'; |
| 8 | +import { PatchClothingDto } from 'src/post/dtos/patch-Post.dto'; |
| 9 | +import { QueryRunner, Repository } from 'typeorm'; |
2 | 10 |
|
3 | 11 | @Injectable()
|
4 |
| -export class PostClothingService {} |
| 12 | +export class PostClothingService { |
| 13 | + constructor( |
| 14 | + @InjectRepository(PostClothing) |
| 15 | + private readonly postClothingRepository: Repository<PostClothing>, |
| 16 | + private readonly clothingService: ClothingService, |
| 17 | + ) {} |
| 18 | + |
| 19 | + // 옷 정보 저장 |
| 20 | + async savePostClothings( |
| 21 | + post: Post, |
| 22 | + uploadClothingDtos: UploadClothingDto[], |
| 23 | + queryRunner?: QueryRunner, |
| 24 | + ): Promise<void> { |
| 25 | + const savedClothings = await this.clothingService.saveClothings( |
| 26 | + uploadClothingDtos, |
| 27 | + queryRunner, |
| 28 | + ); |
| 29 | + |
| 30 | + const postClothingEntities = savedClothings.map((clothing: Clothing) => |
| 31 | + this.postClothingRepository.create({ |
| 32 | + post, |
| 33 | + clothing, |
| 34 | + }), |
| 35 | + ); |
| 36 | + |
| 37 | + await queryRunner.manager.save(postClothingEntities); |
| 38 | + } |
| 39 | + |
| 40 | + // 옷 정보 수정 |
| 41 | + async updatePostClothings( |
| 42 | + post: Post, |
| 43 | + uploadClothingDtos: PatchClothingDto[], |
| 44 | + queryRunner?: QueryRunner, |
| 45 | + ): Promise<void> { |
| 46 | + const existingPostClothings = await this.postClothingRepository.find({ |
| 47 | + where: { post: post, status: 'activated' }, |
| 48 | + relations: ['clothing'], |
| 49 | + }); |
| 50 | + |
| 51 | + // 빈 배열이 들어온 경우 |
| 52 | + if (uploadClothingDtos.length === 0) { |
| 53 | + const clothingsToDeactivate = existingPostClothings.filter( |
| 54 | + (existingClothing) => existingClothing.status === 'activated', |
| 55 | + ); |
| 56 | + |
| 57 | + await this.deletePostClothing(clothingsToDeactivate, queryRunner); |
| 58 | + return; // 함수 종료 |
| 59 | + } |
| 60 | + |
| 61 | + // 삭제할 PostClothing |
| 62 | + const postClothingsToRemove = existingPostClothings.filter( |
| 63 | + (existingPostClothing) => |
| 64 | + existingPostClothing.status === 'activated' && |
| 65 | + !uploadClothingDtos.some( |
| 66 | + (newClothing) => newClothing.id === existingPostClothing.clothing.id, |
| 67 | + ), |
| 68 | + ); |
| 69 | + |
| 70 | + if (postClothingsToRemove.length > 0) { |
| 71 | + await this.deletePostClothing(postClothingsToRemove, queryRunner); |
| 72 | + } |
| 73 | + |
| 74 | + // PostClothing 추가 및 수정 |
| 75 | + const newPostClothings: PostClothing[] = []; |
| 76 | + |
| 77 | + for (const newClothing of uploadClothingDtos) { |
| 78 | + const existingPostClothing = existingPostClothings.find( |
| 79 | + (postClothing) => postClothing.clothing.id === newClothing.id, |
| 80 | + ); |
| 81 | + |
| 82 | + if (existingPostClothing) { |
| 83 | + // Clothing 정보 수정 |
| 84 | + await this.clothingService.updateClothing(newClothing, queryRunner); |
| 85 | + } else { |
| 86 | + // 새로운 Clothing 추가 |
| 87 | + const newClothingEntity = await this.clothingService.saveClothings( |
| 88 | + [newClothing], |
| 89 | + queryRunner, |
| 90 | + ); |
| 91 | + for (const clothing of newClothingEntity) { |
| 92 | + const newPostClothingEntity = this.postClothingRepository.create({ |
| 93 | + post, |
| 94 | + clothing, |
| 95 | + }); |
| 96 | + newPostClothings.push(newPostClothingEntity); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + if (newPostClothings.length > 0) { |
| 101 | + await queryRunner.manager.save(newPostClothings); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // PostClothing 및 Clothing 삭제 처리 |
| 106 | + async deletePostClothing( |
| 107 | + postClothing: PostClothing[], |
| 108 | + queryRunner: QueryRunner, |
| 109 | + ): Promise<void> { |
| 110 | + await Promise.all( |
| 111 | + postClothing.map(async (postClothingItem) => { |
| 112 | + postClothingItem.status = 'deactivated'; |
| 113 | + postClothingItem.softDelete(); |
| 114 | + |
| 115 | + await this.clothingService.deleteClothing( |
| 116 | + postClothingItem.clothing, |
| 117 | + queryRunner, |
| 118 | + ); |
| 119 | + |
| 120 | + await queryRunner.manager.save(postClothingItem); |
| 121 | + }), |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + // Post에 연결된 PostClothing 및 Clothing 삭제 처리 |
| 126 | + async deletePostClothingByPostId( |
| 127 | + postId: number, |
| 128 | + queryRunner: QueryRunner, |
| 129 | + ): Promise<void> { |
| 130 | + const clothingToRemove = await queryRunner.manager.find(PostClothing, { |
| 131 | + where: { post: { id: postId } }, |
| 132 | + relations: ['clothing'], |
| 133 | + }); |
| 134 | + |
| 135 | + await Promise.all( |
| 136 | + clothingToRemove.map(async (Postclothing) => { |
| 137 | + Postclothing.status = 'deactivated'; |
| 138 | + Postclothing.softDelete(); |
| 139 | + await this.clothingService.deleteClothing(Postclothing.clothing, queryRunner); |
| 140 | + |
| 141 | + return queryRunner.manager.save(Postclothing); |
| 142 | + }), |
| 143 | + ); |
| 144 | + } |
| 145 | +} |
0 commit comments