-
Notifications
You must be signed in to change notification settings - Fork 0
Feat: [FN-117] 그룹 수정 API 추가 #34
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/project/flipnote/group/model/GroupPutRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package project.flipnote.group.model; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import jakarta.validation.constraints.Size; | ||
| import project.flipnote.group.entity.Category; | ||
|
|
||
| public record GroupPutRequest( | ||
| @NotBlank | ||
| @Size(max = 50) | ||
| String name, | ||
|
|
||
| @NotNull | ||
| Category category, | ||
|
|
||
| @NotBlank | ||
| @Size(max = 150) | ||
| String description, | ||
|
|
||
| @NotNull | ||
| Boolean applicationRequired, | ||
|
|
||
| @NotNull | ||
| Boolean publicVisible, | ||
|
|
||
| @NotNull | ||
| Integer maxMember, | ||
|
|
||
| String image | ||
| ) { | ||
| } |
40 changes: 40 additions & 0 deletions
40
src/main/java/project/flipnote/group/model/GroupPutResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package project.flipnote.group.model; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| import project.flipnote.group.entity.Category; | ||
| import project.flipnote.group.entity.Group; | ||
|
|
||
| public record GroupPutResponse( | ||
| String name, | ||
|
|
||
| Category category, | ||
|
|
||
| String description, | ||
|
|
||
| Boolean applicationRequired, | ||
|
|
||
| Boolean publicVisible, | ||
|
|
||
| Integer maxMember, | ||
|
|
||
| String imageUrl, | ||
|
|
||
| LocalDateTime createdAt, | ||
|
|
||
| LocalDateTime modifiedAt | ||
| ) { | ||
| public static GroupPutResponse from(Group group) { | ||
| return new GroupPutResponse( | ||
| group.getName(), | ||
| group.getCategory(), | ||
| group.getDescription(), | ||
| group.getApplicationRequired(), | ||
| group.getPublicVisible(), | ||
| group.getMaxMember(), | ||
| group.getImageUrl(), | ||
| group.getCreatedAt(), | ||
| group.getModifiedAt() | ||
| ); | ||
| } | ||
| } |
54 changes: 54 additions & 0 deletions
54
src/main/java/project/flipnote/group/service/GroupPolicyService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package project.flipnote.group.service; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import org.redisson.api.RLock; | ||
| import org.redisson.api.RedissonClient; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import project.flipnote.common.exception.BizException; | ||
| import project.flipnote.common.exception.CommonErrorCode; | ||
| import project.flipnote.group.entity.Group; | ||
| import project.flipnote.group.exception.GroupErrorCode; | ||
| import project.flipnote.group.model.GroupPutRequest; | ||
| import project.flipnote.group.repository.GroupRepository; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class GroupPolicyService { | ||
| private final GroupRepository groupRepository; | ||
| private final RedissonClient redissonClient; | ||
|
|
||
| @Transactional | ||
| public Group changeGroup(Long groupId, GroupPutRequest req) { | ||
| String lockKey = "group_lock:" + groupId; | ||
| RLock lock = redissonClient.getLock(lockKey); | ||
|
|
||
| boolean isLocked = false; | ||
| try { | ||
| isLocked = lock.tryLock(2, 3, TimeUnit.SECONDS); | ||
| if (!isLocked) { | ||
| throw new BizException(CommonErrorCode.SERVICE_TEMPORARILY_UNAVAILABLE); | ||
| } | ||
|
|
||
| Group lockedGroup = groupRepository.findByIdForUpdate(groupId) | ||
| .orElseThrow(() -> new BizException(GroupErrorCode.GROUP_NOT_FOUND)); | ||
|
|
||
| lockedGroup.validateMaxMemberUpdatable(req.maxMember()); | ||
|
|
||
| lockedGroup.changeGroup(req); | ||
|
|
||
| return lockedGroup; | ||
|
|
||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new BizException(CommonErrorCode.SERVICE_TEMPORARILY_UNAVAILABLE); | ||
| } finally { | ||
| if (isLocked && lock.isHeldByCurrentThread()) { | ||
| lock.unlock(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/test/java/project/flipnote/group/service/GroupPolicyServiceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package project.flipnote.group.service; | ||
|
|
||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
| import static org.mockito.BDDMockito.*; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
| import org.redisson.api.RLock; | ||
| import org.redisson.api.RedissonClient; | ||
| import org.springframework.test.util.ReflectionTestUtils; | ||
|
|
||
| import project.flipnote.common.exception.BizException; | ||
| import project.flipnote.group.entity.Category; | ||
| import project.flipnote.group.entity.Group; | ||
| import project.flipnote.group.exception.GroupErrorCode; | ||
| import project.flipnote.group.model.GroupPutRequest; | ||
| import project.flipnote.group.repository.GroupRepository; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| class GroupPolicyServiceTest { | ||
|
|
||
| @InjectMocks | ||
| GroupPolicyService groupPolicyService; | ||
|
|
||
| @Mock | ||
| GroupRepository groupRepository; | ||
|
|
||
| @Mock | ||
| RedissonClient redissonClient; | ||
|
|
||
| @Mock | ||
| RLock rLock; | ||
|
|
||
| @Test | ||
| void 실패_유저수보다_작게_변경() throws Exception { | ||
| Long groupId = 1L; | ||
| Group group = Group.builder() | ||
| .name("그룹1") | ||
| .category(Category.IT) | ||
| .description("설명1") | ||
| .publicVisible(true) | ||
| .applicationRequired(true) | ||
| .maxMember(100) | ||
| .imageUrl("www.~~~") | ||
| .build(); | ||
|
|
||
| ReflectionTestUtils.setField(group, "id", 1L); | ||
| ReflectionTestUtils.setField(group, "memberCount", 100); | ||
|
|
||
| GroupPutRequest req = new GroupPutRequest("그룹1", Category.ENGLISH, "설명1", true, true, 50, "www.~~"); | ||
|
|
||
| given(redissonClient.getLock(anyString())).willReturn(rLock); | ||
| given(rLock.tryLock(anyLong(), anyLong(), any())).willReturn(true); | ||
| given(rLock.isHeldByCurrentThread()).willReturn(true); | ||
| given(groupRepository.findByIdForUpdate(groupId)).willReturn(Optional.of(group)); | ||
|
|
||
|
|
||
| //when & then | ||
| BizException exception = | ||
| assertThrows(BizException.class, () -> groupPolicyService.changeGroup(groupId, req)); | ||
|
|
||
| assertEquals(GroupErrorCode.INVALID_MEMBER_COUNT, exception.getErrorCode()); | ||
| then(rLock).should().unlock(); | ||
| } | ||
|
|
||
| @Test | ||
| void 그룹_수정_성공() throws Exception { | ||
| Long groupId = 1L; | ||
| Group group = Group.builder() | ||
| .name("그룹1") | ||
| .category(Category.IT) | ||
| .description("설명1") | ||
| .publicVisible(true) | ||
| .applicationRequired(true) | ||
| .maxMember(100) | ||
| .imageUrl("www.~~~") | ||
| .build(); | ||
|
|
||
| ReflectionTestUtils.setField(group, "id", 1L); | ||
| ReflectionTestUtils.setField(group, "memberCount", 3); | ||
|
|
||
| GroupPutRequest req = new GroupPutRequest("그룹1", Category.ENGLISH, "설명1", true, true, 50, "www.~~"); | ||
|
|
||
| given(redissonClient.getLock(anyString())).willReturn(rLock); | ||
| given(rLock.tryLock(anyLong(), anyLong(), any())).willReturn(true); | ||
| given(rLock.isHeldByCurrentThread()).willReturn(true); | ||
| given(groupRepository.findByIdForUpdate(groupId)).willReturn(Optional.of(group)); | ||
|
|
||
|
|
||
| //when | ||
| Group changeGroup = groupPolicyService.changeGroup(groupId, req); | ||
|
|
||
| assertEquals(req.name(), changeGroup.getName()); | ||
| assertEquals(req.category(), changeGroup.getCategory()); | ||
|
|
||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.