Skip to content

Commit

Permalink
[#3] feat : 양방향 매핑 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
sangminee committed May 13, 2023
1 parent 7484cb7 commit 6386d86
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
13 changes: 8 additions & 5 deletions src/main/java/com/mju/course/application/CourseServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mju.course.domain.model.Course;
import com.mju.course.domain.model.Curriculum;
import com.mju.course.domain.model.Lecture;
import com.mju.course.domain.model.Skill;
import com.mju.course.domain.model.other.Exception.CourseException;
import com.mju.course.domain.model.other.Result.CommonResult;
import com.mju.course.domain.repository.course.CourseRepository;
Expand Down Expand Up @@ -58,14 +59,16 @@ public CommonResult readCourse(Long course_index) {
courseRepository.save(findCourse);

// 스킬
ArrayList<String> skillList = skillRepository.findByCourse(findCourse);
List<String> skillList = new ArrayList<>();
List<Skill> getSkillList = findCourse.getSkillList();
getSkillList.forEach(s->skillList.add(s.getSkill()));

// 커리 큘럼, 강의
List<Curriculum> findCurriculum = curriculumRepository.findByCourse(findCourse); // 에러 처리
ArrayList<CurriculumReadDto> curriculumReadDtoList = new ArrayList<>();
findCurriculum.forEach(curriculum -> {
List<Lecture> lectures = lectureRepository.findByCurriculum(curriculum);
List<LectureReadDto> lectureReadDtos = lectures
List<Curriculum> curriculumList = findCourse.getCurriculumList();
curriculumList.forEach(curriculum -> {
List<Lecture> lectureList = curriculum.getLectureList();
List<LectureReadDto> lectureReadDtos = lectureList
.stream()
.map(LectureReadDto::of)
.collect(Collectors.toList());
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/mju/course/domain/model/Course.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

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

@Entity
@Getter
@NoArgsConstructor
Expand Down Expand Up @@ -49,6 +52,13 @@ public class Course extends BaseTimeEntity {
@Column(name = "course_title_photo_key")
private String courseTitlePhotoKey; // 코스 대표 사진

// 양방향 매핑
@OneToMany(mappedBy = "course")
private List<Skill> skillList = new ArrayList<>();

@OneToMany(mappedBy = "course")
private List<Curriculum> curriculumList = new ArrayList<>();

@Builder
public Course(String category, String courseName, String price, String courseDescription,
int difficulty, int courseTime, Long hits, CourseState status){
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/mju/course/domain/model/Curriculum.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

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

@Entity
@Getter
@NoArgsConstructor
Expand All @@ -29,6 +32,9 @@ public class Curriculum extends BaseTimeEntity{
@Column(name = "lecture_sum")
private int lectureSum;

@OneToMany(mappedBy = "curriculum")
private List<Lecture> lectureList = new ArrayList<>();

@Builder
public Curriculum(int chapter, String curriculumTitle, int lectureSum, Course course){
this.course = course;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.mju.course.presentation.dto.response;

import com.mju.course.domain.model.Course;
import com.mju.course.domain.model.Skill;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

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

@Getter
@AllArgsConstructor
Expand All @@ -28,7 +30,6 @@ public class CourseReadDto {
private int difficulty;
@Schema(description = "코스 생성 시간")
private int courseTime;

@Schema(description = "조회수", defaultValue = "2")
private Long hits;
@Schema(description = "코스 기본(타이틀) 사진 url")
Expand All @@ -38,12 +39,12 @@ public class CourseReadDto {
private int curriculumSum;

@Schema(description = "스킬", defaultValue = "Java, Programming")
private ArrayList<String> skillList;
private List<String> skillList;

@Schema(description = "커리 큘럼 객체")
private ArrayList<CurriculumReadDto> curriculumReadDtoList;
private List<CurriculumReadDto> curriculumReadDtoList;

public static CourseReadDto of(Course course, ArrayList<String> skillList, ArrayList<CurriculumReadDto> curriculumReadDtoList){
public static CourseReadDto of(Course course, List<String> skillList, ArrayList<CurriculumReadDto> curriculumReadDtoList){
return CourseReadDto.builder()
.courseIndex(course.getId())
.category(course.getCategory())
Expand Down

0 comments on commit 6386d86

Please sign in to comment.