Skip to content

Commit

Permalink
#44 [Feat] 시간표 수정 기능 완료, [Fix] BaseEntity에서 updatedAt @LastModifiedDa…
Browse files Browse the repository at this point in the history
…te로 바꿈, dto에서 searchSubject credit을 Integer로 바꿈
  • Loading branch information
dlguszoo committed Jan 31, 2024
1 parent 3310eef commit 4eda32d
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 100 deletions.
19 changes: 15 additions & 4 deletions src/main/java/umc/forgrad/controller/TimetableController.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,47 @@
@RequiredArgsConstructor
public class TimetableController {
private final TimetableService timetableService;

@GetMapping(value = "/plans/timetable/searchHakki")
public ApiResponse<List<TimetableRequestDto.HakkiDto>> searchHakki(HttpSession session) throws IOException {
List<TimetableRequestDto.HakkiDto> hakkiDtos = timetableService.searchHakki(session);
return ApiResponse.onSuccess(hakkiDtos);
}

@GetMapping(value = "/plans/timetable/searchTrack")
public ApiResponse<List<TimetableRequestDto.TrackDto>> searchTrack(HttpSession session, String hakki) throws IOException {
List<TimetableRequestDto.TrackDto> trackDtos = timetableService.searchTrack(session, hakki);
return ApiResponse.onSuccess(trackDtos);
}

@GetMapping(value = "/plans/timetable/searchSubject")
public ApiResponse<List<TimetableRequestDto.SearchSubjectDto>> searchSubject(HttpSession session, String hakki, String track) throws IOException {
List<TimetableRequestDto.SearchSubjectDto> searchSubjectDtos = timetableService.searchSubject(session, hakki, track);
return ApiResponse.onSuccess(searchSubjectDtos);
}

@PostMapping(value = "/plans/timetable")
public ApiResponse<AddTimetableResponseDto.addResponseDtoList> addTimetable(@RequestBody TimetableRequestDto.TimetableDto timetableDto, @SessionAttribute(name="student") Long stuId) {
public ApiResponse<AddTimetableResponseDto.addResponseDtoList> addTimetable(@RequestBody TimetableRequestDto.TimetableDto timetableDto, @SessionAttribute(name = "student") Long stuId) {
AddTimetableResponseDto.addResponseDtoList addResponseDtoList = timetableService.addTimetable(timetableDto, stuId);
return ApiResponse.onSuccess(addResponseDtoList);
}

@PatchMapping(value = "/plans/timetable")
public ApiResponse<UpdateTimetableResponseDto> updateTimetable(@RequestBody TimetableRequestDto.TimetableDto timetableDto, @SessionAttribute(name="student") Long stuId) {
UpdateTimetableResponseDto updateTimetableResponseDto = timetableService.updateTimetable(timetableDto, stuId);
return ApiResponse.onSuccess();
public ApiResponse<UpdateTimetableResponseDto.updateResponseDtoList> updateTimetable(@RequestBody TimetableRequestDto.TimetableDto timetableDto, @SessionAttribute(name="student") Long stuId) {
UpdateTimetableResponseDto.updateResponseDtoList updateResponseDtoList = timetableService.updateTimetable(timetableDto, stuId);
return ApiResponse.onSuccess(updateResponseDtoList);
}


@GetMapping(value = "/plans/timetable")
public ApiResponse<List<ViewTimetableResponseDto>> viewTimetable(@SessionAttribute(name="student") Long stuId, Integer grade, Integer semester) {
List<ViewTimetableResponseDto> viewTimetableResponseDtos = timetableService.viewTimetable(stuId, grade, semester);
return ApiResponse.onSuccess(viewTimetableResponseDtos);
}

@DeleteMapping(value = "/plans/timetable")
public ApiResponse<List<ViewTimetableResponseDto>> deleteTimetable(@SessionAttribute(name="student") Long stuId, Integer grade, Integer semester, Long subjectId) {
List<ViewTimetableResponseDto> viewTimetableResponseDtos = timetableService.deleteTimetable(stuId, grade, semester, subjectId);
return ApiResponse.onSuccess(viewTimetableResponseDtos);
}
}
20 changes: 18 additions & 2 deletions src/main/java/umc/forgrad/converter/TimetableConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import umc.forgrad.domain.Subject;
import umc.forgrad.dto.Timetable.TimetableRequestDto;
import umc.forgrad.dto.Timetable.AddTimetableResponseDto;
import umc.forgrad.dto.Timetable.UpdateTimetableResponseDto;
import umc.forgrad.dto.Timetable.ViewTimetableResponseDto;

import java.time.LocalDateTime;
Expand All @@ -18,7 +19,7 @@ public static AddTimetableResponseDto.addResponseDtoList toAddResultDto(List<Sub
for(Subject subject : subjects) {
AddTimetableResponseDto.addResponseDto addResponseDto = AddTimetableResponseDto.addResponseDto.builder()
.subjectId(subject.getId())
.createdAt(LocalDateTime.now())
.createdAt(subject.getCreatedAt())
.build();
addResponseDtos.add(addResponseDto);
}
Expand All @@ -33,11 +34,12 @@ public static Semester toSemester(TimetableRequestDto.SemesterDto semesterDto, S
.student(student)
.build();
}
public static Subject toSubject(TimetableRequestDto.SubjectDto subjectDto) {
public static Subject toSubject(TimetableRequestDto.SubjectDto subjectDto, Semester semester) {
return Subject.builder()
.type(subjectDto.getType())
.name(subjectDto.getName())
.credit(subjectDto.getCredit())
.semester(semester)
.build();
}
public static List<ViewTimetableResponseDto> toViewResultDto(List<Subject> subjects) {
Expand All @@ -50,4 +52,18 @@ public static List<ViewTimetableResponseDto> toViewResultDto(List<Subject> subje
.build())
.collect(Collectors.toList());
}

public static UpdateTimetableResponseDto.updateResponseDtoList toUpdateResultDto(List<Subject> subjects) {
List<UpdateTimetableResponseDto.updateResponseDto> updateResponseDtos = new ArrayList<>();
for(Subject subject : subjects) {
UpdateTimetableResponseDto.updateResponseDto updateResponseDto = UpdateTimetableResponseDto.updateResponseDto.builder()
.subjectId(subject.getId())
.updatedAt(subject.getUpdatedAt())
.build();
updateResponseDtos.add(updateResponseDto);
}
return UpdateTimetableResponseDto.updateResponseDtoList.builder()
.updateResponseDtos(updateResponseDtos)
.build();
}
}
7 changes: 3 additions & 4 deletions src/main/java/umc/forgrad/domain/Semester.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import lombok.*;
import umc.forgrad.domain.Student;
import umc.forgrad.domain.common.BaseEntity;
import umc.forgrad.domain.mapping.SemesterSubject;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -25,11 +24,11 @@ public class Semester extends BaseEntity {
@Column(nullable = false)
private Integer semester;

@OneToMany(mappedBy = "semester", cascade = CascadeType.ALL)
private List<Subject> subjectList = new ArrayList<>();

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "student_id")
private Student student; //Student 엔티티와 연결

@OneToMany(mappedBy = "semester")
private List<SemesterSubject> semesterSubjectList = new ArrayList<>();

}
9 changes: 3 additions & 6 deletions src/main/java/umc/forgrad/domain/Subject.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
import jakarta.persistence.*;
import lombok.*;
import umc.forgrad.domain.common.BaseEntity;
import umc.forgrad.domain.mapping.SemesterSubject;

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

@Entity
@Getter
Expand All @@ -27,8 +23,9 @@ public class Subject extends BaseEntity {
@Column(nullable = false)
private Integer credit;

@OneToMany(mappedBy = "subject", cascade = CascadeType.ALL)
private List<SemesterSubject> semesterSubjectList = new ArrayList<>();
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "semester_id")
private Semester semester;

public void update(String type, String name, Integer credit) {
this.type = type;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/umc/forgrad/domain/common/BaseEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;
Expand All @@ -17,7 +18,7 @@ public class BaseEntity {
@CreatedDate
private LocalDateTime createdAt;

@LastModifiedBy
@LastModifiedDate
private LocalDateTime updatedAt;

}
Empty file.
27 changes: 0 additions & 27 deletions src/main/java/umc/forgrad/domain/mapping/SemesterSubject.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public static class SemesterDto {
@Getter
@Builder
public static class SubjectDto {
private Long subjuctId;
private String type;
private String name;
private Integer credit;
Expand Down Expand Up @@ -49,6 +50,6 @@ public static class SearchSubjectDto {
private String searchGrade;
private String searchType;
private String searchName;
private String searchCredit;
private Integer searchCredit;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
public interface SemesterRepository extends JpaRepository<Semester, Long> {

Optional<Semester> findByStudentAndGradeAndSemester(Student student, Integer grade, Integer semester);
List<Semester> findAllByStudentAndGradeAndSemester(Student student, Integer grade, Integer semester);

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import org.springframework.stereotype.Repository;
import umc.forgrad.domain.Semester;
import umc.forgrad.domain.Subject;
import umc.forgrad.domain.mapping.SemesterSubject;

import java.util.List;
import java.util.Optional;

@Repository
public interface SubjectRepository extends JpaRepository<Subject, Long> {
Optional<Subject> findByIdAndSemester_id(Long id, Long Semester_id);
}
77 changes: 43 additions & 34 deletions src/main/java/umc/forgrad/service/TimetableService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
import umc.forgrad.domain.Semester;
import umc.forgrad.domain.Student;
import umc.forgrad.domain.Subject;
import umc.forgrad.domain.mapping.SemesterSubject;
import umc.forgrad.dto.Timetable.*;
import umc.forgrad.repository.SemesterRepository;
import umc.forgrad.repository.SemesterSubjectRepository;
import umc.forgrad.repository.StudentRepository;
import umc.forgrad.repository.SubjectRepository;

Expand All @@ -30,7 +28,6 @@ public class TimetableService {
private final SemesterRepository semesterRepository;
private final SubjectRepository subjectRepository;
private final StudentRepository studentRepository;
private final SemesterSubjectRepository semesterSubjectRepository;

@Transactional
public List<TimetableRequestDto.HakkiDto> searchHakki(HttpSession session) throws IOException {
Expand Down Expand Up @@ -58,6 +55,7 @@ public List<TimetableRequestDto.HakkiDto> searchHakki(HttpSession session) throw
}
return hakkiDtos;
}

@Transactional
public List<TimetableRequestDto.TrackDto> searchTrack(HttpSession session, String hakki) throws IOException {
String trackSearchUrl = "https://info.hansung.ac.kr/jsp_21/student/kyomu/kyoyukgwajung_data_aui.jsp";
Expand Down Expand Up @@ -85,6 +83,7 @@ public List<TimetableRequestDto.TrackDto> searchTrack(HttpSession session, Strin
return trackDtos;
}

@Transactional
public List<TimetableRequestDto.SearchSubjectDto> searchSubject(HttpSession session, String hakki, String track) throws IOException {
String subjectSearchUrl = "https://info.hansung.ac.kr/jsp_21/student/kyomu/kyoyukgwajung_data_aui.jsp";

Expand All @@ -103,67 +102,77 @@ public List<TimetableRequestDto.SearchSubjectDto> searchSubject(HttpSession sess
final String grade = row.select("haknean").text();
final String type = row.select("isugubun").text();
final String name = row.select("kwamokname").text();
final String credit = row.select("hakjum").text();
final Integer credit = Integer.parseInt(row.select("hakjum").text());
TimetableRequestDto.SearchSubjectDto searchSubjectDto = TimetableRequestDto.SearchSubjectDto.builder()
.searchGrade(grade)
.searchType(type)
.searchName(name)
.searchCredit(credit)
.build();
.searchGrade(grade)
.searchType(type)
.searchName(name)
.searchCredit(credit)
.build();
searchSubjectDtos.add(searchSubjectDto);
}
return searchSubjectDtos;
}

@Transactional
public AddTimetableResponseDto.addResponseDtoList addTimetable(TimetableRequestDto.TimetableDto timetableDto, Long stuId) {
Student student = studentRepository.findById(stuId)
.orElseThrow(() -> new IllegalArgumentException("해당 학번이 존재하지 않습니다. id=" + stuId));

Semester semester = semesterRepository.save(TimetableConverter.toSemester(timetableDto.getSemesterDto(), student));
Semester semester = TimetableConverter.toSemester(timetableDto.getSemesterDto(), student);
Optional<Semester> existingSemester = semesterRepository.findByStudentAndGradeAndSemester(student, timetableDto.getSemesterDto().getGrade(), timetableDto.getSemesterDto().getSemester());
if (existingSemester.isPresent()) {
semester = existingSemester.get();
} else {
semester = semesterRepository.save(semester);
}

List<TimetableRequestDto.SubjectDto> subjectDtoList = timetableDto.getSubjectDtoList();
List<Subject> subjects = new ArrayList<>();
List<SemesterSubject> semesterSubjects = new ArrayList<>();
for(TimetableRequestDto.SubjectDto dto : subjectDtoList) {
Subject subject = TimetableConverter.toSubject(dto);

for (TimetableRequestDto.SubjectDto dto : subjectDtoList) {
Subject subject = TimetableConverter.toSubject(dto, semester);
subjects.add(subject);
SemesterSubject semesterSubject = SemesterSubject.builder()
.semester(semester)
.subject(subject)
.build();
semesterSubjects.add(semesterSubject);
}
subjectRepository.saveAll(subjects);
semesterSubjectRepository.saveAll(semesterSubjects);
return TimetableConverter.toAddResultDto(subjects);
}
@Transactional
public UpdateTimetableResponseDto updateTimetable(TimetableRequestDto.TimetableDto timetableDto, Long stuId) {
public UpdateTimetableResponseDto.updateResponseDtoList updateTimetable(TimetableRequestDto.TimetableDto timetableDto, Long stuId) {
Student student = studentRepository.findById(stuId)
.orElseThrow(() -> new IllegalArgumentException("해당 학번이 존재하지 않습니다. id=" + stuId));
Semester semester = TimetableConverter.toSemester(timetableDto.getSemesterDto(), student);

Optional<Semester> optionalSemester = semesterRepository.findByStudentAndGradeAndSemester(student, timetableDto.getSemesterDto().getGrade(), timetableDto.getSemesterDto().getSemester());
Semester semester = optionalSemester.get();
List<TimetableRequestDto.SubjectDto> subjectDtoList = timetableDto.getSubjectDtoList();
List<Subject> subjects = new ArrayList<>();
for(TimetableRequestDto.SubjectDto dto: subjectDtoList) {
Subject subject = subjectRepository.findByIdAndSemester(,semester);

Subject subject = subjectRepository.findByIdAndSemester_id(dto.getSubjuctId(), semester.getId())
.orElseThrow(() -> new IllegalArgumentException("해당 과목이 존재하지 않습니다."));
subject.update(dto.getType(), dto.getName(), dto.getCredit());
subjects.add(subject);
}
return TimetableConverter.toUpdateResultDto(subjects);
}

@Transactional(readOnly = true)
public List<ViewTimetableResponseDto> viewTimetable(Long stuId, Integer grade, Integer semester) {
Student student = studentRepository.findById(stuId)
.orElseThrow(() -> new IllegalArgumentException("해당 학번이 존재하지 않습니다. id=" + stuId));
List<Semester> semesters = semesterRepository.findAllByStudentAndGradeAndSemester(student, grade, semester);
List<SemesterSubject> semesterSubjects = new ArrayList<>();
for (Semester semesterE : semesters) {
List<SemesterSubject> semesterSubject = semesterSubjectRepository.findBySemester(semesterE);
semesterSubjects.addAll(semesterSubject);
}
List<Subject> subjects = semesterSubjects.stream()
.map(SemesterSubject::getSubject)
.toList();

Semester semesterE = semesterRepository.findByStudentAndGradeAndSemester(student, grade, semester)
.orElseThrow(() -> new IllegalArgumentException("해당 학년 학기가 존재하지 않습니다."));
List<Subject> subjects = semesterE.getSubjectList();
return TimetableConverter.toViewResultDto(subjects);
}

@Transactional
public List<ViewTimetableResponseDto> deleteTimetable(Long stuId, Integer grade, Integer semester, Long subjectId) {
Student student = studentRepository.findById(stuId)
.orElseThrow(() -> new IllegalArgumentException("해당 학번이 존재하지 않습니다. id=" + stuId));
Optional<Semester> optionalSemester = semesterRepository.findByStudentAndGradeAndSemester(student, grade, semester);
Semester semesterE = optionalSemester.get();
Subject subject = subjectRepository.findByIdAndSemester_id(subjectId, semesterE.getId())
.orElseThrow(() -> new IllegalArgumentException("해당 과목이 존재하지 않습니다."));
subjectRepository.delete(subject);
return viewTimetable(stuId, grade, semester);
}
}
Loading

0 comments on commit 4eda32d

Please sign in to comment.