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

[Refactor/81] 공부 분류/방법/장소에 isDefault 추가 #87

Merged
merged 6 commits into from
Aug 20, 2024
Merged
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
55 changes: 31 additions & 24 deletions src/main/java/timify/com/study/StudyConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,58 @@
import timify.com.study.dto.StudyResponse;

public class StudyConverter {

public static StudyType toStudyType(String title, int order) {
return StudyType.builder()
.title(title)
.orderNum(order)
.status(CategoryStatus.ACTIVE)
.build();
.title(title)
.orderNum(order)
.status(CategoryStatus.ACTIVE)
.isDefault(false)
.build();
}

public static StudyResponse.studyTypeDto toStudyTypeDto(StudyType studyType) {
return StudyResponse.studyTypeDto.builder()
.studyTypeId(studyType.getId())
.order(studyType.getOrderNum())
.studyTypeTitle(studyType.getTitle())
.build();
.studyTypeId(studyType.getId())
.order(studyType.getOrderNum())
.studyTypeTitle(studyType.getTitle())
.isDefault(studyType.isDefault())
.build();
}

public static StudyMethod toStudyMethod(String title, int order) {
return StudyMethod.builder()
.title(title)
.orderNum(order)
.status(CategoryStatus.ACTIVE)
.build();
.title(title)
.orderNum(order)
.status(CategoryStatus.ACTIVE)
.isDefault(false)
.build();
}

public static StudyResponse.studyMethodDto toStudyMethodDto(StudyMethod studyMethod) {
return StudyResponse.studyMethodDto.builder()
.studyMethodId(studyMethod.getId())
.order(studyMethod.getOrderNum())
.studyMethodTitle(studyMethod.getTitle())
.build();
.studyMethodId(studyMethod.getId())
.order(studyMethod.getOrderNum())
.studyMethodTitle(studyMethod.getTitle())
.isDefault(studyMethod.isDefault())
.build();
}

public static StudyPlace toStudyPlace(String title, int order) {
return StudyPlace.builder()
.title(title)
.orderNum(order)
.status(CategoryStatus.ACTIVE)
.build();
.title(title)
.orderNum(order)
.status(CategoryStatus.ACTIVE)
.isDefault(false)
.build();
}

public static StudyResponse.studyPlaceDto toStudyPlaceDto(StudyPlace studyPlace) {
return StudyResponse.studyPlaceDto.builder()
.studyPlaceId(studyPlace.getId())
.order(studyPlace.getOrderNum())
.studyPlaceTitle(studyPlace.getTitle())
.build();
.studyPlaceId(studyPlace.getId())
.order(studyPlace.getOrderNum())
.studyPlaceTitle(studyPlace.getTitle())
.isDefault(studyPlace.isDefault())
.build();
}
}
151 changes: 110 additions & 41 deletions src/main/java/timify/com/study/StudyService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package timify.com.study;

import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -15,8 +17,6 @@
import timify.com.study.repository.StudyPlaceRepository;
import timify.com.study.repository.StudyTypeRepository;

import java.util.List;

