Skip to content

Commit

Permalink
feat : 유효성 검사 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
dnd2dnd committed Oct 27, 2024
1 parent dc72310 commit 0a31a5e
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ public static BranchResponse from(Branch branch) {
return new BranchResponse(branch.getId(), branch.getName());
}

public static BranchResponse createNotificationBranch(Branch branch) {
return new BranchResponse(
branch.getId(),
branch.getParentBranch().getName() + " - " + branch.getName()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package kr.co.conceptbe.notification_setting.application;

import jakarta.transaction.Transactional;
import java.util.HashSet;
import java.util.stream.Collectors;
import kr.co.conceptbe.auth.presentation.dto.AuthCredentials;
import kr.co.conceptbe.branch.domain.Branch;
import kr.co.conceptbe.branch.domain.persistense.BranchRepository;
import kr.co.conceptbe.common.auth.Auth;
import kr.co.conceptbe.idea.domain.Idea;
Expand All @@ -12,6 +15,7 @@
import kr.co.conceptbe.notification_setting.application.dto.NotificationSettingRequest;
import kr.co.conceptbe.notification_setting.domain.IdeaNotificationSetting;
import kr.co.conceptbe.notification_setting.domain.repository.NotificationSettingRepository;
import kr.co.conceptbe.purpose.domain.Purpose;
import kr.co.conceptbe.purpose.domain.persistence.PurposeRepository;
import kr.co.conceptbe.skill.domain.SkillCategoryRepository;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -42,8 +46,8 @@ public NotificationSettingMemberInfoResponse getNotificationSettingMemberInfo(Au
public Long createNotificationSetting(AuthCredentials auth, NotificationSettingRequest notificationSettingRequest) {
IdeaNotificationSetting ideaNotificationSetting = IdeaNotificationSetting.of(
auth.id(),
branchRepository.findByIdIn(notificationSettingRequest.branchIds()),
purposeRepository.findByIdIn(notificationSettingRequest.purposeIds()),
new HashSet<>(branchRepository.findByIdIn(notificationSettingRequest.branchIds())),
new HashSet<>(purposeRepository.findByIdIn(notificationSettingRequest.purposeIds())),
notificationSettingRequest.cooperationWay()
);

Expand All @@ -62,8 +66,8 @@ public void updateNotificationSetting(

IdeaNotificationSetting ideaNotificationSetting = notificationSettingRepository.findByMemberId(memberId);
ideaNotificationSetting.update(
branchRepository.findByIdIn(notificationSettingRequest.branchIds()),
purposeRepository.findByIdIn(notificationSettingRequest.purposeIds()),
new HashSet<>(branchRepository.findByIdIn(notificationSettingRequest.branchIds())),
new HashSet<>(purposeRepository.findByIdIn(notificationSettingRequest.purposeIds())),
notificationSettingRequest.cooperationWay()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static NotificationSettingMemberInfoResponse from(IdeaNotificationSetting
ideaNotificationSetting.getBranches()
.getNotificationSettingBranches()
.stream()
.map(branch -> BranchResponse.from(branch.getBranch()))
.map(branch -> BranchResponse.createNotificationBranch(branch.getBranch()))
.toList(),
ideaNotificationSetting.getCooperationWay()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import jakarta.persistence.*;
import java.util.List;
import java.util.Set;
import kr.co.conceptbe.branch.domain.Branch;
import kr.co.conceptbe.branch.exception.InvalidBranchLengthException;
import kr.co.conceptbe.common.entity.base.BaseTimeEntity;
import kr.co.conceptbe.idea.domain.vo.CooperationWay;
import kr.co.conceptbe.notification_setting.domain.vo.NotificationSettingBranches;
import kr.co.conceptbe.notification_setting.domain.vo.NotificationSettingPurposes;
import kr.co.conceptbe.notification_setting.exception.InvalidBranchException;
import kr.co.conceptbe.notification_setting.exception.InvalidPurposeException;
import kr.co.conceptbe.purpose.domain.Purpose;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -41,17 +45,20 @@ public class IdeaNotificationSetting extends BaseTimeEntity {
@Enumerated(EnumType.STRING)
private CooperationWay cooperationWay;

public IdeaNotificationSetting(Long memberId, CooperationWay cooperationWay) {
private IdeaNotificationSetting(Long memberId, CooperationWay cooperationWay) {
this.memberId = memberId;
this.cooperationWay = cooperationWay;
}

public static IdeaNotificationSetting of(
Long memberId,
List<Branch> branches,
List<Purpose> purposes,
Set<Branch> branches,
Set<Purpose> purposes,
CooperationWay cooperationWay
) {
validatePurpose(purposes);
validateBranch(branches);

IdeaNotificationSetting ideaNotificationSetting = new IdeaNotificationSetting(
memberId,
cooperationWay
Expand All @@ -63,13 +70,28 @@ public static IdeaNotificationSetting of(
}

public void update(
List<Branch> branches,
List<Purpose> purposes,
Set<Branch> branches,
Set<Purpose> purposes,
CooperationWay cooperationWay
) {
validatePurpose(purposes);
validateBranch(branches);

this.branches.update(this, branches);
this.purposes.update(this, purposes);
this.cooperationWay = cooperationWay;
}

private static void validatePurpose(Set<Purpose> purposes) {
if (purposes.isEmpty()) {
throw new InvalidPurposeException();
}
}

private static void validateBranch(Set<Branch> branches) {
if (branches.isEmpty() || branches.size() > 10) {
throw new InvalidBranchException();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.persistence.CascadeType;
import jakarta.persistence.Embeddable;
import jakarta.persistence.OneToMany;
import java.util.Set;
import java.util.stream.Collectors;
import kr.co.conceptbe.branch.domain.Branch;
import kr.co.conceptbe.notification_setting.domain.IdeaNotificationSetting;
Expand Down Expand Up @@ -33,7 +34,7 @@ private NotificationSettingBranches(List<NotificationSettingBranch> notification

public static NotificationSettingBranches of(
IdeaNotificationSetting ideaNotificationSetting,
List<Branch> branches
Set<Branch> branches
) {
List<NotificationSettingBranch> notificationSettingBranches = branches.stream()
.map(branch -> NotificationSettingBranch.of(ideaNotificationSetting, branch))
Expand All @@ -42,7 +43,7 @@ public static NotificationSettingBranches of(
return new NotificationSettingBranches(notificationSettingBranches);
}

public void update(IdeaNotificationSetting ideaNotificationSetting, List<Branch> branches) {
public void update(IdeaNotificationSetting ideaNotificationSetting, Set<Branch> branches) {
notificationSettingBranches.clear();
notificationSettingBranches.addAll(
branches.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.persistence.CascadeType;
import jakarta.persistence.Embeddable;
import jakarta.persistence.OneToMany;
import java.util.Set;
import kr.co.conceptbe.notification_setting.domain.IdeaNotificationSetting;
import kr.co.conceptbe.purpose.domain.Purpose;
import lombok.Getter;
Expand Down Expand Up @@ -32,7 +33,7 @@ private NotificationSettingPurposes(List<NotificationSettingPurpose> notificatio

public static NotificationSettingPurposes of(
IdeaNotificationSetting ideaNotificationSetting,
List<Purpose> purposes
Set<Purpose> purposes
) {
List<NotificationSettingPurpose> notificationSettingPurposes = purposes.stream()
.map(purpose -> NotificationSettingPurpose.of(ideaNotificationSetting, purpose))
Expand All @@ -41,7 +42,7 @@ public static NotificationSettingPurposes of(
return new NotificationSettingPurposes(notificationSettingPurposes);
}

public void update(IdeaNotificationSetting ideaNotificationSetting, List<Purpose> purposes) {
public void update(IdeaNotificationSetting ideaNotificationSetting, Set<Purpose> purposes) {
notificationSettingPurposes.clear();
notificationSettingPurposes.addAll(
purposes.stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package kr.co.conceptbe.notification_setting.exception;

import static org.springframework.http.HttpStatus.BAD_REQUEST;

import kr.co.conceptbe.common.exception.ConceptBeException;
import kr.co.conceptbe.common.exception.ErrorCode;

public class InvalidBranchException extends ConceptBeException {

public InvalidBranchException() {
super(new ErrorCode(BAD_REQUEST, "1개 이상 10개 이하 선택해야합니다."));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package kr.co.conceptbe.notification_setting.exception;

import static org.springframework.http.HttpStatus.BAD_REQUEST;

import kr.co.conceptbe.common.exception.ConceptBeException;
import kr.co.conceptbe.common.exception.ErrorCode;

public class InvalidPurposeException extends ConceptBeException {

public InvalidPurposeException() {
super(new ErrorCode(BAD_REQUEST, "1개 이상 선택해야합니다."));
}

}

0 comments on commit 0a31a5e

Please sign in to comment.