Skip to content

Commit

Permalink
feat: 스터디 수정 기능 추가
Browse files Browse the repository at this point in the history
스터디 수정 기능을 service와 controller에 추가
  • Loading branch information
ybkang1108 committed Jan 17, 2025
1 parent e99cab5 commit 52858dc
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 33 deletions.
18 changes: 15 additions & 3 deletions src/main/java/com/gdgoc/study_group/study/api/StudyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,35 @@ public ResponseEntity<?> getStudyDetail(@PathVariable("studyId") Long studyId) {

if (studyDetail == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(MessageResponse.builder().message("해당하는 스터디가 없습니다.").build());
.body(MessageResponse.builder().message("해당하는 스터디가 없습니다.").build());
}

return ResponseEntity.status(HttpStatus.OK).body(studyDetail);
}

@PatchMapping("/{studyId}")
public ResponseEntity<?> updateStudy(
@PathVariable("studyId") Long studyId, @RequestBody StudyCreateRequest updateRequest) {
Long updatedStudyId = studyService.updateStudy(studyId, updateRequest);
if (updatedStudyId == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(MessageResponse.builder().message("해당하는 스터디가 없습니다").build());
}
return ResponseEntity.status(HttpStatus.OK)
.body(StudyCreateResponse.builder().message("스터디가 수정되었습니다.").id(updatedStudyId).build());
}

@DeleteMapping("/{studyId}")
public ResponseEntity<MessageResponse> deleteStudy(@PathVariable("studyId") Long studyId) {

boolean isStudyExist = studyService.deleteStudy(studyId);

if (isStudyExist) {
return ResponseEntity.status(HttpStatus.NO_CONTENT)
.body(MessageResponse.builder().message("스터디가 삭제되었습니다.").build());
.body(MessageResponse.builder().message("스터디가 삭제되었습니다.").build());
}

return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(MessageResponse.builder().message("해당하는 스터디가 없습니다.").build());
.body(MessageResponse.builder().message("해당하는 스터디가 없습니다.").build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import com.gdgoc.study_group.study.dao.StudyRepository;
import com.gdgoc.study_group.study.domain.Study;
import com.gdgoc.study_group.study.dto.*;
import com.gdgoc.study_group.studyMember.domain.StudyMember;
import com.gdgoc.study_group.studyMember.domain.StudyMemberStatus;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand All @@ -27,32 +25,25 @@ public StudyService(StudyRepository studyRepository, MemberRepository memberRepo

private Curriculum createCurriculum(CurriculumDTO curriculumDTO, Study study) {
return Curriculum.builder()
.study(study)
.week(curriculumDTO.getWeek())
.subject(curriculumDTO.getSubject())
.build();
.study(study)
.week(curriculumDTO.getWeek())
.subject(curriculumDTO.getSubject())
.build();
}

private CurriculumDTO curriculumEntityToDTO(Curriculum curriculum) {
return CurriculumDTO.builder()
.week(curriculum.getWeek())
.subject(curriculum.getSubject())
.build();
.week(curriculum.getWeek())
.subject(curriculum.getSubject())
.build();
}

private DayDTO dayEntityToDTO(Day day) {
return DayDTO.builder()
.day(day.getDay())
.startTime(day.getStartTime())
.build();
return DayDTO.builder().day(day.getDay()).startTime(day.getStartTime()).build();
}

private Day createDay(DayDTO dayDTO, Study study) {
return Day.builder()
.study(study)
.day(dayDTO.getDay())
.startTime(dayDTO.getStartTime())
.build();
return Day.builder().study(study).day(dayDTO.getDay()).startTime(dayDTO.getStartTime()).build();
}

/**
Expand Down Expand Up @@ -129,32 +120,34 @@ public List<StudyListResponse> getStudyList() {
* @return 스터디 정보 반환
*/
public StudyDetailResponse getStudyDetail(Long studyId) {
Optional<Study> study = studyRepository.findById(studyId);
Optional<Study> existingStudy = studyRepository.findById(studyId);

if (existingStudy.isPresent()) { // 해당 아이디를 가진 스터디가 존재할 때
Study study = existingStudy.get();

if (study.isPresent()) { // 해당 아이디를 가진 스터디가 존재할 때
StudyDetailResponse detailResponse =
StudyDetailResponse.builder()
.id(studyId)
.name(study.get().getName())
.description(study.get().getDescription())
.requirement(study.get().getRequirement())
.question(study.get().getQuestion())
.name(study.getName())
.description(study.getDescription())
.requirement(study.getRequirement())
.question(study.getQuestion())
.curriculums(new ArrayList<>())
.days(new ArrayList<>())
.maxParticipants(study.get().getMaxParticipants())
.isApplicationClosed(study.get().getIsApplicationClosed())
.maxParticipants(study.getMaxParticipants())
.isApplicationClosed(study.getIsApplicationClosed())
.build();

// curriculum이 있다면 curriculumDTO로 변환해 response에 추가
if (study.get().getCurriculums() != null) {
for (Curriculum curriculum : study.get().getCurriculums()) {
if (study.getCurriculums() != null) {
for (Curriculum curriculum : study.getCurriculums()) {
detailResponse.getCurriculums().add(curriculumEntityToDTO(curriculum));
}
}

// day가 있다면 dayDTO로 변환해 response에 추가
if (study.get().getDays() != null) {
for (Day day : study.get().getDays()) {
if (study.getDays() != null) {
for (Day day : study.getDays()) {
detailResponse.getDays().add(dayEntityToDTO(day));
}
}
Expand All @@ -164,6 +157,44 @@ public StudyDetailResponse getStudyDetail(Long studyId) {
return null;
}

public Long updateStudy(Long studyId, StudyCreateRequest updateRequest) {
Optional<Study> study = studyRepository.findById(studyId);

if (study.isEmpty()) {}

Study existingStudy = studyRepository.findById(studyId).get();
Study updatedStudy =
Study.builder()
.id(existingStudy.getId())
.name(existingStudy.getName())
.description(existingStudy.getDescription())
.requirement(existingStudy.getRequirement())
.question(existingStudy.getQuestion())
.curriculums(new ArrayList<>())
.days(new ArrayList<>())
.maxParticipants(existingStudy.getMaxParticipants())
.isApplicationClosed(existingStudy.getIsApplicationClosed())
.build();

// 등록된 커리큘럼이 있다면 엔티티로 변환하여 스터디에 추가
if (updateRequest.getCurriculums() != null) {
for (CurriculumDTO curriculumDTO : updateRequest.getCurriculums()) {
updatedStudy.getCurriculums().add(createCurriculum(curriculumDTO, updatedStudy));
}
}

// 등록된 스터디 날짜가 있다면 엔티티로 변환하여 스터디에 추가
if (updatedStudy.getDays() != null) {
for (DayDTO dayDTO : updateRequest.getDays()) {
updatedStudy.getDays().add(createDay(dayDTO, updatedStudy));
}
}

studyRepository.save(updatedStudy);

return updatedStudy.getId();
}

/**
* 스터디장 권한 확인 필요 스터디를 삭제합니다
*
Expand Down

0 comments on commit 52858dc

Please sign in to comment.