@Service
@RequiredArgsConstructor
public class StudyService {
Expand All @@ -37,13 +37,15 @@ public StudyType insertStudyType(StudyRequest.studyTypeRequest request, Member m
}

// 이미 존재하는 이름의 StudyType인지 검증
boolean exists = studyTypeRepository.existsByMemberAndTitleAndStatus(member, request.getTitle(), CategoryStatus.ACTIVE);
boolean exists = studyTypeRepository.existsByMemberAndTitleAndStatus(member,
request.getTitle(), CategoryStatus.ACTIVE);
if (exists) {
throw new StudyHandler(ErrorStatus.STUDY_TYPE_ALREADY_EXISTS);
}

// StudyType 엔티티 생성 및 연관관계 매핑
StudyType studyType = StudyConverter.toStudyType(request.getTitle(), member.getStudyTypeList().size() + 1);
StudyType studyType = StudyConverter.toStudyType(request.getTitle(),
member.getStudyTypeList().size() + 1);
studyType.setMember(member);

return studyTypeRepository.save(studyType);
Expand All @@ -55,31 +57,52 @@ public List<StudyType> getStudyTypes(Member member) {
}

@Transactional
public StudyType updateStudyType(StudyRequest.studyTypeRequest request, Long studyTypeId, Member member) {
StudyType studyType = studyTypeRepository.findByIdAndStatus(studyTypeId, CategoryStatus.ACTIVE)
.orElseThrow(() -> new StudyHandler(ErrorStatus.STUDY_TYPE_NOT_FOUND));
public StudyType updateStudyType(StudyRequest.studyTypeRequest request, Long studyTypeId,
Member member) {
StudyType studyType = studyTypeRepository.findByIdAndStatus(studyTypeId,
CategoryStatus.ACTIVE)
.orElseThrow(() -> new StudyHandler(ErrorStatus.STUDY_TYPE_NOT_FOUND));

// 해당 studyType이 member의 것이 맞는지 검증
if (!studyType.getMember().equals(member)) {
throw new StudyHandler(ErrorStatus.NOT_STUDY_TYPE_OWNER);
}

// 활성화된 공부 분류와의 이름 중복 여부 검증
boolean exists = studyTypeRepository.existsByMemberAndTitleAndStatus(member, request.getTitle(), CategoryStatus.ACTIVE);
if (exists) {
throw new StudyHandler(ErrorStatus.STUDY_TYPE_ALREADY_EXISTS);
// title을 수정하는 경우
if (request.getTitle() != null) {
// 활성화된 공부 분류와의 이름 중복 여부 검증
boolean exists = studyTypeRepository.existsByMemberAndTitleAndStatus(member,
request.getTitle(), CategoryStatus.ACTIVE);
if (exists) {
Copy link
Contributor

Choose a reason for hiding this comment

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

추후에 if문 안에 if문 리팩토링하면 좋을 듯 싶습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

넵! 전체적인 기능 구현 완료 후에 리팩토링 해보겠습니다 !!

throw new StudyHandler(ErrorStatus.STUDY_TYPE_ALREADY_EXISTS);
}

// studyType의 이름 수정
studyType.setTitle(request.getTitle());
}

// studyType의 이름 수정
studyType.setTitle(request.getTitle());
// default 여부를 수정하는 경우
if (request.getIsDefault() != null) {
List<StudyType> defaultStudyType = member.getStudyTypeList().stream()
.filter(StudyType::isDefault).collect(Collectors.toList());
if (!defaultStudyType.isEmpty()) { // 기존 default 값이 존재하는 경우

// 기존 default 값 해제
defaultStudyType.get(0).updateIsDefault(false);

// 요청한 studyType을 default true로 변경
studyType.updateIsDefault(true);
}
}

return studyType;
}

@Transactional
public void deleteStudyType(Long studyTypeId, Member member) {
StudyType studyType = studyTypeRepository.findByIdAndStatus(studyTypeId, CategoryStatus.ACTIVE)
.orElseThrow(() -> new StudyHandler(ErrorStatus.STUDY_TYPE_NOT_FOUND));
StudyType studyType = studyTypeRepository.findByIdAndStatus(studyTypeId,
CategoryStatus.ACTIVE)
.orElseThrow(() -> new StudyHandler(ErrorStatus.STUDY_TYPE_NOT_FOUND));

// 해당 studyType이 member의 것이 맞는지 검증
if (!studyType.getMember().equals(member)) {
Expand All @@ -98,13 +121,15 @@ public StudyMethod insertStudyMethod(StudyRequest.studyMethodRequest request, Me
}

// 이미 존재하는 이름의 StudyMethod인지 검증
boolean exists = studyMethodRepository.existsByMemberAndTitleAndStatus(member, request.getTitle(), CategoryStatus.ACTIVE);
boolean exists = studyMethodRepository.existsByMemberAndTitleAndStatus(member,
request.getTitle(), CategoryStatus.ACTIVE);
if (exists) {
throw new StudyHandler(ErrorStatus.STUDY_METHOD_ALREADY_EXISTS);
}

// StudyMethod 엔티티 생성 및 연관관계 매핑
StudyMethod studyMethod = StudyConverter.toStudyMethod(request.getTitle(), member.getStudyMethodList().size() + 1);
StudyMethod studyMethod = StudyConverter.toStudyMethod(request.getTitle(),
member.getStudyMethodList().size() + 1);
studyMethod.setMember(member);

return studyMethodRepository.save(studyMethod);
Expand All @@ -116,31 +141,52 @@ public List<StudyMethod> getStudyMethods(Member member) {
}

@Transactional
public StudyMethod updateStudyMethod(StudyRequest.studyMethodRequest request, Long studyMethodId, Member member) {
StudyMethod studyMethod = studyMethodRepository.findByIdAndStatus(studyMethodId, CategoryStatus.ACTIVE)
.orElseThrow(() -> new StudyHandler(ErrorStatus.STUDY_METHOD_NOT_FOUND));
public StudyMethod updateStudyMethod(StudyRequest.studyMethodRequest request,
Long studyMethodId, Member member) {
StudyMethod studyMethod = studyMethodRepository.findByIdAndStatus(studyMethodId,
CategoryStatus.ACTIVE)
.orElseThrow(() -> new StudyHandler(ErrorStatus.STUDY_METHOD_NOT_FOUND));

// 해당 studyMethod가 member의 것이 맞는지 검증
if (!studyMethod.getMember().equals(member)) {
throw new StudyHandler(ErrorStatus.NOT_STUDY_METHOD_OWNER);
}

// 활성화된 공부 방법과의 이름 중복 여부 검증
boolean exists = studyMethodRepository.existsByMemberAndTitleAndStatus(member, request.getTitle(), CategoryStatus.ACTIVE);
if (exists) {
throw new StudyHandler(ErrorStatus.STUDY_METHOD_ALREADY_EXISTS);
// title을 수정하는 경우
if (request.getTitle() != null) {
// 활성화된 공부 방법과의 이름 중복 여부 검증
boolean exists = studyMethodRepository.existsByMemberAndTitleAndStatus(member,
request.getTitle(), CategoryStatus.ACTIVE);
if (exists) {
throw new StudyHandler(ErrorStatus.STUDY_METHOD_ALREADY_EXISTS);
}

// studyMethod의 이름 수정
studyMethod.setTitle(request.getTitle());
}

// studyMethod의 이름 수정
studyMethod.setTitle(request.getTitle());
// default 여부를 수정하는 경우
if (request.getIsDefault() != null) {
List<StudyMethod> defaultStudyMethod = member.getStudyMethodList().stream()
.filter(StudyMethod::isDefault).collect(Collectors.toList());
if (!defaultStudyMethod.isEmpty()) { // 기존 default 값이 존재하는 경우

// 기존 default 값 해제
defaultStudyMethod.get(0).updateIsDefault(false);

// 요청한 studyMethod를 default true로 변경
studyMethod.updateIsDefault(true);
}
}

return studyMethod;
}

@Transactional
public void deleteStudyMethod(Long studyMethodId, Member member) {
StudyMethod studyMethod = studyMethodRepository.findByIdAndStatus(studyMethodId, CategoryStatus.ACTIVE)
.orElseThrow(() -> new StudyHandler(ErrorStatus.STUDY_TYPE_NOT_FOUND));
StudyMethod studyMethod = studyMethodRepository.findByIdAndStatus(studyMethodId,
CategoryStatus.ACTIVE)
.orElseThrow(() -> new StudyHandler(ErrorStatus.STUDY_TYPE_NOT_FOUND));

// 해당 studyMethod가 member의 것이 맞는지 검증
if (!studyMethod.getMember().equals(member)) {
Expand All @@ -159,13 +205,15 @@ public StudyPlace insertStudyPlace(StudyRequest.studyPlaceRequest request, Membe
}

// 이미 존재하는 이름의 StudyPlace인지 검증
boolean exists = studyPlaceRepository.existsByMemberAndTitleAndStatus(member, request.getTitle(), CategoryStatus.ACTIVE);
boolean exists = studyPlaceRepository.existsByMemberAndTitleAndStatus(member,
request.getTitle(), CategoryStatus.ACTIVE);
if (exists) {
throw new StudyHandler(ErrorStatus.STUDY_PLACE_ALREADY_EXISTS);
}

// StudyPlace 엔티티 생성 및 연관관계 매핑
StudyPlace studyPlace = StudyConverter.toStudyPlace(request.getTitle(), member.getStudyPlaceList().size() + 1);
StudyPlace studyPlace = StudyConverter.toStudyPlace(request.getTitle(),
member.getStudyPlaceList().size() + 1);
studyPlace.setMember(member);

return studyPlaceRepository.save(studyPlace);
Expand All @@ -177,31 +225,52 @@ public List<StudyPlace> getStudyPlaces(Member member) {
}

@Transactional
public StudyPlace updateStudyPlace(StudyRequest.studyPlaceRequest request, Long studyPlaceId, Member member) {
StudyPlace studyPlace = studyPlaceRepository.findByIdAndStatus(studyPlaceId, CategoryStatus.ACTIVE)
.orElseThrow(() -> new StudyHandler(ErrorStatus.STUDY_PLACE_NOT_FOUND));
public StudyPlace updateStudyPlace(StudyRequest.studyPlaceRequest request, Long studyPlaceId,
Member member) {
StudyPlace studyPlace = studyPlaceRepository.findByIdAndStatus(studyPlaceId,
CategoryStatus.ACTIVE)
.orElseThrow(() -> new StudyHandler(ErrorStatus.STUDY_PLACE_NOT_FOUND));

// 해당 studyPlace가 member의 것이 맞는지 검증
if (!studyPlace.getMember().equals(member)) {
throw new StudyHandler(ErrorStatus.NOT_STUDY_PLACE_OWNER);
}

// 활성화된 공부 징소와의 이름 중복 여부 검증
boolean exists = studyPlaceRepository.existsByMemberAndTitleAndStatus(member, request.getTitle(), CategoryStatus.ACTIVE);
if (exists) {
throw new StudyHandler(ErrorStatus.STUDY_PLACE_ALREADY_EXISTS);
// title을 수정하는 경우
if (request.getTitle() != null) {
// 활성화된 공부 징소와의 이름 중복 여부 검증
boolean exists = studyPlaceRepository.existsByMemberAndTitleAndStatus(member,
request.getTitle(), CategoryStatus.ACTIVE);
if (exists) {
throw new StudyHandler(ErrorStatus.STUDY_PLACE_ALREADY_EXISTS);
}

// studyPlace의 이름 수정
studyPlace.setTitle(request.getTitle());
}

// studyPlace의 이름 수정
studyPlace.setTitle(request.getTitle());
// default 여부를 수정하는 경우
if (request.getIsDefault() != null) {
List<StudyPlace> defaultStudyPlace = member.getStudyPlaceList().stream()
.filter(StudyPlace::isDefault).collect(Collectors.toList());
if (!defaultStudyPlace.isEmpty()) { // 기존 default 값이 존재하는 경우

// 기존 default 값 해제
defaultStudyPlace.get(0).updateIsDefault(false);

// 요청한 studyMethod를 default true로 변경
studyPlace.updateIsDefault(true);
}
}

return studyPlace;
}

@Transactional
public void deleteStudyPlace(Long studyPlaceId, Member member) {
StudyPlace studyPlace = studyPlaceRepository.findByIdAndStatus(studyPlaceId, CategoryStatus.ACTIVE)
.orElseThrow(() -> new StudyHandler(ErrorStatus.STUDY_PLACE_NOT_FOUND));
StudyPlace studyPlace = studyPlaceRepository.findByIdAndStatus(studyPlaceId,
CategoryStatus.ACTIVE)
.orElseThrow(() -> new StudyHandler(ErrorStatus.STUDY_PLACE_NOT_FOUND));

// 해당 studyPlace가 member의 것이 맞는지 검증
if (!studyPlace.getMember().equals(member)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -54,7 +55,7 @@ public ApiResponse<List<studyTypeDto>> getStudyType(@AuthMember Member member) {
}

@Override
@PostMapping("/type/{studyTypeId}/update")
@PatchMapping("/type/{studyTypeId}/update")
public ApiResponse<StudyResponse.studyTypeDto> updateStudyType(
@AuthMember Member member,
@RequestBody @Valid StudyRequest.studyTypeRequest request,
Expand Down Expand Up @@ -98,7 +99,7 @@ public ApiResponse<List<StudyResponse.studyMethodDto>> getStudyMethod(
}

@Override
@PostMapping("/method/{studyMethodId}/update")
@PatchMapping("/method/{studyMethodId}/update")
public ApiResponse<StudyResponse.studyMethodDto> updateStudyMethod(
@AuthMember Member member,
@RequestBody @Valid StudyRequest.studyMethodRequest request,
Expand Down Expand Up @@ -141,7 +142,7 @@ public ApiResponse<List<StudyResponse.studyPlaceDto>> getStudyPlace(@AuthMember
}

@Override
@PostMapping("/place/{studyPlaceId}/update")
@PatchMapping("/place/{studyPlaceId}/update")
public ApiResponse<StudyResponse.studyPlaceDto> updateStudyPlace(
@AuthMember Member member,
@RequestBody @Valid StudyRequest.studyPlaceRequest request,
Expand Down
Loading
Loading