Skip to content

Commit

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

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

* test: 방장이 아닌 유저의 요청인 경우 추가

* feat: participant deletedAt null일때 찾도록 추가

* feat: 방 추방 기능 구현

* test: 방 추방 통합 테스트 구현

* refactor: nginx conf 수정

* refactor: nginx conf 추가 수정

* refactor: nginx conf

* chore: config 업데이트
  • Loading branch information
ymkim97 authored Nov 14, 2023
1 parent 2500f8c commit 261f698
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 10 deletions.
12 changes: 8 additions & 4 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ http {

server_tokens off;

include ./conf.d/header.conf
map $http_upgrade $connection_upgrade {
default "upgrade";
}

include conf.d/header.conf;

log_format main '$remote_addr $remote_user "$request" '
'$status $body_bytes_sent "$http_referer" "$request_time" '
'"$http_user_agent" ';

include ./conf.d/upstream.conf
include ./conf.d/http-server.conf
include ./conf.d/ssl-server.conf;
include conf.d/upstream.conf;
include conf.d/http-server.conf;
include conf.d/ssl-server.conf;
}
19 changes: 19 additions & 0 deletions src/main/java/com/moabam/api/application/room/RoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ public Long createRoom(Long memberId, CreateRoomRequest createRoomRequest) {
.memberId(memberId)
.build();

if (!isEnterRoomAvailable(memberId, room.getRoomType())) {
throw new BadRequestException(MEMBER_ROOM_EXCEED);
}

increaseRoomCount(memberId, room.getRoomType());

participant.enableManager();
Room savedRoom = roomRepository.save(room);
routineRepository.saveAll(routines);
Expand Down Expand Up @@ -126,6 +132,19 @@ public void mandateRoomManager(Long managerId, Long roomId, Long memberId) {
memberParticipant.enableManager();
}

@Transactional
public void deportParticipant(Long managerId, Long roomId, Long memberId) {
Participant managerParticipant = getParticipant(managerId, roomId);
Participant memberParticipant = getParticipant(memberId, roomId);
Room room = managerParticipant.getRoom();

validateManagerAuthorization(managerParticipant);

participantRepository.delete(memberParticipant);
room.decreaseCurrentUserCount();
decreaseRoomCount(memberId, room.getRoomType());
}

public void validateRoomById(Long roomId) {
if (!roomRepository.existsById(roomId)) {
throw new NotFoundException(ROOM_NOT_FOUND);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public Optional<Participant> findOne(Long memberId, Long roomId) {
.join(participant.room, room).fetchJoin()
.where(
DynamicQuery.generateEq(roomId, participant.room.id::eq),
DynamicQuery.generateEq(memberId, participant.memberId::eq)
DynamicQuery.generateEq(memberId, participant.memberId::eq),
participant.deletedAt.isNull()
)
.fetchOne()
);
Expand All @@ -37,7 +38,8 @@ public List<Participant> findParticipants(Long roomId) {
return jpaQueryFactory
.selectFrom(participant)
.where(
participant.room.id.eq(roomId)
participant.room.id.eq(roomId),
participant.deletedAt.isNull()
)
.fetch();
}
Expand All @@ -47,7 +49,8 @@ public List<Participant> findOtherParticipantsInRoom(Long memberId, Long roomId)
.selectFrom(participant)
.where(
participant.room.id.eq(roomId),
participant.memberId.ne(memberId)
participant.memberId.ne(memberId),
participant.deletedAt.isNull()
)
.fetch();
}
Expand All @@ -56,7 +59,10 @@ public List<Participant> findAllByRoomCertifyTime(int certifyTime) {
return jpaQueryFactory
.selectFrom(participant)
.join(participant.room, room).fetchJoin()
.where(participant.room.certifyTime.eq(certifyTime))
.where(
participant.room.certifyTime.eq(certifyTime),
participant.deletedAt.isNull()
)
.fetch();
}
}
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 @@ -77,4 +77,10 @@ public void certifyRoom(@PathVariable("roomId") Long roomId, @RequestPart List<M
public void mandateManager(@PathVariable("roomId") Long roomId, @PathVariable("memberId") Long memberId) {
roomService.mandateRoomManager(1L, roomId, memberId);
}

@DeleteMapping("/{roomId}/members/{memberId}")
@ResponseStatus(HttpStatus.OK)
public void deportParticipant(@PathVariable("roomId") Long roomId, @PathVariable("memberId") Long memberId) {
roomService.deportParticipant(1L, roomId, memberId);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/config
10 changes: 9 additions & 1 deletion src/test/java/com/moabam/api/application/RoomServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.moabam.api.application.member.MemberService;
import com.moabam.api.application.room.RoomService;
import com.moabam.api.application.room.mapper.RoomMapper;
import com.moabam.api.domain.member.Member;
import com.moabam.api.domain.room.Participant;
import com.moabam.api.domain.room.Room;
import com.moabam.api.domain.room.Routine;
Expand All @@ -28,6 +29,7 @@
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.MemberFixture;
import com.moabam.support.fixture.RoomFixture;

@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -59,11 +61,14 @@ void create_room_no_password_success() {
routines.add("물 마시기");
routines.add("코테 풀기");

Member member = spy(MemberFixture.member());

CreateRoomRequest createRoomRequest = new CreateRoomRequest(
"재윤과 앵맹이의 방임", null, routines, MORNING, 10, 4);

Room expectedRoom = RoomMapper.toRoomEntity(createRoomRequest);
given(roomRepository.save(any(Room.class))).willReturn(expectedRoom);
given(memberService.getById(1L)).willReturn(member);

// when
Long result = roomService.createRoom(1L, createRoomRequest);
Expand All @@ -84,11 +89,14 @@ void create_room_with_password_success() {
routines.add("물 마시기");
routines.add("코테 풀기");

Member member = spy(MemberFixture.member());

CreateRoomRequest createRoomRequest = new CreateRoomRequest(
"재윤과 앵맹이의 방임", "1234", routines, MORNING, 10, 4);

Room expectedRoom = RoomMapper.toRoomEntity(createRoomRequest);
given(roomRepository.save(any(Room.class))).willReturn(expectedRoom);
given(memberService.getById(1L)).willReturn(member);

// when
Long result = roomService.createRoom(1L, createRoomRequest);
Expand Down Expand Up @@ -128,7 +136,7 @@ void room_manager_mandate_success() {
assertThat(memberParticipant.isManager()).isTrue();
}

@DisplayName("방장 위임 실패 - 방장이 아닌 유저가 요청할때")
@DisplayName("방장 위임 실패 - 방장이 아닌 유저가 요청할 때")
@Test
void room_manager_mandate_fail() {
// given
Expand Down
38 changes: 38 additions & 0 deletions src/test/java/com/moabam/api/presentation/RoomControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.moabam.api.domain.room.repository.DailyMemberCertificationRepository;
import com.moabam.api.domain.room.repository.DailyRoomCertificationRepository;
import com.moabam.api.domain.room.repository.ParticipantRepository;
import com.moabam.api.domain.room.repository.ParticipantSearchRepository;
import com.moabam.api.domain.room.repository.RoomRepository;
import com.moabam.api.domain.room.repository.RoutineRepository;
import com.moabam.api.dto.room.CreateRoomRequest;
Expand All @@ -46,6 +47,7 @@
import com.moabam.support.common.WithoutFilterSupporter;
import com.moabam.support.fixture.BugFixture;
import com.moabam.support.fixture.MemberFixture;
import com.moabam.support.fixture.RoomFixture;

@Transactional
@SpringBootTest
Expand Down Expand Up @@ -80,6 +82,9 @@ class RoomControllerTest extends WithoutFilterSupporter {
@Autowired
private DailyRoomCertificationRepository dailyRoomCertificationRepository;

@Autowired
private ParticipantSearchRepository participantSearchRepository;

Member member;

@BeforeAll
Expand Down Expand Up @@ -143,6 +148,7 @@ void create_room_with_password_success(String password) throws Exception {
.content(json))
.andExpect(status().isCreated())
.andDo(print());

assertThat(roomRepository.findAll()).hasSize(1);
assertThat(roomRepository.findAll().get(0).getTitle()).isEqualTo("비번 있는 재맹의 방임");
assertThat(roomRepository.findAll().get(0).getPassword()).isEqualTo(password);
Expand Down Expand Up @@ -839,4 +845,36 @@ void get_room_details_test() throws Exception {
.andExpect(status().isOk())
.andDo(print());
}

@DisplayName("방 추방 성공")
@Test
void deport_member_success() throws Exception {
// given
Room room = RoomFixture.room();
Member member = MemberFixture.member(1234L, "참여자");
memberRepository.save(member);

Participant memberParticipant = RoomFixture.participant(room, member.getId());
Participant managerParticipant = RoomFixture.participant(room, 1L);
managerParticipant.enableManager();

room.increaseCurrentUserCount();

roomRepository.save(room);
participantRepository.save(memberParticipant);
participantRepository.save(managerParticipant);

// expected
mockMvc.perform(delete("/rooms/" + room.getId() + "/members/" + member.getId()))
.andExpect(status().isOk())
.andDo(print());
roomRepository.flush();

Room getRoom = roomRepository.findById(room.getId()).orElseThrow();
Participant getMemberParticipant = participantRepository.findById(memberParticipant.getId()).orElseThrow();

assertThat(getRoom.getCurrentUserCount()).isEqualTo(1);
assertThat(getMemberParticipant.getDeletedAt()).isNotNull();
assertThat(participantSearchRepository.findOne(3L, room.getId())).isEmpty();
}
}

0 comments on commit 261f698

Please sign in to comment.