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

fix: lecutre 테이블 컬럼 수정 #1065

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,4 @@ public ResponseEntity<Void> deleteTimetableById(
timetableService.deleteTimetableLecture(userId, timetableId);
return ResponseEntity.ok().build();
}

}
23 changes: 13 additions & 10 deletions src/main/java/in/koreatech/koin/domain/timetable/model/Lecture.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
Expand All @@ -24,11 +27,6 @@ public class Lecture {
@GeneratedValue(strategy = IDENTITY)
private Integer id;

@Size(max = 255)
@NotNull
@Column(name = "semester_date", nullable = false)
private String semester;

@Size(max = 255)
@NotNull
@Column(name = "code", nullable = false)
Expand Down Expand Up @@ -86,17 +84,21 @@ public class Lecture {
@Column(name = "class_time", nullable = false)
private String classTime;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "semester_id")
private Semester semester;

@Builder
private Lecture(
String code, String semester,
String name, String grades, String lectureClass,
public Lecture(
String code, String name,
String grades, String lectureClass,
String regularNumber, String department,
String target, String professor,
String isEnglish, String designScore,
String isElearning, String classTime
String isElearning, String classTime,
Semester semester
) {
this.code = code;
this.semester = semester;
this.name = name;
this.grades = grades;
this.lectureClass = lectureClass;
Expand All @@ -108,5 +110,6 @@ private Lecture(
this.designScore = designScore;
this.isElearning = isElearning;
this.classTime = classTime;
this.semester = semester;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PROTECTED;

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

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
Expand All @@ -29,8 +33,12 @@ public class Semester {
@Column(name = "semester", nullable = false, unique = true)
private String semester;

@OneToMany(mappedBy = "semester")
private List<Lecture> lectures = new ArrayList<>();

@Builder
private Semester(String semester) {
public Semester(String semester, List<Lecture> lectures) {
this.semester = semester;
this.lectures = lectures;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@
import in.koreatech.koin.domain.timetable.exception.LectureNotFoundException;
import in.koreatech.koin.domain.timetable.exception.SemesterNotFoundException;
import in.koreatech.koin.domain.timetable.model.Lecture;
import in.koreatech.koin.domain.timetable.model.Semester;

public interface LectureRepository extends Repository<Lecture, Integer> {

List<Lecture> findBySemester(String semesterDate);
List<Lecture> findBySemester(Semester semester);

Lecture save(Lecture lecture);

Optional<Lecture> findById(Integer id);

Optional<Lecture> findBySemesterAndCodeAndLectureClass(String semesterDate, String code, String classLecture);
Optional<Lecture> findBySemesterAndCodeAndLectureClass(Semester semester, String code, String classLecture);

default Lecture getBySemesterAndCodeAndLectureClass(String semesterDate, String code, String classLecture) {
return findBySemesterAndCodeAndLectureClass(semesterDate, code, classLecture)
.orElseThrow(() -> SemesterNotFoundException.withDetail("semester: " + semesterDate + " code: " + code + " classLecture: " + classLecture));
default Lecture getBySemesterAndCodeAndLectureClass(Semester semester, String code, String classLecture) {
return findBySemesterAndCodeAndLectureClass(semester, code, classLecture)
.orElseThrow(() -> SemesterNotFoundException.withDetail("semester: " + semester.getSemester() + " code: " + code + " classLecture: " + classLecture));
}

default Lecture getLectureById(Integer id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public class TimetableService {
private final UserRepository userRepository;
private final EntityManager entityManager;

public List<LectureResponse> getLecturesBySemester(String semester) {
semesterRepositoryV2.getBySemester(semester);
public List<LectureResponse> getLecturesBySemester(String requestSemester) {
Semester semester = semesterRepositoryV2.getBySemester(requestSemester);
List<Lecture> lectures = lectureRepositoryV2.findBySemester(semester);
return lectures.stream()
.map(LectureResponse::from)
Expand All @@ -57,7 +57,7 @@ public TimetableResponse createTimetables(Integer userId, TimetableCreateRequest
semester.getId());

for (TimetableCreateRequest.InnerTimetableRequest timeTable : request.timetable()) {
Lecture lecture = lectureRepositoryV2.getBySemesterAndCodeAndLectureClass(request.semester(),
Lecture lecture = lectureRepositoryV2.getBySemesterAndCodeAndLectureClass(semester,
timeTable.code(), timeTable.lectureClass());
TimetableLecture timetableLecture = TimetableLecture.builder()
.classPlace(timeTable.classPlace())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@
import in.koreatech.koin.domain.timetable.exception.LectureNotFoundException;
import in.koreatech.koin.domain.timetable.exception.SemesterNotFoundException;
import in.koreatech.koin.domain.timetable.model.Lecture;
import in.koreatech.koin.domain.timetable.model.Semester;

public interface LectureRepositoryV2 extends Repository<Lecture, Integer> {

List<Lecture> findBySemester(String semesterDate);
List<Lecture> findBySemester(Semester semester);

Lecture save(Lecture lecture);

Optional<Lecture> findById(Integer id);

Optional<Lecture> findBySemesterAndCodeAndLectureClass(String semesterDate, String code, String classLecture);
Optional<Lecture> findBySemesterAndCodeAndLectureClass(Semester semester, String code, String classLecture);

default Lecture getBySemesterAndCodeAndLectureClass(String semesterDate, String code, String classLecture) {
return findBySemesterAndCodeAndLectureClass(semesterDate, code, classLecture)
default Lecture getBySemesterAndCodeAndLectureClass(Semester semester, String code, String classLecture) {
return findBySemesterAndCodeAndLectureClass(semester, code, classLecture)
.orElseThrow(() -> SemesterNotFoundException.withDetail(
"semester: " + semesterDate + " code: " + code + " classLecture: " + classLecture));
"semester: " + semester.getSemester() + " code: " + code + " classLecture: " + classLecture));
}

default Lecture getLectureById(Integer id) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE `koin`.`lectures`
MODIFY COLUMN `class_time` VARCHAR(255) NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ALTER TABLE `koin`.`lectures`
ADD COLUMN `semester_id` INT UNSIGNED NOT NULL;

UPDATE `koin`.`lectures` l
SET `semester_id` = (SELECT `id`
FROM `koin`.`semester` s
WHERE l.`semester_date` = s.`semester`);
Comment on lines +4 to +7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C

DML이랑 DDL이 같이 있네요👀(분리..!!)
추가적으로 최근에 진호님이 PR 올리신 내용이 있어서 한 번 확인해보시는 것도 좋을 거 같아요

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

헉,, 감사합니다 !


ALTER TABLE `koin`.`lectures`
ADD CONSTRAINT `FK_LECTURES_ON_SEMESTER`
FOREIGN KEY (`semester_id`)
REFERENCES `koin`.`semester` (`id`)
ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE `koin`.`lectures`
DROP COLUMN `semester_date`;
Comment on lines +1 to +2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C

컬럼 삭제..하는 과정에서 flyway 오류가 안 날지 모르겠네용

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

흠,, 일단은 컬럼을 삭제하지 않는 방향이 적절할까요..?
지금 그렇게 큰 문제가 되지는 않는 것 같아서, 있어도 그만, 없어도 그만이라고 생각은 듭니다.

36 changes: 17 additions & 19 deletions src/test/java/in/koreatech/koin/acceptance/TimetableApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,14 @@ void setup() {

@Test
void 특정_학기_강의를_조회한다() throws Exception {
semesterFixture.semester("20192");
semesterFixture.semester("20201");
String semester = "20201";
Semester semester1 = semesterFixture.semester("20192");
Semester semester = semesterFixture.semester("20201");
lectureFixture.HRD_개론(semester);
lectureFixture.건축구조의_이해_및_실습("20192");
lectureFixture.건축구조의_이해_및_실습(semester1);

mockMvc.perform(
get("/lectures")
.param("semester_date", semester)
.param("semester_date", semester.getSemester())
.contentType(MediaType.APPLICATION_JSON)
)
.andExpect(status().isOk())
Expand Down Expand Up @@ -85,15 +84,14 @@ void setup() {

@Test
void 특정_학기_강의들을_조회한다() throws Exception {
semesterFixture.semester("20201");
String semester = "20201";
Semester semester = semesterFixture.semester("20201");
lectureFixture.HRD_개론(semester);
lectureFixture.건축구조의_이해_및_실습(semester);
lectureFixture.재료역학(semester);

mockMvc.perform(
get("/lectures")
.param("semester_date", semester)
.param("semester_date", semester.getSemester())
.contentType(MediaType.APPLICATION_JSON)
)
.andExpect(status().isOk())
Expand Down Expand Up @@ -156,7 +154,7 @@ void setup() {

@Test
void 존재하지_않는_학기를_조회하면_404() throws Exception {
String semester = "20201";
Semester semester = semesterFixture.semester("20201");
lectureFixture.HRD_개론(semester);
lectureFixture.건축구조의_이해_및_실습(semester);

Expand Down Expand Up @@ -239,8 +237,8 @@ void setup() {
String token = userFixture.getToken(user);
Semester semester = semesterFixture.semester("20192");

Lecture 건축구조의_이해_및_실습 = lectureFixture.건축구조의_이해_및_실습(semester.getSemester());
Lecture HRD_개론 = lectureFixture.HRD_개론(semester.getSemester());
Lecture 건축구조의_이해_및_실습 = lectureFixture.건축구조의_이해_및_실습(semester);
Lecture HRD_개론 = lectureFixture.HRD_개론(semester);

timetableV2Fixture.시간표6(user, semester, 건축구조의_이해_및_실습, HRD_개론);

Expand Down Expand Up @@ -322,8 +320,8 @@ void setup() {
String token = userFixture.getToken(user);
Semester semester1 = semesterFixture.semester("20192");
Semester semester2 = semesterFixture.semester("20201");
Lecture HRD_개론 = lectureFixture.HRD_개론(semester1.getSemester());
Lecture 건축구조의_이해_및_실습 = lectureFixture.건축구조의_이해_및_실습(semester2.getSemester());
Lecture HRD_개론 = lectureFixture.HRD_개론(semester1);
Lecture 건축구조의_이해_및_실습 = lectureFixture.건축구조의_이해_및_실습(semester2);
timetableV2Fixture.시간표6(user, semester1, HRD_개론, null);
timetableV2Fixture.시간표6(user, semester2, 건축구조의_이해_및_실습, null);

Expand All @@ -350,8 +348,8 @@ void setup() {
String token = userFixture.getToken(user);
Semester semester = semesterFixture.semester("20192");

lectureFixture.건축구조의_이해_및_실습(semester.getSemester());
lectureFixture.HRD_개론(semester.getSemester());
lectureFixture.건축구조의_이해_및_실습(semester);
lectureFixture.HRD_개론(semester);

timetableV2Fixture.시간표1(user, semester);

Expand Down Expand Up @@ -443,8 +441,8 @@ void setup() {
String token = userFixture.getToken(user);
Semester semester = semesterFixture.semester("20192");

lectureFixture.건축구조의_이해_및_실습(semester.getSemester());
lectureFixture.HRD_개론(semester.getSemester());
lectureFixture.건축구조의_이해_및_실습(semester);
lectureFixture.HRD_개론(semester);

timetableV2Fixture.시간표1(user, semester);

Expand Down Expand Up @@ -550,8 +548,8 @@ void setup() {
String token = userFixture.getToken(user);
Semester semester = semesterFixture.semester("20192");

Lecture 건축구조의_이해_및_실습 = lectureFixture.건축구조의_이해_및_실습(semester.getSemester());
Lecture HRD_개론 = lectureFixture.HRD_개론(semester.getSemester());
Lecture 건축구조의_이해_및_실습 = lectureFixture.건축구조의_이해_및_실습(semester);
Lecture HRD_개론 = lectureFixture.HRD_개론(semester);

timetableV2Fixture.시간표6(user, semester, 건축구조의_이해_및_실습, HRD_개론);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ void setup() {
User user = userFixture.준호_학생().getUser();
String token = userFixture.getToken(user);
Semester semester = semesterFixture.semester("20192");
Lecture lecture = lectureFixture.HRD_개론(semester.getSemester());
Lecture lecture = lectureFixture.HRD_개론(semester);

TimetableFrame frame1 = timetableV2Fixture.시간표5(user, semester, lecture);

Expand Down Expand Up @@ -425,8 +425,8 @@ void setup() {
String token = userFixture.getToken(user);
Semester semester = semesterFixture.semester("20192");

Lecture 건축구조의_이해_및_실습 = lectureFixture.건축구조의_이해_및_실습(semester.getSemester());
Lecture HRD_개론 = lectureFixture.HRD_개론(semester.getSemester());
Lecture 건축구조의_이해_및_실습 = lectureFixture.건축구조의_이해_및_실습(semester);
Lecture HRD_개론 = lectureFixture.HRD_개론(semester);

TimetableFrame frame = timetableV2Fixture.시간표6(user, semester, 건축구조의_이해_및_실습, HRD_개론);

Expand Down Expand Up @@ -485,8 +485,8 @@ void setup() {
User user1 = userFixture.준호_학생().getUser();
String token = userFixture.getToken(user1);
Semester semester = semesterFixture.semester("20192");
Lecture lecture1 = lectureFixture.HRD_개론("20192");
Lecture lecture2 = lectureFixture.영어청해("20192");
Lecture lecture1 = lectureFixture.HRD_개론(semester);
Lecture lecture2 = lectureFixture.영어청해(semester);
TimetableFrame frame = timetableV2Fixture.시간표4(user1, semester, lecture1, lecture2);

Integer lectureId = lecture1.getId();
Expand All @@ -505,8 +505,8 @@ void setup() {
User user1 = userFixture.준호_학생().getUser();
String token = userFixture.getToken(user1);
Semester semester = semesterFixture.semester("20192");
Lecture lecture1 = lectureFixture.HRD_개론("20192");
Lecture lecture2 = lectureFixture.영어청해("20192");
Lecture lecture1 = lectureFixture.HRD_개론(semester);
Lecture lecture2 = lectureFixture.영어청해(semester);
TimetableFrame frame = timetableV2Fixture.시간표4(user1, semester, lecture1, lecture2);

Integer frameId = frame.getId();
Expand All @@ -525,8 +525,8 @@ void setup() {
User user1 = userFixture.준호_학생().getUser();
String token = userFixture.getToken(user1);
Semester semester = semesterFixture.semester("20192");
Lecture lecture1 = lectureFixture.HRD_개론("20192");
Lecture lecture2 = lectureFixture.영어청해("20192");
Lecture lecture1 = lectureFixture.HRD_개론(semester);
Lecture lecture2 = lectureFixture.영어청해(semester);
TimetableFrame frame = timetableV2Fixture.시간표4(user1, semester, lecture1, lecture2);

List<Integer> timetableLectureIds = frame.getTimetableLectures().stream()
Expand Down
9 changes: 5 additions & 4 deletions src/test/java/in/koreatech/koin/fixture/LectureFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.stereotype.Component;

import in.koreatech.koin.domain.timetable.model.Lecture;
import in.koreatech.koin.domain.timetable.model.Semester;
import in.koreatech.koin.domain.timetable.repository.LectureRepository;

@Component
Expand All @@ -15,7 +16,7 @@ public LectureFixture(LectureRepository lectureRepository) {
this.lectureRepository = lectureRepository;
}

public Lecture 건축구조의_이해_및_실습(String semester) {
public Lecture 건축구조의_이해_및_실습(Semester semester) {
return lectureRepository.save(
Lecture.builder()
.code("ARB244")
Expand All @@ -35,7 +36,7 @@ public LectureFixture(LectureRepository lectureRepository) {
);
}

public Lecture HRD_개론(String semester) {
public Lecture HRD_개론(Semester semester) {
return lectureRepository.save(
Lecture.builder()
.code("BSM590")
Expand All @@ -55,7 +56,7 @@ public LectureFixture(LectureRepository lectureRepository) {
);
}

public Lecture 재료역학(String semester) {
public Lecture 재료역학(Semester semester) {
return lectureRepository.save(
Lecture.builder()
.code("MEB311")
Expand All @@ -75,7 +76,7 @@ public LectureFixture(LectureRepository lectureRepository) {
);
}

public Lecture 영어청해(String semester) {
public Lecture 영어청해(Semester semester) {
return lectureRepository.save(
Lecture.builder()
.code("LAN324")
Expand Down
Loading