Skip to content

Commit

Permalink
chore
Browse files Browse the repository at this point in the history
  • Loading branch information
SangWoon123 committed Apr 15, 2024
1 parent a6f8cc3 commit aa06913
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public CommonResponse<GroupResponse> createGroupRoom(@AuthenticationPrincipal Us
@Operation(summary = "다른유저 그룹으로 초대")
@PostMapping("/invite")
public CommonResponse<GroupResponse> inviteGroupRoom(@AuthenticationPrincipal UserInfo userInfo, @RequestBody GroupInviteRequest invitedUser) {
GroupResponse groupResponse = groupRoomService.inviteGroupRoom(userInfo, invitedUser);
GroupResponse groupResponse = groupRoomService.handleInvitation(userInfo, invitedUser);
return CommonUtils.success(groupResponse);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,19 +46,19 @@ void createGroupRoom() {
.userCode("#abcd")
.build();

UserInfo userInfo = User.toUserInfo(user);
UserInfo userInfo = UserMapper.toUserInfo(user);

userRepository.save(user);

GroupResponse groupRoom = groupRoomService.createGroupRoom(userInfo, GroupCreateRequest.builder()
.name("first_group")
.build());

Optional<GroupRoom> getgroup = groupRoomRepository.findById(groupRoom.getId());
Optional<GroupRoom> 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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand All @@ -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);
Expand All @@ -132,7 +133,7 @@ public void createGroupScheduleTest2() {
groupScheduleService.createGroupSchedule(groupRoom.code(), requestSchedule);

//then
List<ScheduleResponse> result = scheduleService.getSchedulesByGroupRoom(groupRoom.id(), User.toUserInfo(userB));
List<ScheduleResponse> result = scheduleService.getSchedulesByGroupRoom(groupRoom.id(), UserMapper.toUserInfo(userB));

assertNotNull(result);
assertEquals(result.get(0).title(), requestSchedule.title());
Expand All @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -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())
Expand All @@ -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());
Expand All @@ -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);
Expand Down Expand Up @@ -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())
Expand All @@ -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());
Expand Down Expand Up @@ -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());
Expand All @@ -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)));
Expand All @@ -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());
Expand All @@ -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);
Expand All @@ -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)));
}
Expand All @@ -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());
Expand All @@ -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)));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit aa06913

Please sign in to comment.