Skip to content
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
27 changes: 14 additions & 13 deletions src/main/java/flipnote/group/adapter/out/entity/GroupEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import flipnote.group.domain.model.group.Category;
import flipnote.group.domain.model.group.JoinPolicy;
import flipnote.group.domain.model.group.Visibility;
import flipnote.group.domain.policy.BusinessException;
import flipnote.group.domain.policy.ErrorCode;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
Expand Down Expand Up @@ -110,36 +112,35 @@ public static GroupEntity create(CreateGroupCommand cmd) {
*/
private static void validate(CreateGroupCommand cmd) {
if (cmd == null) {
throw new IllegalArgumentException("command required");
throw new BusinessException(ErrorCode.INVALID_INPUT);
}

if (cmd.name() == null || cmd.name().isBlank()) {
throw new IllegalArgumentException("name required");
throw new BusinessException(ErrorCode.GROUP_INVALID_NAME);
}
if (cmd.name().length() > 50) {
throw new BusinessException(ErrorCode.GROUP_NAME_TOO_LONG);
}
if (cmd.maxMember() < 1 || cmd.maxMember() > 100) {
throw new IllegalArgumentException("maxMember invalid");
throw new BusinessException(ErrorCode.GROUP_INVALID_MAX_MEMBER);
}
if (cmd.category() == null) {
throw new IllegalArgumentException("category required");
throw new BusinessException(ErrorCode.GROUP_INVALID_CATEGORY);
}
if (cmd.joinPolicy() == null) {
throw new IllegalArgumentException("join required");
throw new BusinessException(ErrorCode.GROUP_INVALID_JOIN_POLICY);
}
if (cmd.visibility() == null) {
throw new IllegalArgumentException("visibility required");
throw new BusinessException(ErrorCode.GROUP_INVALID_VISIBILITY);
}
if (cmd.description() == null || cmd.description().isBlank()) {
throw new IllegalArgumentException("description required");
}
if (cmd.name().length() > 50) {
throw new IllegalArgumentException("name too long");
throw new BusinessException(ErrorCode.GROUP_INVALID_DESCRIPTION);
}
}

public void plusCount() {

if(this.memberCount+1 > this.maxMember) {
throw new IllegalArgumentException("max member");
throw new BusinessException(ErrorCode.GROUP_MEMBER_LIMIT_EXCEEDED);
}

this.memberCount++;
Expand All @@ -148,7 +149,7 @@ public void plusCount() {
public void minusCount() {

if(this.memberCount-1 < 0) {
throw new IllegalArgumentException("not minus member");
throw new BusinessException(ErrorCode.MEMBER_COUNT_UNDERFLOW);
}

this.memberCount--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import flipnote.group.application.port.out.GroupMemberRepositoryPort;
import flipnote.group.domain.model.member.GroupMemberRole;
import flipnote.group.domain.model.member.MemberInfo;
import flipnote.group.domain.policy.BusinessException;
import flipnote.group.domain.policy.ErrorCode;
import flipnote.group.infrastructure.persistence.jpa.GroupMemberRepository;
import flipnote.group.infrastructure.persistence.jpa.GroupRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -31,7 +33,7 @@ public void save(GroupMemberEntity groupMember) {
groupMemberRepository.save(groupMember);

GroupEntity groupEntity = groupRepository.findByIdForUpdate(groupMember.getGroupId()).orElseThrow(
() -> new IllegalArgumentException("not exist group")
() -> new BusinessException(ErrorCode.GROUP_NOT_FOUND)
);

//그룹 엔티티 + 1
Expand All @@ -49,7 +51,7 @@ public void existsUserInGroup(Long groupId, Long userId) {
boolean isMember = groupMemberRepository.existsByGroupIdAndUserId(groupId, userId);

if(!isMember) {
throw new IllegalArgumentException("user not in Group");
throw new BusinessException(ErrorCode.USER_NOT_IN_GROUP);
}
}

Expand All @@ -72,7 +74,7 @@ public List<MemberInfo> findMemberInfo(Long groupId) {
public boolean checkOwner(Long groupId, Long userId) {

GroupMemberEntity groupMember = groupMemberRepository.findByGroupIdAndUserId(groupId, userId).orElseThrow(
() -> new IllegalArgumentException("member not in group")
() -> new BusinessException(ErrorCode.USER_NOT_IN_GROUP)
);

return groupMember.getRole().equals(GroupMemberRole.OWNER);
Expand All @@ -82,7 +84,7 @@ public boolean checkOwner(Long groupId, Long userId) {
public GroupMemberEntity findMyRole(Long groupId, Long userId) {

GroupMemberEntity entity = groupMemberRepository.findByGroupIdAndUserId(groupId, userId).orElseThrow(
() -> new IllegalArgumentException("entity not exist")
() -> new BusinessException(ErrorCode.MEMBER_NOT_FOUND)
);

return entity;
Expand All @@ -92,14 +94,14 @@ public GroupMemberEntity findMyRole(Long groupId, Long userId) {
public void deleteGroupMember(Long memberId) {

GroupMemberEntity groupMember = groupMemberRepository.findById(memberId).orElseThrow(
() -> new IllegalArgumentException("not exist member")
() -> new BusinessException(ErrorCode.MEMBER_NOT_FOUND)
);

groupMemberRepository.deleteById(memberId);

//그룹 인원수 동기화
GroupEntity group = groupRepository.findByIdForUpdate(groupMember.getGroupId()).orElseThrow(
() -> new IllegalArgumentException("not exist group")
() -> new BusinessException(ErrorCode.GROUP_NOT_FOUND)
);

group.minusCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import flipnote.group.application.port.out.GroupRepositoryPort;
import flipnote.group.domain.model.group.Category;
import flipnote.group.domain.model.group.GroupInfo;
import flipnote.group.domain.policy.BusinessException;
import flipnote.group.domain.policy.ErrorCode;
import flipnote.group.infrastructure.persistence.jpa.GroupRepository;
import lombok.RequiredArgsConstructor;

Expand All @@ -30,15 +32,15 @@ public Long saveNewGroup(GroupEntity group) {
@Override
public GroupEntity findById(Long id) {
GroupEntity group = groupRepository.findById(id).orElseThrow(
() -> new IllegalArgumentException("Group not Exist")
() -> new BusinessException(ErrorCode.GROUP_NOT_FOUND)
);
return group;
}

@Override
public void delete(Long groupId) {
if (!groupRepository.existsById(groupId)) {
throw new IllegalArgumentException("Group not Exist");
throw new BusinessException(ErrorCode.GROUP_NOT_FOUND);
}
groupRepository.deleteById(groupId);
}
Expand All @@ -62,7 +64,7 @@ public List<GroupInfo> findAllByCursorAndCreatedUserId(Long cursorId, Category c
public boolean checkJoinable(Long groupId) {

GroupEntity groupEntity = groupRepository.findByIdForUpdate(groupId).orElseThrow(
() -> new IllegalArgumentException("not exists")
() -> new BusinessException(ErrorCode.GROUP_NOT_FOUND)
);

int maxMember = groupEntity.getMaxMember();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import flipnote.group.application.port.out.GroupRoleRepositoryPort;
import flipnote.group.domain.model.member.GroupMemberRole;
import flipnote.group.domain.model.permission.GroupPermission;
import flipnote.group.domain.policy.BusinessException;
import flipnote.group.domain.policy.ErrorCode;
import flipnote.group.infrastructure.persistence.jpa.GroupMemberRepository;
import flipnote.group.infrastructure.persistence.jpa.GroupRolePermissionRepository;
import flipnote.group.infrastructure.persistence.jpa.GroupRoleRepository;
Expand Down Expand Up @@ -90,7 +92,7 @@ public RoleEntity create(Long groupId) {
@Override
public boolean checkRole(Long userId, Long groupId, GroupMemberRole groupMemberRole) {
RoleEntity roleEntity = groupRoleRepository.findByGroupIdAndRole(groupId, groupMemberRole).orElseThrow(
() -> new IllegalArgumentException("not exist role")
() -> new BusinessException(ErrorCode.MEMBER_NOT_FOUND)
);
return groupMemberRepository.existsByUserIdAndRole_Id(userId, roleEntity.getId());
}
Expand All @@ -106,7 +108,7 @@ public boolean checkRole(Long userId, Long groupId, GroupMemberRole groupMemberR
public boolean checkPermission(Long userId, Long groupId, GroupPermission permission) {

GroupMemberEntity groupMember = groupMemberRepository.findByGroupIdAndUserId(groupId, userId).orElseThrow(
() -> new IllegalArgumentException("not exist member")
() -> new BusinessException(ErrorCode.MEMBER_NOT_FOUND)
);

return groupRoleRepository.existsByGroupIdAndRole(groupId, groupMember.getRole().getRole());
Expand All @@ -122,7 +124,7 @@ public boolean checkPermission(Long userId, Long groupId, GroupPermission permis
public List<GroupPermission> addPermission(Long groupId, GroupMemberRole role, GroupPermission permission) {

RoleEntity roleEntity = groupRoleRepository.findByGroupIdAndRole(groupId, role).orElseThrow(
() -> new IllegalArgumentException("not exist role")
() -> new BusinessException(ErrorCode.MEMBER_NOT_FOUND)
);

PermissionEntity permissionEntity = PermissionEntity.builder()
Expand Down Expand Up @@ -150,7 +152,7 @@ public List<GroupPermission> addPermission(Long groupId, GroupMemberRole role, G
public boolean existPermission(GroupMemberRole role, Long groupId, GroupPermission permission) {

RoleEntity roleEntity = groupRoleRepository.findByGroupIdAndRole(groupId, role).orElseThrow(
() -> new IllegalArgumentException("not exist role")
() -> new BusinessException(ErrorCode.MEMBER_NOT_FOUND)
);

return groupRolePermissionRepository.existsByGroupRoleIdAndPermission(roleEntity.getId(), permission);
Expand All @@ -159,14 +161,14 @@ public boolean existPermission(GroupMemberRole role, Long groupId, GroupPermissi
@Override
public RoleEntity findByIdAndRole(Long id, GroupMemberRole groupMemberRole) {
return groupRoleRepository.findByGroupIdAndRole(id, groupMemberRole).orElseThrow(
() -> new IllegalArgumentException("not exist role")
() -> new BusinessException(ErrorCode.MEMBER_NOT_FOUND)
);
}

@Override
public GroupMemberRole findRole(Long userId, Long groupId) {
GroupMemberEntity groupMember = groupMemberRepository.findByGroupIdAndUserId(groupId, userId).orElseThrow(
() -> new IllegalArgumentException("not exists member")
() -> new BusinessException(ErrorCode.MEMBER_NOT_FOUND)
);

return groupMember.getRole().getRole();
Expand All @@ -182,7 +184,7 @@ public GroupMemberRole findRole(Long userId, Long groupId) {
@Override
public List<GroupPermission> removePermission(Long groupId, GroupMemberRole role, GroupPermission permission) {
RoleEntity roleEntity = groupRoleRepository.findByGroupIdAndRole(groupId, role).orElseThrow(
() -> new IllegalArgumentException("not exists member")
() -> new BusinessException(ErrorCode.MEMBER_NOT_FOUND)
);

groupRolePermissionRepository.deleteByGroupRoleIdAndPermission(roleEntity.getId(), permission);
Expand All @@ -199,7 +201,7 @@ public List<GroupPermission> removePermission(Long groupId, GroupMemberRole role
public List<GroupPermission> findMyRolePermission(Long groupId, GroupMemberRole role) {

RoleEntity roleEntity = groupRoleRepository.findByGroupIdAndRole(groupId, role).orElseThrow(
() -> new IllegalArgumentException("not exists member")
() -> new BusinessException(ErrorCode.MEMBER_NOT_FOUND)
);

List<PermissionEntity> permissions = groupRolePermissionRepository.findAllByGroupRoleId(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import flipnote.group.adapter.out.entity.JoinEntity;
import flipnote.group.application.port.out.JoinRepositoryPort;
import flipnote.group.domain.policy.BusinessException;
import flipnote.group.domain.policy.ErrorCode;
import flipnote.group.infrastructure.persistence.jpa.JoinRepository;
import lombok.RequiredArgsConstructor;

Expand Down Expand Up @@ -37,7 +39,7 @@ public List<JoinEntity> findFormList(Long groupId) {
public JoinEntity findJoin(Long joinId) {

JoinEntity entity = joinRepository.findById(joinId).orElseThrow(
() -> new IllegalArgumentException("not exist")
() -> new BusinessException(ErrorCode.JOIN_NOT_FOUND)
);
return entity;
}
Expand Down
Empty file.
16 changes: 16 additions & 0 deletions src/main/java/flipnote/group/api/advice/ErrorResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package flipnote.group.api.advice;

import flipnote.group.domain.policy.ErrorCode;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class ErrorResponse {
private final String code;
private final String message;

public static ErrorResponse of(ErrorCode errorCode) {
return new ErrorResponse(errorCode.getCode(), errorCode.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package flipnote.group.api.advice;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import flipnote.group.domain.policy.BusinessException;
import flipnote.group.domain.policy.ErrorCode;

@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(BusinessException.class)
public ResponseEntity<ErrorResponse> handle(BusinessException e) {
ErrorCode errorCode = e.getErrorCode();
return ResponseEntity
.status(errorCode.getStatus())
.body(ErrorResponse.of(errorCode));
}

@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handle(Exception e) {
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(ErrorResponse.of(ErrorCode.INTERNAL_SERVER_ERROR));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import flipnote.group.application.port.out.GroupRoleRepositoryPort;
import flipnote.group.domain.model.member.GroupMemberRole;
import flipnote.group.domain.model.permission.GroupPermission;
import flipnote.group.domain.policy.BusinessException;
import flipnote.group.domain.policy.ErrorCode;
import lombok.RequiredArgsConstructor;

@Service
Expand All @@ -34,18 +36,18 @@ public AddPermissionResult addPermission(PermissionCommand cmd) {
boolean existHostPermission = groupRoleRepository.checkPermission(cmd.userId(), cmd.groupId(), cmd.permission());

if(!existHostPermission) {
throw new IllegalArgumentException("host not exist permission");
throw new BusinessException(ErrorCode.PERMISSION_DENIED);
}

//권한이 낮을 경우
if(!role.isHigherThan(cmd.changeRole())) {
throw new IllegalArgumentException("host lower than changeRole");
throw new BusinessException(ErrorCode.PERMISSION_DENIED);
}

boolean existPermission = groupRoleRepository.existPermission(cmd.changeRole(), cmd.groupId(), cmd.permission());

if(existPermission) {
throw new IllegalArgumentException("already exist permission");
throw new BusinessException(ErrorCode.PERMISSION_ALREADY_EXISTS);
}

List<GroupPermission> groupPermissions = groupRoleRepository.addPermission(cmd.groupId(), cmd.changeRole(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import flipnote.group.domain.model.join.JoinStatus;
import flipnote.group.domain.model.member.GroupMemberRole;
import flipnote.group.domain.model.permission.GroupPermission;
import flipnote.group.domain.policy.BusinessException;
import flipnote.group.domain.policy.ErrorCode;
import lombok.RequiredArgsConstructor;

@Service
Expand Down Expand Up @@ -52,7 +54,7 @@ public ApplicationFormResult joinRequest(ApplicationFormCommand cmd) {

//이미 가입 신청 여부
if(joinRepository.existsJoin(cmd.groupId(), cmd.userId())) {
throw new IllegalArgumentException("already join");
throw new BusinessException(ErrorCode.JOIN_ALREADY_EXISTS);
}

JoinStatus status = JoinStatus.ACCEPT;
Expand Down Expand Up @@ -85,7 +87,7 @@ public FindJoinFormListResult findJoinFormList(FindJoinFormCommand cmd) {
boolean checkPermission = groupRoleRepository.checkPermission(cmd.userId(), cmd.groupId(), JOIN_MANAGE);

if(!checkPermission) {
throw new IllegalArgumentException("not permission");
throw new BusinessException(ErrorCode.PERMISSION_DENIED);
}

List<JoinEntity> joinDomainList = joinRepository.findFormList(cmd.groupId());
Expand All @@ -97,12 +99,12 @@ public FindJoinFormListResult findJoinFormList(FindJoinFormCommand cmd) {
private void checkJoinable(GroupEntity group) {
//비공개 그룹 인지 확인
if(group.getVisibility().equals(Visibility.PRIVATE)) {
throw new IllegalArgumentException("private group");
throw new BusinessException(ErrorCode.GROUP_PRIVATE);
}

//멤버가 최대인 경우
if(group.getMemberCount() >= group.getMaxMember()) {
throw new IllegalArgumentException("max member");
throw new BusinessException(ErrorCode.GROUP_MEMBER_LIMIT_EXCEEDED);
}
}
}
Loading