Skip to content

Commit

Permalink
feat: 스터디 상세 정보 비즈니스 로직에서 스터디 비즈니스 로직으로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmondBreez3 committed Aug 18, 2024
1 parent 5badce8 commit a444b60
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 121 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package com.gdschongik.gdsc.domain.study.api;

import com.gdschongik.gdsc.domain.study.application.MentorStudyService;
import com.gdschongik.gdsc.domain.study.dto.request.StudyDetailUpdateRequest;
import com.gdschongik.gdsc.domain.study.dto.response.MentorStudyResponse;
import com.gdschongik.gdsc.domain.study.dto.response.StudyStudentResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@Tag(name = "Mentor Study", description = "멘토 스터디 API입니다.")
@RestController
Expand All @@ -21,6 +19,14 @@ public class MentorStudyController {

private final MentorStudyService mentorStudyService;

@Operation(summary = "스터디 상세 정보 작성", description = "스터디 상세 정보를 작성합니다.")
@PatchMapping("/{studyId}")
public ResponseEntity<Void> updateStudyDetail(
@PathVariable Long studyId, @RequestBody StudyDetailUpdateRequest request) {
mentorStudyService.updateStudyDetail(studyId, request);
return ResponseEntity.ok().build();
}

@Operation(summary = "내 스터디 조회", description = "내가 멘토로 있는 스터디를 조회합니다.")
@GetMapping("/me")
public ResponseEntity<List<MentorStudyResponse>> getStudiesInCharge() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.gdschongik.gdsc.domain.study.application.MentorStudyDetailService;
import com.gdschongik.gdsc.domain.study.dto.request.AssignmentCreateUpdateRequest;
import com.gdschongik.gdsc.domain.study.dto.request.StudyDetailUpdateRequest;
import com.gdschongik.gdsc.domain.study.dto.response.AssignmentResponse;
import com.gdschongik.gdsc.domain.study.dto.response.StudySessionResponse;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -28,14 +27,6 @@ public class MentorStudyDetailController {

private final MentorStudyDetailService mentorStudyDetailService;

@Operation(summary = "스터디 상세 정보 작성", description = "스터디 상세 정보를 작성합니다.")
@PatchMapping
public ResponseEntity<Void> updateStudyDetail(
@RequestParam(name = "studyId") Long studyId, @RequestBody StudyDetailUpdateRequest request) {
mentorStudyDetailService.updateStudyDetail(studyId, request);
return ResponseEntity.ok().build();
}

@Operation(summary = "스터디 과제 수정", description = "멘토만 과제를 수정할 수 있습니다.")
@PatchMapping("/{studyDetailId}/assignments")
public ResponseEntity<Void> updateStudyAssignment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,14 @@

import com.gdschongik.gdsc.domain.member.domain.Member;
import com.gdschongik.gdsc.domain.study.dao.StudyDetailRepository;
import com.gdschongik.gdsc.domain.study.dao.StudyRepository;
import com.gdschongik.gdsc.domain.study.domain.Study;
import com.gdschongik.gdsc.domain.study.domain.StudyDetail;
import com.gdschongik.gdsc.domain.study.domain.StudyDetailValidator;
import com.gdschongik.gdsc.domain.study.domain.StudyValidator;
import com.gdschongik.gdsc.domain.study.dto.request.AssignmentCreateUpdateRequest;
import com.gdschongik.gdsc.domain.study.dto.request.StudyDetailUpdateRequest;
import com.gdschongik.gdsc.domain.study.dto.request.StudySessionCreateRequest;
import com.gdschongik.gdsc.domain.study.dto.response.AssignmentResponse;
import com.gdschongik.gdsc.domain.study.dto.response.StudySessionResponse;
import com.gdschongik.gdsc.global.exception.CustomException;
import com.gdschongik.gdsc.global.util.MemberUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -34,8 +25,6 @@ public class MentorStudyDetailService {
private final MemberUtil memberUtil;
private final StudyDetailRepository studyDetailRepository;
private final StudyDetailValidator studyDetailValidator;
private final StudyRepository studyRepository;
private final StudyValidator studyValidator;

@Transactional(readOnly = true)
public List<AssignmentResponse> getWeeklyAssignments(Long studyId) {
Expand Down Expand Up @@ -101,39 +90,4 @@ public List<StudySessionResponse> getSessions(Long studyId) {
List<StudyDetail> studyDetails = studyDetailRepository.findAllByStudyId(studyId);
return studyDetails.stream().map(StudySessionResponse::from).toList();
}

// TODO session -> curriculum 변경
@Transactional
public void updateStudyDetail(Long studyId, StudyDetailUpdateRequest request) {
Member currentMember = memberUtil.getCurrentMember();
Study study = studyRepository.findById(studyId).orElseThrow(() -> new CustomException(STUDY_NOT_FOUND));
studyValidator.validateStudyMentor(currentMember, study);

List<StudyDetail> studyDetails = studyDetailRepository.findAllByStudyId(studyId);
studyDetailValidator.validateUpdateStudyDetail(studyDetails, request.studySessions());

study.update(request.notionLink(), request.introduction());
studyRepository.save(study);
log.info("[MentorStudyDetailService] 스터디 기본 정보 수정 완료: studyId={}", studyId);

Map<Long, StudySessionCreateRequest> requestMap = request.studySessions().stream()
.collect(Collectors.toMap(StudySessionCreateRequest::studyDetailId, Function.identity()));

List<StudyDetail> updatedStudyDetails = new ArrayList<>();
for (StudyDetail studyDetail : studyDetails) {
Long id = studyDetail.getId();
StudySessionCreateRequest matchingSession = requestMap.get(id);

studyDetail.updateSession(
studyDetail.getStudy().getPeriod().getStartDate(),
matchingSession.title(),
matchingSession.description(),
matchingSession.difficulty(),
matchingSession.status());

updatedStudyDetails.add(studyDetail);
}
studyDetailRepository.saveAll(updatedStudyDetails);
log.info("[MentorStudyDetailService] 스터디 상세정보 커리큘럼 작성 완료: studyDetailId={}", studyDetails);
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
package com.gdschongik.gdsc.domain.study.application;

import static com.gdschongik.gdsc.global.exception.ErrorCode.STUDY_NOT_FOUND;

import com.gdschongik.gdsc.domain.member.domain.Member;
import com.gdschongik.gdsc.domain.study.dao.StudyDetailRepository;
import com.gdschongik.gdsc.domain.study.dao.StudyHistoryRepository;
import com.gdschongik.gdsc.domain.study.dao.StudyRepository;
import com.gdschongik.gdsc.domain.study.domain.Study;
import com.gdschongik.gdsc.domain.study.domain.StudyHistory;
import com.gdschongik.gdsc.domain.study.domain.StudyValidator;
import com.gdschongik.gdsc.domain.study.domain.*;
import com.gdschongik.gdsc.domain.study.dto.request.StudyDetailUpdateRequest;
import com.gdschongik.gdsc.domain.study.dto.request.StudySessionCreateRequest;
import com.gdschongik.gdsc.domain.study.dto.response.MentorStudyResponse;
import com.gdschongik.gdsc.domain.study.dto.response.StudyStudentResponse;
import com.gdschongik.gdsc.global.exception.CustomException;
import com.gdschongik.gdsc.global.exception.ErrorCode;
import com.gdschongik.gdsc.global.util.MemberUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Slf4j
public class MentorStudyService {

private final MemberUtil memberUtil;
private final StudyRepository studyRepository;
private final StudyHistoryRepository studyHistoryRepository;
private final StudyValidator studyValidator;

private final StudyDetailRepository studyDetailRepository;
private final StudyDetailValidator studyDetailValidator;

@Transactional(readOnly = true)
public List<MentorStudyResponse> getStudiesInCharge() {
Member currentMember = memberUtil.getCurrentMember();
Expand All @@ -43,4 +55,39 @@ public List<StudyStudentResponse> getStudyStudents(Long studyId) {

return studyHistories.stream().map(StudyStudentResponse::from).toList();
}

// TODO session -> curriculum 변경
@Transactional
public void updateStudyDetail(Long studyId, StudyDetailUpdateRequest request) {
Member currentMember = memberUtil.getCurrentMember();
Study study = studyRepository.findById(studyId).orElseThrow(() -> new CustomException(STUDY_NOT_FOUND));
studyValidator.validateStudyMentor(currentMember, study);

List<StudyDetail> studyDetails = studyDetailRepository.findAllByStudyId(studyId);
studyDetailValidator.validateUpdateStudyDetail(studyDetails, request.studySessions());

study.update(request.notionLink(), request.introduction());
studyRepository.save(study);
log.info("[MentorStudyDetailService] 스터디 기본 정보 수정 완료: studyId={}", studyId);

Map<Long, StudySessionCreateRequest> requestMap = request.studySessions().stream()
.collect(Collectors.toMap(StudySessionCreateRequest::studyDetailId, Function.identity()));

List<StudyDetail> updatedStudyDetails = new ArrayList<>();
for (StudyDetail studyDetail : studyDetails) {
Long id = studyDetail.getId();
StudySessionCreateRequest matchingSession = requestMap.get(id);

studyDetail.updateSession(
studyDetail.getStudy().getPeriod().getStartDate(),
matchingSession.title(),
matchingSession.description(),
matchingSession.difficulty(),
matchingSession.status());

updatedStudyDetails.add(studyDetail);
}
studyDetailRepository.saveAll(updatedStudyDetails);
log.info("[MentorStudyDetailService] 스터디 상세정보 커리큘럼 작성 완료: studyDetailId={}", studyDetails);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package com.gdschongik.gdsc.domain.study.application;

import static com.gdschongik.gdsc.global.common.constant.StudyConstant.*;
import static org.assertj.core.api.Assertions.*;

import com.gdschongik.gdsc.domain.member.domain.Member;
import com.gdschongik.gdsc.domain.member.domain.MemberRole;
import com.gdschongik.gdsc.domain.recruitment.domain.vo.Period;
import com.gdschongik.gdsc.domain.study.dao.StudyDetailRepository;
import com.gdschongik.gdsc.domain.study.domain.Difficulty;
import com.gdschongik.gdsc.domain.study.domain.Study;
import com.gdschongik.gdsc.domain.study.domain.StudyDetail;
import com.gdschongik.gdsc.domain.study.domain.StudyStatus;
import com.gdschongik.gdsc.domain.study.dto.request.StudyDetailUpdateRequest;
import com.gdschongik.gdsc.domain.study.dto.request.StudySessionCreateRequest;
import com.gdschongik.gdsc.helper.IntegrationTest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -53,57 +47,4 @@ class 스터디_과제_휴강_처리시 {
assertThat(cancelledStudyDetail.getAssignment().getStatus()).isEqualTo(StudyStatus.CANCELLED);
}
}

@Nested
class 스터디_상세정보_작성시 {

@Test
void 성공한다() {
// given
LocalDateTime now = LocalDateTime.now();
Member mentor = createMentor();
Study study = createNewStudy(
mentor,
4L,
Period.createPeriod(now.plusDays(5), now.plusDays(10)),
Period.createPeriod(now.minusDays(5), now));
for (int i = 1; i <= 4; i++) {
Long week = (long) i;
createNewStudyDetail(week, study, now, now.plusDays(7));
now = now.plusDays(8);
}
logoutAndReloginAs(study.getMentor().getId(), MemberRole.ASSOCIATE);

List<StudySessionCreateRequest> sessionCreateRequests = new ArrayList<>();
for (int i = 1; i <= study.getTotalWeek(); i++) {
Long id = (long) i;
StudySessionCreateRequest sessionCreateRequest = new StudySessionCreateRequest(
id, SESSION_TITLE + i, SESSION_DESCRIPTION + i, Difficulty.HIGH, StudyStatus.OPEN);
sessionCreateRequests.add(sessionCreateRequest);
}

StudyDetailUpdateRequest request =
new StudyDetailUpdateRequest(STUDY_NOTION_LINK, STUDY_INTRODUCTION, sessionCreateRequests);

// when
mentorStudyDetailService.updateStudyDetail(1L, request);

// then
Study savedStudy = studyRepository.findById(study.getId()).get();
assertThat(savedStudy.getNotionLink()).isEqualTo(request.notionLink());
assertThat(savedStudy.getIntroduction()).isEqualTo(request.introduction());

List<StudyDetail> studyDetails = studyDetailRepository.findAllByStudyId(1L);
for (int i = 0; i < studyDetails.size(); i++) {
StudyDetail studyDetail = studyDetails.get(i);
Long expectedId = studyDetail.getId();

assertThat(studyDetail.getId()).isEqualTo(expectedId);
assertThat(studyDetail.getSession().getTitle()).isEqualTo(SESSION_TITLE + expectedId);
assertThat(studyDetail.getSession().getDescription()).isEqualTo(SESSION_DESCRIPTION + expectedId);
assertThat(studyDetail.getSession().getDifficulty()).isEqualTo(Difficulty.HIGH);
assertThat(studyDetail.getSession().getStatus()).isEqualTo(StudyStatus.OPEN);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.gdschongik.gdsc.domain.study.application;

import static com.gdschongik.gdsc.global.common.constant.StudyConstant.*;
import static com.gdschongik.gdsc.global.common.constant.StudyConstant.SESSION_DESCRIPTION;
import static org.assertj.core.api.Assertions.assertThat;

import com.gdschongik.gdsc.domain.member.domain.Member;
import com.gdschongik.gdsc.domain.member.domain.MemberRole;
import com.gdschongik.gdsc.domain.recruitment.domain.vo.Period;
import com.gdschongik.gdsc.domain.study.domain.Difficulty;
import com.gdschongik.gdsc.domain.study.domain.Study;
import com.gdschongik.gdsc.domain.study.domain.StudyDetail;
import com.gdschongik.gdsc.domain.study.domain.StudyStatus;
import com.gdschongik.gdsc.domain.study.dto.request.StudyDetailUpdateRequest;
import com.gdschongik.gdsc.domain.study.dto.request.StudySessionCreateRequest;
import com.gdschongik.gdsc.helper.IntegrationTest;
import java.time.LocalDateTime;
import java.util.*;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;

public class MentorStudyServiceTest extends IntegrationTest {

@Autowired
private MentorStudyService mentorStudyService;

@Nested
class 스터디_상세정보_작성시 {

@Test
void 성공한다() {
// given
LocalDateTime now = LocalDateTime.now();
Member mentor = createMentor();
Study study = createNewStudy(
mentor,
4L,
Period.createPeriod(now.plusDays(5), now.plusDays(10)),
Period.createPeriod(now.minusDays(5), now));
for (int i = 1; i <= 4; i++) {
Long week = (long) i;
createNewStudyDetail(week, study, now, now.plusDays(7));
now = now.plusDays(8);
}
logoutAndReloginAs(study.getMentor().getId(), MemberRole.ASSOCIATE);

List<StudySessionCreateRequest> sessionCreateRequests = new ArrayList<>();
for (int i = 1; i <= study.getTotalWeek(); i++) {
Long id = (long) i;
StudySessionCreateRequest sessionCreateRequest = new StudySessionCreateRequest(
id, SESSION_TITLE + i, SESSION_DESCRIPTION + i, Difficulty.HIGH, StudyStatus.OPEN);
sessionCreateRequests.add(sessionCreateRequest);
}

StudyDetailUpdateRequest request =
new StudyDetailUpdateRequest(STUDY_NOTION_LINK, STUDY_INTRODUCTION, sessionCreateRequests);

// when
mentorStudyService.updateStudyDetail(1L, request);

// then
Study savedStudy = studyRepository.findById(study.getId()).get();
assertThat(savedStudy.getNotionLink()).isEqualTo(request.notionLink());
assertThat(savedStudy.getIntroduction()).isEqualTo(request.introduction());

List<StudyDetail> studyDetails = studyDetailRepository.findAllByStudyId(1L);
for (int i = 0; i < studyDetails.size(); i++) {
StudyDetail studyDetail = studyDetails.get(i);
Long expectedId = studyDetail.getId();

assertThat(studyDetail.getId()).isEqualTo(expectedId);
assertThat(studyDetail.getSession().getTitle()).isEqualTo(SESSION_TITLE + expectedId);
assertThat(studyDetail.getSession().getDescription()).isEqualTo(SESSION_DESCRIPTION + expectedId);
assertThat(studyDetail.getSession().getDifficulty()).isEqualTo(Difficulty.HIGH);
assertThat(studyDetail.getSession().getStatus()).isEqualTo(StudyStatus.OPEN);
}
}
}
}

0 comments on commit a444b60

Please sign in to comment.