From a8cdff57718e3ec361f86a27c95d02e7f92c87f3 Mon Sep 17 00:00:00 2001 From: BK Date: Wed, 15 Nov 2023 02:30:37 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20=EB=8B=A4=EC=A4=91=20=EC=9D=B4=EB=AF=B8?= =?UTF-8?q?=EC=A7=80=20=EC=A0=80=EC=9E=A5&=EC=A1=B0=ED=9A=8C=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../generated/igoMoney/BE/domain/QImage.java | 39 +++++++++ .../generated/igoMoney/BE/domain/QRecord.java | 2 +- src/main/java/igoMoney/BE/domain/Image.java | 22 ++++++ src/main/java/igoMoney/BE/domain/Record.java | 25 +++++- .../BE/dto/request/RecordSaveRequest.java | 4 +- .../BE/dto/request/RecordUpdateRequest.java | 4 +- .../BE/dto/response/RecordResponse.java | 3 +- .../igoMoney/BE/service/RecordService.java | 79 +++++++++---------- 8 files changed, 130 insertions(+), 48 deletions(-) create mode 100644 src/main/generated/igoMoney/BE/domain/QImage.java create mode 100644 src/main/java/igoMoney/BE/domain/Image.java diff --git a/src/main/generated/igoMoney/BE/domain/QImage.java b/src/main/generated/igoMoney/BE/domain/QImage.java new file mode 100644 index 0000000..5e4341a --- /dev/null +++ b/src/main/generated/igoMoney/BE/domain/QImage.java @@ -0,0 +1,39 @@ +package igoMoney.BE.domain; + +import static com.querydsl.core.types.PathMetadataFactory.*; + +import com.querydsl.core.types.dsl.*; + +import com.querydsl.core.types.PathMetadata; +import javax.annotation.processing.Generated; +import com.querydsl.core.types.Path; + + +/** + * QImage is a Querydsl query type for Image + */ +@Generated("com.querydsl.codegen.DefaultEntitySerializer") +public class QImage extends EntityPathBase { + + private static final long serialVersionUID = -1431651427L; + + public static final QImage image = new QImage("image"); + + public final NumberPath id = createNumber("id", Long.class); + + public final StringPath uuid = createString("uuid"); + + public QImage(String variable) { + super(Image.class, forVariable(variable)); + } + + public QImage(Path path) { + super(path.getType(), path.getMetadata()); + } + + public QImage(PathMetadata metadata) { + super(Image.class, metadata); + } + +} + diff --git a/src/main/generated/igoMoney/BE/domain/QRecord.java b/src/main/generated/igoMoney/BE/domain/QRecord.java index 2bb05bc..c2fd8a1 100644 --- a/src/main/generated/igoMoney/BE/domain/QRecord.java +++ b/src/main/generated/igoMoney/BE/domain/QRecord.java @@ -39,7 +39,7 @@ public class QRecord extends EntityPathBase { public final NumberPath id = createNumber("id", Long.class); - public final StringPath image = createString("image"); + public final ListPath image = this.createList("image", Image.class, QImage.class, PathInits.DIRECT2); //inherited public final DateTimePath modifiedDate = _super.modifiedDate; diff --git a/src/main/java/igoMoney/BE/domain/Image.java b/src/main/java/igoMoney/BE/domain/Image.java new file mode 100644 index 0000000..748682d --- /dev/null +++ b/src/main/java/igoMoney/BE/domain/Image.java @@ -0,0 +1,22 @@ +package igoMoney.BE.domain; + +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Getter +@NoArgsConstructor +@AllArgsConstructor +@Builder +public class Image { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "image_id") + private Long id; + + private String uuid; +} diff --git a/src/main/java/igoMoney/BE/domain/Record.java b/src/main/java/igoMoney/BE/domain/Record.java index f77f8ee..9f36615 100644 --- a/src/main/java/igoMoney/BE/domain/Record.java +++ b/src/main/java/igoMoney/BE/domain/Record.java @@ -2,9 +2,14 @@ import igoMoney.BE.common.entity.BaseEntity; import jakarta.persistence.*; -import lombok.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; @Entity @Getter @@ -31,19 +36,31 @@ public class Record extends BaseEntity { @Column(nullable = false) private String content; private Integer cost; - private String image; + + @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) + @JoinColumn(name = "record_id") + @Builder.Default + private List image = new ArrayList(); + @Column(nullable = false) private LocalDate date; @Column(nullable = false) @Builder.Default private Boolean hide = false; // 신고당했을 때 가림 - public void updateRecord(String title, String content, Integer cost, String image){ + public void updateRecord(String title, String content, Integer cost, List image){ this.title = title; this.content = content; this.cost = cost; - this.image = image; + setImage(image); } public void setHidden() { this.hide = true; } + + public void setImage(List aList){ + this.image.clear(); + if (aList != null){ + this.image.addAll(aList); + } + } } diff --git a/src/main/java/igoMoney/BE/dto/request/RecordSaveRequest.java b/src/main/java/igoMoney/BE/dto/request/RecordSaveRequest.java index b885544..7000550 100644 --- a/src/main/java/igoMoney/BE/dto/request/RecordSaveRequest.java +++ b/src/main/java/igoMoney/BE/dto/request/RecordSaveRequest.java @@ -5,6 +5,8 @@ import lombok.*; import org.springframework.web.multipart.MultipartFile; +import java.util.List; + @Getter @Setter @NoArgsConstructor @@ -21,5 +23,5 @@ public class RecordSaveRequest { @Size(max=300) private String content; private Integer cost; - private MultipartFile image; + private List image; } diff --git a/src/main/java/igoMoney/BE/dto/request/RecordUpdateRequest.java b/src/main/java/igoMoney/BE/dto/request/RecordUpdateRequest.java index 26cca47..8aa15a1 100644 --- a/src/main/java/igoMoney/BE/dto/request/RecordUpdateRequest.java +++ b/src/main/java/igoMoney/BE/dto/request/RecordUpdateRequest.java @@ -5,6 +5,8 @@ import lombok.*; import org.springframework.web.multipart.MultipartFile; +import java.util.List; + @Getter @Setter @NoArgsConstructor @@ -19,5 +21,5 @@ public class RecordUpdateRequest { @Size(max=300) private String content; private Integer cost; - private MultipartFile image; + private List image; } diff --git a/src/main/java/igoMoney/BE/dto/response/RecordResponse.java b/src/main/java/igoMoney/BE/dto/response/RecordResponse.java index 04f7dea..effbfbd 100644 --- a/src/main/java/igoMoney/BE/dto/response/RecordResponse.java +++ b/src/main/java/igoMoney/BE/dto/response/RecordResponse.java @@ -4,6 +4,7 @@ import lombok.Getter; import java.time.LocalDate; +import java.util.List; @Getter @Builder @@ -14,7 +15,7 @@ public class RecordResponse { private Long userId; private String title; private String content; - private String image; + private List image; private Integer cost; private LocalDate date; private Boolean hide; diff --git a/src/main/java/igoMoney/BE/service/RecordService.java b/src/main/java/igoMoney/BE/service/RecordService.java index 19dff11..bfa076c 100644 --- a/src/main/java/igoMoney/BE/service/RecordService.java +++ b/src/main/java/igoMoney/BE/service/RecordService.java @@ -46,8 +46,7 @@ public Long saveRecord (RecordSaveRequest request) throws IOException { Challenge challenge = getChallengeOrThrow(request.getChallengeId()); checkPermission(findUser, request.getChallengeId()); - checkExistsImage(request.getImage()); - String image = imageService.uploadImage(request.getImage()); + List image = multipartFilesToUUIDStrings(request.getImage()); Record record = Record.builder() .challenge(challenge) .user(findUser) @@ -67,47 +66,18 @@ public Long saveRecord (RecordSaveRequest request) throws IOException { public RecordResponse getRecord(Long recordId) { Record record = getRecordOrThrow(recordId); - - String imageUrl = imageService.processImage(record.getImage()); - RecordResponse response = RecordResponse.builder() - .recordId(record.getId()) - .challengeId(record.getChallenge().getId()) - .userId(record.getUser().getId()) - .title(record.getTitle()) - .content(record.getContent()) - .cost(record.getCost()) - .image(imageUrl) - .date(record.getDate()) - .hide(record.getHide()) - .build(); - - return response; + return recordToRecordResponse(record); } - // 사용자의 그날의 모든 record 조회 public List getUserDailyRecordList(Long userId, LocalDate date) { - List responseList = new ArrayList<>(); - List recordList = recordRepository.findAllByUserIdAndDate(userId, date); - for (Record record: recordList){ - String image = imageService.processImage(record.getImage()); - RecordResponse recordResponse = RecordResponse.builder() - .recordId(record.getId()) - .challengeId(record.getChallenge().getId()) - .userId(record.getUser().getId()) - .title(record.getTitle()) - .content(record.getContent()) - .cost(record.getCost()) - .image(image) - .date(record.getDate()) - .hide(record.getHide()) - .build(); - - responseList.add(recordResponse); + List responses = new ArrayList<>(); + List records = recordRepository.findAllByUserIdAndDate(userId, date); + for (Record record: records){ + responses.add(recordToRecordResponse(record)); } - - return responseList; + return responses; } // 사용자의 모든 record 삭제 (회원탈퇴 시) @@ -130,9 +100,8 @@ public void deleteRecord(Long recordId) { public void updateRecord(RecordUpdateRequest request) throws IOException { Record findRecord = getRecordOrThrow(request.getRecordId()); - checkExistsImage(request.getImage()); - String image = imageService.uploadImage(request.getImage()); - findRecord.updateRecord(request.getTitle(), request.getContent(), request.getCost(), image); + List imageUUIDs = multipartFilesToUUIDStrings(request.getImage()); + findRecord.updateRecord(request.getTitle(), request.getContent(), request.getCost(), imageUUIDs); } public Long reportRecord(RecordReportRequest request) { @@ -203,6 +172,36 @@ private void checkPermission(User user, Long ChallengeId) { } } + private List multipartFilesToUUIDStrings(List multipartFiles) throws IOException { + + if ((multipartFiles == null) || multipartFiles.isEmpty()) { + throw new CustomException(ErrorCode.SHOULD_EXIST_IMAGE); + } + List uuids = new ArrayList<>(); + for (MultipartFile m : multipartFiles){ + checkExistsImage(m); + uuids.add(Image.builder().uuid(imageService.uploadImage(m)).build()); + } + return uuids; + } + + private RecordResponse recordToRecordResponse(Record record) { + List imageUrls = new ArrayList<>(); + for (Image i : record.getImage()){ + imageUrls.add(imageService.processImage(i.getUuid())); + } + return RecordResponse.builder() + .recordId(record.getId()) + .challengeId(record.getChallenge().getId()) + .userId(record.getUser().getId()) + .title(record.getTitle()) + .content(record.getContent()) + .cost(record.getCost()) + .image(imageUrls) + .date(record.getDate()) + .hide(record.getHide()) + .build(); + } // 예외 처리 - 이미지 존재여부 private void checkExistsImage(MultipartFile image) {