Skip to content

Commit

Permalink
Type: 다중 이미지 저장 & 조회 구현 (#88)
Browse files Browse the repository at this point in the history
[Feat] 다중 이미지 저장 & 조회 구현
  • Loading branch information
pingowl authored Nov 14, 2023
2 parents 1f0e342 + a8cdff5 commit 452af80
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 48 deletions.
39 changes: 39 additions & 0 deletions src/main/generated/igoMoney/BE/domain/QImage.java
Original file line number Diff line number Diff line change
@@ -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<Image> {

private static final long serialVersionUID = -1431651427L;

public static final QImage image = new QImage("image");

public final NumberPath<Long> id = createNumber("id", Long.class);

public final StringPath uuid = createString("uuid");

public QImage(String variable) {
super(Image.class, forVariable(variable));
}

public QImage(Path<? extends Image> path) {
super(path.getType(), path.getMetadata());
}

public QImage(PathMetadata metadata) {
super(Image.class, metadata);
}

}

2 changes: 1 addition & 1 deletion src/main/generated/igoMoney/BE/domain/QRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class QRecord extends EntityPathBase<Record> {

public final NumberPath<Long> id = createNumber("id", Long.class);

public final StringPath image = createString("image");
public final ListPath<Image, QImage> image = this.<Image, QImage>createList("image", Image.class, QImage.class, PathInits.DIRECT2);

//inherited
public final DateTimePath<java.time.LocalDateTime> modifiedDate = _super.modifiedDate;
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/igoMoney/BE/domain/Image.java
Original file line number Diff line number Diff line change
@@ -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;
}
25 changes: 21 additions & 4 deletions src/main/java/igoMoney/BE/domain/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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> image = new ArrayList<Image>();

@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> 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<Image> aList){
this.image.clear();
if (aList != null){
this.image.addAll(aList);
}
}
}
4 changes: 3 additions & 1 deletion src/main/java/igoMoney/BE/dto/request/RecordSaveRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import lombok.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Getter
@Setter
@NoArgsConstructor
Expand All @@ -21,5 +23,5 @@ public class RecordSaveRequest {
@Size(max=300)
private String content;
private Integer cost;
private MultipartFile image;
private List<MultipartFile> image;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import lombok.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Getter
@Setter
@NoArgsConstructor
Expand All @@ -19,5 +21,5 @@ public class RecordUpdateRequest {
@Size(max=300)
private String content;
private Integer cost;
private MultipartFile image;
private List<MultipartFile> image;
}
3 changes: 2 additions & 1 deletion src/main/java/igoMoney/BE/dto/response/RecordResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Getter;

import java.time.LocalDate;
import java.util.List;

@Getter
@Builder
Expand All @@ -14,7 +15,7 @@ public class RecordResponse {
private Long userId;
private String title;
private String content;
private String image;
private List<String> image;
private Integer cost;
private LocalDate date;
private Boolean hide;
Expand Down
79 changes: 39 additions & 40 deletions src/main/java/igoMoney/BE/service/RecordService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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> image = multipartFilesToUUIDStrings(request.getImage());
Record record = Record.builder()
.challenge(challenge)
.user(findUser)
Expand All @@ -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<RecordResponse> getUserDailyRecordList(Long userId, LocalDate date) {

List<RecordResponse> responseList = new ArrayList<>();
List<Record> 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<RecordResponse> responses = new ArrayList<>();
List<Record> records = recordRepository.findAllByUserIdAndDate(userId, date);
for (Record record: records){
responses.add(recordToRecordResponse(record));
}

return responseList;
return responses;
}

// 사용자의 모든 record 삭제 (회원탈퇴 시)
Expand All @@ -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<Image> imageUUIDs = multipartFilesToUUIDStrings(request.getImage());
findRecord.updateRecord(request.getTitle(), request.getContent(), request.getCost(), imageUUIDs);
}

public Long reportRecord(RecordReportRequest request) {
Expand Down Expand Up @@ -203,6 +172,36 @@ private void checkPermission(User user, Long ChallengeId) {
}
}

private List<Image> multipartFilesToUUIDStrings(List<MultipartFile> multipartFiles) throws IOException {

if ((multipartFiles == null) || multipartFiles.isEmpty()) {
throw new CustomException(ErrorCode.SHOULD_EXIST_IMAGE);
}
List<Image> 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<String> 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) {
Expand Down

0 comments on commit 452af80

Please sign in to comment.