Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] ChecklistAnswer 엔티티 추가 #85

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 4 additions & 23 deletions src/main/java/ttakkeun/ttakkeun_server/entity/ChecklistAnswer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.Check;
import ttakkeun.ttakkeun_server.entity.common.BaseEntity;

import java.util.ArrayList;
import java.util.List;

@Entity
@Getter
@Builder
Expand All @@ -16,29 +12,14 @@
public class ChecklistAnswer extends BaseEntity {

@Id
@Column(name = "anwser_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long answerId;
@Column(name = "checklist_answer_id")
private Long checklistAnswerId;

private String answerText;
private String checklistAnswerText;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "question_id")
private ChecklistQuestion question;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "record_id")
private Record record;

@Builder.Default
@OneToMany(mappedBy = "answer", cascade = CascadeType.ALL)
private List<Image> images = new ArrayList<>();
private ChecklistQuestion checklistQuestion;

public void addImages(List<Image> images) {
for (Image image : images) {
image.setAnswer(this);
this.images.add(image);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public class ChecklistQuestion extends BaseEntity {

@Builder.Default
@OneToMany(mappedBy = "question", cascade = CascadeType.ALL)
private List<ChecklistAnswer> answerList = new ArrayList<>();
private List<UserAnswer> answerList = new ArrayList<>();

}
2 changes: 1 addition & 1 deletion src/main/java/ttakkeun/ttakkeun_server/entity/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public class Image {
@Setter
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "answer_id")
private ChecklistAnswer answer;
private UserAnswer answer;

}
2 changes: 1 addition & 1 deletion src/main/java/ttakkeun/ttakkeun_server/entity/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class Record extends BaseEntity {

@Builder.Default
@OneToMany(mappedBy = "record", cascade = CascadeType.ALL)
private List<ChecklistAnswer> answerList = new ArrayList<>();
private List<UserAnswer> answerList = new ArrayList<>();

@OneToMany(mappedBy = "record")
private List<Result> results = new ArrayList<>();
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/ttakkeun/ttakkeun_server/entity/UserAnswer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ttakkeun.ttakkeun_server.entity;

import jakarta.persistence.*;
import lombok.*;
import ttakkeun.ttakkeun_server.entity.common.BaseEntity;

import java.util.ArrayList;
import java.util.List;

@Entity
@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class UserAnswer extends BaseEntity {

@Id
@Column(name = "user_anwser_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long userAnswerId;

private String userAnswerText;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "question_id")
private ChecklistQuestion question;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "record_id")
private Record record;

@Builder.Default
@OneToMany(mappedBy = "answer", cascade = CascadeType.ALL)
private List<Image> imageList = new ArrayList<>();

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.data.jpa.repository.JpaRepository;
import ttakkeun.ttakkeun_server.entity.ChecklistAnswer;
import ttakkeun.ttakkeun_server.entity.UserAnswer;

import java.util.Optional;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ttakkeun.ttakkeun_server.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import ttakkeun.ttakkeun_server.entity.UserAnswer;

public interface UserAnswerRepository extends JpaRepository<UserAnswer, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
import ttakkeun.ttakkeun_server.entity.*;
import ttakkeun.ttakkeun_server.entity.Record;
import ttakkeun.ttakkeun_server.entity.enums.Category;
import ttakkeun.ttakkeun_server.repository.ChecklistAnswerRepository;
import ttakkeun.ttakkeun_server.repository.ChecklistQuestionRepository;
import ttakkeun.ttakkeun_server.repository.PetRepository;
import ttakkeun.ttakkeun_server.repository.RecordRepository;
import ttakkeun.ttakkeun_server.repository.*;

import java.io.IOException;
import java.time.LocalDate;
Expand All @@ -41,6 +38,7 @@ public class RecordService {
private final PetRepository petRepository;
private final ChecklistQuestionRepository checklistQuestionRepository;
private final ChecklistAnswerRepository checklistAnswerRepository;
private final UserAnswerRepository userAnswerRepository;
private final S3ImageService s3ImageService;

public List<RecordListResponseDto> getRecordsByCategory(Member member, Long petId, Category category, int page, int size) {
Expand Down Expand Up @@ -92,19 +90,19 @@ public RecordResponseDTO.RegisterResultDTO registerRecord(Long petId, RecordRequ
.etc(request.getEtc())
.build();

List<ChecklistAnswer> answerList = new ArrayList<>();
List<UserAnswer> answerList = new ArrayList<>();
for (RecordRequestDTO.AnswerDTO answerDTO : request.getAnswers()) {
ChecklistQuestion question = checklistQuestionRepository.findById(answerDTO.getQuestionId())
.orElseThrow(() -> new ExceptionHandler(QUESTION_NOT_FOUND));

ChecklistAnswer answer = ChecklistAnswer.builder()
.answerText(answerDTO.getAnswerText())
UserAnswer answer = UserAnswer.builder()
.userAnswerText(answerDTO.getAnswerText())
.question(question)
.record(record)
.build();

answerList.add(answer);
checklistAnswerRepository.save(answer);
userAnswerRepository.save(answer);
}

record.getAnswerList().addAll(answerList);
Expand Down
Loading