Skip to content

Commit

Permalink
�feat: 방장 위임 기능 구현 (#81)
Browse files Browse the repository at this point in the history
* feat: 방장 위임 기능 구현

* test: 방장 위임 기능 테스트 작성

* test: 방장이 아닌 유저의 요청인 경우 추가
  • Loading branch information
ymkim97 authored Nov 14, 2023
1 parent ca77e2b commit 622bc97
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/main/java/com/moabam/api/application/room/RoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ public Long createRoom(Long memberId, CreateRoomRequest createRoomRequest) {
@Transactional
public void modifyRoom(Long memberId, Long roomId, ModifyRoomRequest modifyRoomRequest) {
Participant participant = getParticipant(memberId, roomId);

if (!participant.isManager()) {
throw new ForbiddenException(ROOM_MODIFY_UNAUTHORIZED_REQUEST);
}
validateManagerAuthorization(participant);

Room room = participant.getRoom();
room.changeTitle(modifyRoomRequest.title());
Expand Down Expand Up @@ -119,6 +116,16 @@ public void exitRoom(Long memberId, Long roomId) {
roomRepository.delete(room);
}

@Transactional
public void mandateRoomManager(Long managerId, Long roomId, Long memberId) {
Participant managerParticipant = getParticipant(managerId, roomId);
Participant memberParticipant = getParticipant(memberId, roomId);
validateManagerAuthorization(managerParticipant);

managerParticipant.disableManager();
memberParticipant.enableManager();
}

public void validateRoomById(Long roomId) {
if (!roomRepository.existsById(roomId)) {
throw new NotFoundException(ROOM_NOT_FOUND);
Expand All @@ -130,6 +137,12 @@ private Participant getParticipant(Long memberId, Long roomId) {
.orElseThrow(() -> new NotFoundException(PARTICIPANT_NOT_FOUND));
}

private void validateManagerAuthorization(Participant participant) {
if (!participant.isManager()) {
throw new ForbiddenException(ROOM_MODIFY_UNAUTHORIZED_REQUEST);
}
}

private void validateRoomEnter(Long memberId, String requestPassword, Room room) {
if (!isEnterRoomAvailable(memberId, room.getRoomType())) {
throw new BadRequestException(MEMBER_ROOM_EXCEED);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/moabam/api/presentation/RoomController.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,10 @@ public RoomDetailsResponse getRoomDetails(@PathVariable("roomId") Long roomId) {
public void certifyRoom(@PathVariable("roomId") Long roomId, @RequestPart List<MultipartFile> multipartFiles) {
roomCertificationService.certifyRoom(1L, roomId, multipartFiles);
}

@PutMapping("/{roomId}/members/{memberId}/mandate")
@ResponseStatus(HttpStatus.OK)
public void mandateManager(@PathVariable("roomId") Long roomId, @PathVariable("memberId") Long memberId) {
roomService.mandateRoomManager(1L, roomId, memberId);
}
}
53 changes: 53 additions & 0 deletions src/test/java/com/moabam/api/application/RoomServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -26,6 +27,8 @@
import com.moabam.api.domain.room.repository.RoomRepository;
import com.moabam.api.domain.room.repository.RoutineRepository;
import com.moabam.api.dto.room.CreateRoomRequest;
import com.moabam.global.error.exception.ForbiddenException;
import com.moabam.support.fixture.RoomFixture;

@ExtendWith(MockitoExtension.class)
class RoomServiceTest {
Expand Down Expand Up @@ -97,4 +100,54 @@ void create_room_with_password_success() {
assertThat(result).isEqualTo(expectedRoom.getId());
assertThat(expectedRoom.getPassword()).isEqualTo("1234");
}

@DisplayName("방장 위임 성공")
@Test
void room_manager_mandate_success() {
// given
Long managerId = 1L;
Long memberId = 2L;

Room room = spy(RoomFixture.room());
given(room.getId()).willReturn(1L);

Participant memberParticipant = RoomFixture.participant(room, memberId);
Participant managerParticipant = RoomFixture.participant(room, managerId);
managerParticipant.enableManager();

given(participantSearchRepository.findOne(memberId, room.getId())).willReturn(
Optional.of(memberParticipant));
given(participantSearchRepository.findOne(managerId, room.getId())).willReturn(
Optional.of(managerParticipant));

// when
roomService.mandateRoomManager(managerId, room.getId(), memberId);

// then
assertThat(managerParticipant.isManager()).isFalse();
assertThat(memberParticipant.isManager()).isTrue();
}

@DisplayName("방장 위임 실패 - 방장이 아닌 유저가 요청할때")
@Test
void room_manager_mandate_fail() {
// given
Long managerId = 1L;
Long memberId = 2L;

Room room = spy(RoomFixture.room());
given(room.getId()).willReturn(1L);

Participant memberParticipant = RoomFixture.participant(room, memberId);
Participant managerParticipant = RoomFixture.participant(room, managerId);

given(participantSearchRepository.findOne(memberId, room.getId())).willReturn(
Optional.of(memberParticipant));
given(participantSearchRepository.findOne(managerId, room.getId())).willReturn(
Optional.of(managerParticipant));

// when, then
assertThatThrownBy(() -> roomService.mandateRoomManager(managerId, 1L, memberId))
.isInstanceOf(ForbiddenException.class);
}
}

0 comments on commit 622bc97

Please sign in to comment.