From aa06913b76325eaf17609f4dd14b072f53fdf18c Mon Sep 17 00:00:00 2001 From: SangWoon123 Date: Mon, 15 Apr 2024 21:07:14 +0900 Subject: [PATCH] chore --- .../group/controller/GroupRoomController.java | 2 +- .../domain/group/entity/GroupRoom.java | 3 +- .../group/service/GroupRoomService.java | 10 ++++- .../planding/global/error/ErrorCode.java | 5 ++- .../group/service/GroupRoomServiceTest.java | 13 +++--- .../group/service/GroupScheduleTest.java | 41 ++++++++++--------- .../controller/ScheduleControllerTest.java | 3 +- .../schedule/service/ScheduleServiceTest.java | 3 +- 8 files changed, 47 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/tukorea/planding/domain/group/controller/GroupRoomController.java b/src/main/java/com/tukorea/planding/domain/group/controller/GroupRoomController.java index e87efd6..a62e4e6 100644 --- a/src/main/java/com/tukorea/planding/domain/group/controller/GroupRoomController.java +++ b/src/main/java/com/tukorea/planding/domain/group/controller/GroupRoomController.java @@ -32,7 +32,7 @@ public CommonResponse createGroupRoom(@AuthenticationPrincipal Us @Operation(summary = "다른유저 그룹으로 초대") @PostMapping("/invite") public CommonResponse inviteGroupRoom(@AuthenticationPrincipal UserInfo userInfo, @RequestBody GroupInviteRequest invitedUser) { - GroupResponse groupResponse = groupRoomService.inviteGroupRoom(userInfo, invitedUser); + GroupResponse groupResponse = groupRoomService.handleInvitation(userInfo, invitedUser); return CommonUtils.success(groupResponse); } diff --git a/src/main/java/com/tukorea/planding/domain/group/entity/GroupRoom.java b/src/main/java/com/tukorea/planding/domain/group/entity/GroupRoom.java index 0d1c240..32c7e38 100644 --- a/src/main/java/com/tukorea/planding/domain/group/entity/GroupRoom.java +++ b/src/main/java/com/tukorea/planding/domain/group/entity/GroupRoom.java @@ -1,6 +1,7 @@ package com.tukorea.planding.domain.group.entity; -import com.tukorea.planding.domain.user.entity.User;import com.tukorea.planding.global.audit.BaseEntity; +import com.tukorea.planding.domain.user.entity.User; +import com.tukorea.planding.global.audit.BaseEntity; import com.tukorea.planding.domain.schedule.entity.Schedule; import jakarta.persistence.*; import lombok.*; diff --git a/src/main/java/com/tukorea/planding/domain/group/service/GroupRoomService.java b/src/main/java/com/tukorea/planding/domain/group/service/GroupRoomService.java index 765020e..09d2a57 100644 --- a/src/main/java/com/tukorea/planding/domain/group/service/GroupRoomService.java +++ b/src/main/java/com/tukorea/planding/domain/group/service/GroupRoomService.java @@ -48,7 +48,7 @@ public GroupResponse createGroupRoom(UserInfo userInfo, GroupCreateRequest creat } @Transactional - public GroupResponse inviteGroupRoom(UserInfo userInfo, GroupInviteRequest invitedUserInfo) { + public GroupResponse handleInvitation(UserInfo userInfo, GroupInviteRequest invitedUserInfo) { // 초대하는 유저가 존재하는지 체크하는 로직 User invitingUser = userRepository.findByUserCode(userInfo.getUserCode()) .orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND)); @@ -61,8 +61,16 @@ public GroupResponse inviteGroupRoom(UserInfo userInfo, GroupInviteRequest invit User invitedUser = userRepository.findByUserCode(invitedUserInfo.getUserCode()) .orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND)); + + // 초대한 유저가 이미 그룹에 속해 있는지 확인 + if (groupRoom.getGroupMemberships().contains(invitedUser)) { + throw new BusinessException(ErrorCode.USER_ALREADY_INVITED); + } + groupRoom.addUser(invitedUser); + + // 중간테이블에 유저, 그룹 정보 저장 userGroupMembershipRepository.saveAll(groupRoom.getGroupMemberships()); diff --git a/src/main/java/com/tukorea/planding/global/error/ErrorCode.java b/src/main/java/com/tukorea/planding/global/error/ErrorCode.java index 6daddba..e873360 100644 --- a/src/main/java/com/tukorea/planding/global/error/ErrorCode.java +++ b/src/main/java/com/tukorea/planding/global/error/ErrorCode.java @@ -11,7 +11,7 @@ public enum ErrorCode { /** * User Error */ - USER_NOT_FOUND("USER-001", "유저가 존재하지 않습니다.",HttpStatus.NOT_FOUND), + USER_NOT_FOUND("USER-001", "유저가 존재하지 않습니다.", HttpStatus.NOT_FOUND), /** * Schedule Error @@ -25,7 +25,8 @@ public enum ErrorCode { ACCESS_DENIED("GROUP-001", "본 사용자는 이 그룹에 접근할 권한이 없습니다.", HttpStatus.FORBIDDEN), USER_NOT_INVITABLE("GROUP-002", "초대할 유저를 찾지 못하였습니다.", HttpStatus.NOT_FOUND), GROUP_ROOM_NOT_FOUND("GROUP-003", "그룹룸이 존재하지 않습니다.", HttpStatus.NOT_FOUND), - UNAUTHORIZED_GROUP_ROOM_INVITATION("GROUP-004", "그룹룸에 초대할 권한이 없습니다.", HttpStatus.UNAUTHORIZED); + UNAUTHORIZED_GROUP_ROOM_INVITATION("GROUP-004", "그룹룸에 초대할 권한이 없습니다.", HttpStatus.UNAUTHORIZED), + USER_ALREADY_INVITED("GROUP-005", "이미 그룹룸에 초대되었습니다.", HttpStatus.UNAUTHORIZED); private final String errorCode; diff --git a/src/test/java/com/tukorea/planding/group/service/GroupRoomServiceTest.java b/src/test/java/com/tukorea/planding/group/service/GroupRoomServiceTest.java index 6c4d761..1e91cf1 100644 --- a/src/test/java/com/tukorea/planding/group/service/GroupRoomServiceTest.java +++ b/src/test/java/com/tukorea/planding/group/service/GroupRoomServiceTest.java @@ -8,6 +8,7 @@ import com.tukorea.planding.domain.group.service.GroupRoomService; import com.tukorea.planding.domain.schedule.repository.ScheduleRepository; import com.tukorea.planding.domain.schedule.service.ScheduleService; +import com.tukorea.planding.domain.user.mapper.UserMapper; import com.tukorea.planding.domain.user.repository.UserRepository; import com.tukorea.planding.domain.user.entity.User; import com.tukorea.planding.domain.user.dto.UserInfo; @@ -45,7 +46,7 @@ void createGroupRoom() { .userCode("#abcd") .build(); - UserInfo userInfo = User.toUserInfo(user); + UserInfo userInfo = UserMapper.toUserInfo(user); userRepository.save(user); @@ -53,11 +54,11 @@ void createGroupRoom() { .name("first_group") .build()); - Optional getgroup = groupRoomRepository.findById(groupRoom.getId()); + Optional getgroup = groupRoomRepository.findById(groupRoom.id()); Assertions.assertNotNull(getgroup); - Assertions.assertEquals(groupRoom.getCode(), getgroup.get().getGroupCode()); - Assertions.assertEquals(groupRoom.getOwnerCode(), getgroup.get().getOwner()); + Assertions.assertEquals(groupRoom.code(), getgroup.get().getGroupCode()); + Assertions.assertEquals(groupRoom.ownerCode(), getgroup.get().getOwner()); } @Test @@ -125,7 +126,7 @@ public void inviteGroupService() { .build(); // 유저 B 그룹방에 초대 - groupRoomService.inviteGroupRoom(User.toUserInfo(userA), groupInviteRequest); + groupRoomService.handleInvitation(UserMapper.toUserInfo(userA), groupInviteRequest); // 그룹방에 유저 B가 초대되었는지 확인 GroupRoom savedGroupRoom = groupRoomRepository.findById(groupRoom.getId()).orElse(null); @@ -169,7 +170,7 @@ public void failInviteGroupService() { // 그룹방에 유저 B가 초대되었는지 확인 Assertions.assertThrows(IllegalArgumentException.class, () -> - groupRoomService.inviteGroupRoom(User.toUserInfo(userC), groupInviteRequest), + groupRoomService.handleInvitation(UserMapper.toUserInfo(userC), groupInviteRequest), "User does not have permission to invite this groupRoom"); } } \ No newline at end of file diff --git a/src/test/java/com/tukorea/planding/group/service/GroupScheduleTest.java b/src/test/java/com/tukorea/planding/group/service/GroupScheduleTest.java index df4cab1..b3072e1 100644 --- a/src/test/java/com/tukorea/planding/group/service/GroupScheduleTest.java +++ b/src/test/java/com/tukorea/planding/group/service/GroupScheduleTest.java @@ -14,6 +14,7 @@ import com.tukorea.planding.domain.schedule.repository.ScheduleRepository; import com.tukorea.planding.domain.schedule.service.ScheduleService; import com.tukorea.planding.domain.user.entity.User; +import com.tukorea.planding.domain.user.mapper.UserMapper; import com.tukorea.planding.domain.user.repository.UserRepository; import com.tukorea.planding.global.error.BusinessException; import com.tukorea.planding.global.error.ErrorCode; @@ -60,7 +61,7 @@ public class GroupScheduleTest { @DisplayName("성공: 유저 A가 작성한 스케줄 조회") public void createGroupScheduleTest() { User user = createUserAndSave(TEST_EMAIL, "code"); - GroupResponse groupRoom = groupRoomService.createGroupRoom(User.toUserInfo(user), GroupCreateRequest + GroupResponse groupRoom = groupRoomService.createGroupRoom(UserMapper.toUserInfo(user), GroupCreateRequest .builder() .name("group_name") .build()); @@ -101,7 +102,7 @@ public void createGroupScheduleTest() { public void createGroupScheduleTest2() { //given User userA = createUserAndSave(TEST_EMAIL, "code"); - GroupResponse groupRoom = groupRoomService.createGroupRoom(User.toUserInfo(userA), GroupCreateRequest + GroupResponse groupRoom = groupRoomService.createGroupRoom(UserMapper.toUserInfo(userA), GroupCreateRequest .builder() .name("group_name") .build()); @@ -114,7 +115,7 @@ public void createGroupScheduleTest2() { .userCode(userB.getUserCode()) .build(); - groupRoomService.inviteGroupRoom(User.toUserInfo(userA), groupInviteRequest); + groupRoomService.handleInvitation(UserMapper.toUserInfo(userA), groupInviteRequest); LocalTime startTime = LocalTime.of(7, 0); LocalTime endTime = LocalTime.of(9, 0); @@ -132,7 +133,7 @@ public void createGroupScheduleTest2() { groupScheduleService.createGroupSchedule(groupRoom.code(), requestSchedule); //then - List result = scheduleService.getSchedulesByGroupRoom(groupRoom.id(), User.toUserInfo(userB)); + List result = scheduleService.getSchedulesByGroupRoom(groupRoom.id(), UserMapper.toUserInfo(userB)); assertNotNull(result); assertEquals(result.get(0).title(), requestSchedule.title()); @@ -146,7 +147,7 @@ public void createGroupScheduleTest2() { public void createGroupScheduleFailTest() { //given User userA = createUserAndSave(TEST_EMAIL, "code"); - GroupResponse groupRoom = groupRoomService.createGroupRoom(User.toUserInfo(userA), GroupCreateRequest + GroupResponse groupRoom = groupRoomService.createGroupRoom(UserMapper.toUserInfo(userA), GroupCreateRequest .builder() .name("group_name") .build()); @@ -169,14 +170,14 @@ public void createGroupScheduleFailTest() { groupScheduleService.createGroupSchedule(groupRoom.code(), requestSchedule); //then - assertThrows(BusinessException.class, () -> scheduleService.getSchedulesByGroupRoom(groupRoom.id(), User.toUserInfo(userC))); + assertThrows(BusinessException.class, () -> scheduleService.getSchedulesByGroupRoom(groupRoom.id(), UserMapper.toUserInfo(userC))); } @Test @DisplayName("성공: 유저 A가 작성한 스케줄 수정") public void update1() { User user = createUserAndSave(TEST_EMAIL, "code"); - GroupResponse groupRoom = groupRoomService.createGroupRoom(User.toUserInfo(user), GroupCreateRequest + GroupResponse groupRoom = groupRoomService.createGroupRoom(UserMapper.toUserInfo(user), GroupCreateRequest .builder() .name("group_name") .build()); @@ -208,7 +209,7 @@ public void update1() { .build(); //when - scheduleService.updateScheduleByGroupRoom(groupRoom.id(), schedule.getId(), updateSchedule, User.toUserInfo(user)); + scheduleService.updateScheduleByGroupRoom(groupRoom.id(), schedule.getId(), updateSchedule, UserMapper.toUserInfo(user)); //then Schedule result = scheduleRepository.findById(groupSchedule.id()) @@ -223,7 +224,7 @@ public void update1() { public void update2() { //given User userA = createUserAndSave(TEST_EMAIL, "code"); - GroupResponse groupRoom = groupRoomService.createGroupRoom(User.toUserInfo(userA), GroupCreateRequest + GroupResponse groupRoom = groupRoomService.createGroupRoom(UserMapper.toUserInfo(userA), GroupCreateRequest .builder() .name("group_name") .build()); @@ -236,7 +237,7 @@ public void update2() { .userCode(userB.getUserCode()) .build(); - groupRoomService.inviteGroupRoom(User.toUserInfo(userA), groupInviteRequest); + groupRoomService.handleInvitation(UserMapper.toUserInfo(userA), groupInviteRequest); LocalTime startTime = LocalTime.of(7, 0); LocalTime endTime = LocalTime.of(9, 0); @@ -265,7 +266,7 @@ public void update2() { .build(); //when - scheduleService.updateScheduleByGroupRoom(groupRoom.id(), schedule.getId(), updateSchedule, User.toUserInfo(userB)); + scheduleService.updateScheduleByGroupRoom(groupRoom.id(), schedule.getId(), updateSchedule, UserMapper.toUserInfo(userB)); //then Schedule result = scheduleRepository.findById(groupSchedule.id()) @@ -279,7 +280,7 @@ public void update2() { @DisplayName("실패: 외부 유저C가 수정") public void update3() { User user = createUserAndSave(TEST_EMAIL, "code"); - GroupResponse groupRoom = groupRoomService.createGroupRoom(User.toUserInfo(user), GroupCreateRequest + GroupResponse groupRoom = groupRoomService.createGroupRoom(UserMapper.toUserInfo(user), GroupCreateRequest .builder() .name("group_name") .build()); @@ -313,14 +314,14 @@ public void update3() { .build(); //when - assertThrows(BusinessException.class, () -> scheduleService.updateScheduleByGroupRoom(groupRoom.id(), schedule.getId(), updateSchedule, User.toUserInfo(userC))); + assertThrows(BusinessException.class, () -> scheduleService.updateScheduleByGroupRoom(groupRoom.id(), schedule.getId(), updateSchedule, UserMapper.toUserInfo(userC))); } @Test @DisplayName("성공: 유저 A가 작성한 스케줄 삭제") public void delete1() { User user = createUserAndSave(TEST_EMAIL, "code"); - GroupResponse groupRoom = groupRoomService.createGroupRoom(User.toUserInfo(user), GroupCreateRequest + GroupResponse groupRoom = groupRoomService.createGroupRoom(UserMapper.toUserInfo(user), GroupCreateRequest .builder() .name("group_name") .build()); @@ -340,7 +341,7 @@ public void delete1() { // 스케줄 생성 ScheduleResponse groupSchedule = groupScheduleService.createGroupSchedule(groupRoom.code(), requestSchedule); - scheduleService.deleteScheduleByGroupRoom(groupRoom.id(), groupSchedule.id(), User.toUserInfo(user)); + scheduleService.deleteScheduleByGroupRoom(groupRoom.id(), groupSchedule.id(), UserMapper.toUserInfo(user)); assertThrows(BusinessException.class, () -> scheduleRepository.findById(groupSchedule.id()).orElseThrow(() -> new BusinessException(ErrorCode.SCHEDULE_NOT_FOUND))); @@ -351,7 +352,7 @@ public void delete1() { public void delete2() { //given User userA = createUserAndSave(TEST_EMAIL, "code"); - GroupResponse groupRoom = groupRoomService.createGroupRoom(User.toUserInfo(userA), GroupCreateRequest + GroupResponse groupRoom = groupRoomService.createGroupRoom(UserMapper.toUserInfo(userA), GroupCreateRequest .builder() .name("group_name") .build()); @@ -364,7 +365,7 @@ public void delete2() { .userCode(userB.getUserCode()) .build(); - groupRoomService.inviteGroupRoom(User.toUserInfo(userA), groupInviteRequest); + groupRoomService.handleInvitation(UserMapper.toUserInfo(userA), groupInviteRequest); LocalTime startTime = LocalTime.of(7, 0); LocalTime endTime = LocalTime.of(9, 0); @@ -382,7 +383,7 @@ public void delete2() { ScheduleResponse groupSchedule = groupScheduleService.createGroupSchedule(groupRoom.code(), requestSchedule); //when - scheduleService.deleteScheduleByGroupRoom(groupRoom.id(), groupSchedule.id(), User.toUserInfo(userB)); + scheduleService.deleteScheduleByGroupRoom(groupRoom.id(), groupSchedule.id(), UserMapper.toUserInfo(userB)); //then assertThrows(BusinessException.class, () -> scheduleRepository.findById(groupSchedule.id()).orElseThrow(() -> new BusinessException(ErrorCode.SCHEDULE_NOT_FOUND))); } @@ -391,7 +392,7 @@ public void delete2() { @DisplayName("실패: 외부 유저 C가 수정") public void delete3() { User user = createUserAndSave(TEST_EMAIL, "code"); - GroupResponse groupRoom = groupRoomService.createGroupRoom(User.toUserInfo(user), GroupCreateRequest + GroupResponse groupRoom = groupRoomService.createGroupRoom(UserMapper.toUserInfo(user), GroupCreateRequest .builder() .name("group_name") .build()); @@ -414,7 +415,7 @@ public void delete3() { ScheduleResponse groupSchedule = groupScheduleService.createGroupSchedule(groupRoom.code(), requestSchedule); //when - assertThrows(BusinessException.class, () -> scheduleService.deleteScheduleByGroupRoom(groupRoom.id(), groupSchedule.id(), User.toUserInfo(userC))); + assertThrows(BusinessException.class, () -> scheduleService.deleteScheduleByGroupRoom(groupRoom.id(), groupSchedule.id(), UserMapper.toUserInfo(userC))); } diff --git a/src/test/java/com/tukorea/planding/schedule/controller/ScheduleControllerTest.java b/src/test/java/com/tukorea/planding/schedule/controller/ScheduleControllerTest.java index 90d4c75..e19ca44 100644 --- a/src/test/java/com/tukorea/planding/schedule/controller/ScheduleControllerTest.java +++ b/src/test/java/com/tukorea/planding/schedule/controller/ScheduleControllerTest.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.tukorea.planding.domain.schedule.controller.ScheduleController; +import com.tukorea.planding.domain.user.mapper.UserMapper; import com.tukorea.planding.global.config.security.SecurityConfig; import com.tukorea.planding.global.config.security.jwt.JwtAuthenticationFilter; import com.tukorea.planding.domain.schedule.dto.ScheduleRequest; @@ -57,7 +58,7 @@ void createSchedule() throws Exception{ .title("test") .build(); - given(scheduleService.createSchedule(User.toUserInfo(user),schedule)).willThrow(UsernameNotFoundException.class); + given(scheduleService.createSchedule(UserMapper.toUserInfo(user),schedule)).willThrow(UsernameNotFoundException.class); ResultActions actions = mockMvc.perform(MockMvcRequestBuilders.post("/api/v1/schedule") diff --git a/src/test/java/com/tukorea/planding/schedule/service/ScheduleServiceTest.java b/src/test/java/com/tukorea/planding/schedule/service/ScheduleServiceTest.java index 41fb40c..464aeb1 100644 --- a/src/test/java/com/tukorea/planding/schedule/service/ScheduleServiceTest.java +++ b/src/test/java/com/tukorea/planding/schedule/service/ScheduleServiceTest.java @@ -7,6 +7,7 @@ import com.tukorea.planding.domain.schedule.dto.ScheduleResponse; import com.tukorea.planding.domain.schedule.repository.ScheduleRepositoryCustomImpl; import com.tukorea.planding.domain.schedule.service.ScheduleService; +import com.tukorea.planding.domain.user.mapper.UserMapper; import com.tukorea.planding.domain.user.repository.UserRepository; import com.tukorea.planding.domain.user.entity.User; import com.tukorea.planding.domain.user.dto.UserInfo; @@ -163,7 +164,7 @@ public void getWeekSchedule() { public void updateSchedule() { //given User user = createUserAndSave(TEST_EMAIL); - UserInfo userInfo = User.toUserInfo(user); + UserInfo userInfo = UserMapper.toUserInfo(user); LocalTime startTime = LocalTime.of(7, 0); LocalTime endTime = LocalTime.of(9, 0);