Skip to content

Commit

Permalink
feat: 방/회원/인증 신고 기능 추가 (#158)
Browse files Browse the repository at this point in the history
* test: 삭제된 회원 조회 테스트 추가

* refactor: 회원 조회 변경

* feat: 신고 기능 추가 및 테스트 코드 추가

* refactor: 신고 기능 로직 수정 및 테스트 코드 추가

* feat: 신고 api 기능 추가 및 테스트 코드 추가

* fix: 통합 테스트간 데이터 중복 및 index 문제 해결

* refactor: CsvSource null 부분 변경
  • Loading branch information
parksey authored Nov 27, 2023
1 parent 6bba643 commit da36a04
Show file tree
Hide file tree
Showing 28 changed files with 623 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/moabam/api/application/bug/BugService.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class BugService {
private final PaymentRepository paymentRepository;

public BugResponse getBug(Long memberId) {
Bug bug = memberService.getById(memberId).getBug();
Bug bug = memberService.findMember(memberId).getBug();

return BugMapper.toBugResponse(bug);
}
Expand Down Expand Up @@ -77,7 +77,7 @@ public PurchaseProductResponse purchaseBugProduct(Long memberId, Long productId,

@Transactional
public void charge(Long memberId, Product bugProduct) {
Bug bug = memberService.getById(memberId).getBug();
Bug bug = memberService.findMember(memberId).getBug();

bug.charge(bugProduct.getQuantity());
bugHistoryRepository.save(BugMapper.toChargeBugHistory(memberId, bugProduct.getQuantity()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public ItemsResponse getItems(Long memberId, ItemType type) {
@Transactional
public void purchaseItem(Long memberId, Long itemId, PurchaseItemRequest request) {
Item item = getItem(itemId);
Member member = memberService.getById(memberId);
Member member = memberService.findMember(memberId);

validateAlreadyPurchased(memberId, itemId);
item.validatePurchasable(request.bugType(), member.getLevel());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public class MemberService {
private final MemberSearchRepository memberSearchRepository;
private final ClockHolder clockHolder;

public Member getById(Long memberId) {
return memberRepository.findById(memberId)
public Member findMember(Long memberId) {
return memberSearchRepository.findMember(memberId)
.orElseThrow(() -> new NotFoundException(MEMBER_NOT_FOUND));
}

Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/moabam/api/application/report/ReportMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.moabam.api.application.report;

import com.moabam.api.domain.report.Report;
import com.moabam.api.domain.room.Certification;
import com.moabam.api.domain.room.Room;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ReportMapper {

public static Report toReport(Long reporterId, Long reportedMemberId,
Room room, Certification certification, String description) {
return Report.builder()
.reporterId(reporterId)
.reportedMemberId(reportedMemberId)
.certification(certification)
.room(room)
.description(description)
.build();
}
}
60 changes: 60 additions & 0 deletions src/main/java/com/moabam/api/application/report/ReportService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.moabam.api.application.report;

import static java.util.Objects.*;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.moabam.api.application.member.MemberService;
import com.moabam.api.application.room.CertificationService;
import com.moabam.api.application.room.RoomService;
import com.moabam.api.domain.member.Member;
import com.moabam.api.domain.report.Report;
import com.moabam.api.domain.report.repository.ReportRepository;
import com.moabam.api.domain.room.Certification;
import com.moabam.api.domain.room.Room;
import com.moabam.api.dto.report.ReportRequest;
import com.moabam.global.auth.model.AuthMember;
import com.moabam.global.error.exception.BadRequestException;
import com.moabam.global.error.model.ErrorMessage;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class ReportService {

private final MemberService memberService;
private final RoomService roomService;
private final CertificationService certificationService;
private final ReportRepository reportRepository;

@Transactional
public void report(AuthMember authMember, ReportRequest reportRequest) {
validateNoReportSubject(reportRequest.roomId(), reportRequest.certificationId());
Report report = createReport(authMember.id(), reportRequest);
reportRepository.save(report);
}

private Report createReport(Long reporterId, ReportRequest reportRequest) {
Member reportedMember = memberService.findMember(reportRequest.reportedId());

if (nonNull(reportRequest.certificationId())) {
Certification certification = certificationService.findCertification(reportRequest.certificationId());

return ReportMapper.toReport(reporterId, reportedMember.getId(),
null, certification, reportRequest.description());
}

Room room = roomService.findRoom(reportRequest.roomId());

return ReportMapper.toReport(reporterId, reportedMember.getId(),
room, null, reportRequest.description());
}

private void validateNoReportSubject(Long roomId, Long certificationId) {
if (isNull(roomId) && isNull(certificationId)) {
throw new BadRequestException(ErrorMessage.REPORT_REQUEST_ERROR);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public CertifiedMemberInfo getCertifiedMemberInfo(Long memberId, Long roomId, Li
Participant participant = participantSearchRepository.findOne(memberId, roomId)
.orElseThrow(() -> new NotFoundException(PARTICIPANT_NOT_FOUND));
Room room = participant.getRoom();
Member member = memberService.getById(memberId);
Member member = memberService.findMember(memberId);
BugType bugType = switch (room.getRoomType()) {
case MORNING -> BugType.MORNING;
case NIGHT -> BugType.NIGHT;
Expand Down Expand Up @@ -100,6 +100,11 @@ public boolean existsRoomCertification(Long roomId, LocalDate date) {
return dailyRoomCertificationRepository.existsByRoomIdAndCertifiedAt(roomId, date);
}

public Certification findCertification(Long certificationId) {
return certificationRepository.findById(certificationId)
.orElseThrow(() -> new NotFoundException(CERTIFICATION_NOT_FOUND));
}

private void validateCertifyTime(LocalDateTime now, int certifyTime) {
LocalTime targetTime = LocalTime.of(certifyTime, 0);
LocalDateTime minusTenMinutes = LocalDateTime.of(now.toLocalDate(), targetTime).minusMinutes(10);
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/com/moabam/api/application/room/RoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Long createRoom(Long memberId, String nickname, CreateRoomRequest createR

validateEnteredRoomCount(memberId, room.getRoomType());

Member member = memberService.getById(memberId);
Member member = memberService.findMember(memberId);
member.enterRoom(room.getRoomType());
participant.enableManager();
room.changeManagerNickname(nickname);
Expand Down Expand Up @@ -89,7 +89,7 @@ public void enterRoom(Long memberId, Long roomId, EnterRoomRequest enterRoomRequ
() -> new NotFoundException(ROOM_NOT_FOUND));
validateRoomEnter(memberId, enterRoomRequest.password(), room);

Member member = memberService.getById(memberId);
Member member = memberService.findMember(memberId);
member.enterRoom(room.getRoomType());
room.increaseCurrentUserCount();

Expand All @@ -104,7 +104,7 @@ public void exitRoom(Long memberId, Long roomId) {

validateRoomExit(participant, room);

Member member = memberService.getById(memberId);
Member member = memberService.findMember(memberId);
member.exitRoom(room.getRoomType());

participant.removeRoom();
Expand All @@ -126,7 +126,7 @@ public void mandateManager(Long managerId, Long roomId, Long memberId) {
validateManagerAuthorization(managerParticipant);

Room room = managerParticipant.getRoom();
Member member = memberService.getById(memberParticipant.getMemberId());
Member member = memberService.findMember(memberParticipant.getMemberId());
room.changeManagerNickname(member.getNickname());

managerParticipant.disableManager();
Expand All @@ -143,7 +143,7 @@ public void deportParticipant(Long managerId, Long roomId, Long memberId) {
participantRepository.delete(memberParticipant);
room.decreaseCurrentUserCount();

Member member = memberService.getById(memberId);
Member member = memberService.findMember(memberId);
member.exitRoom(room.getRoomType());
}

Expand All @@ -153,6 +153,11 @@ public void validateRoomById(Long roomId) {
}
}

public Room findRoom(Long roomId) {
return roomRepository.findById(roomId)
.orElseThrow(() -> new NotFoundException(ROOM_NOT_FOUND));
}

private Participant getParticipant(Long memberId, Long roomId) {
return participantSearchRepository.findOne(memberId, roomId)
.orElseThrow(() -> new NotFoundException(PARTICIPANT_NOT_FOUND));
Expand All @@ -176,7 +181,7 @@ private void validateRoomEnter(Long memberId, String requestPassword, Room room)
}

private void validateEnteredRoomCount(Long memberId, RoomType roomType) {
Member member = memberService.getById(memberId);
Member member = memberService.findMember(memberId);

if (roomType.equals(MORNING) && member.getCurrentMorningCount() >= 3) {
throw new BadRequestException(MEMBER_ROOM_EXCEED);
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/com/moabam/api/domain/report/Report.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.moabam.api.domain.report;

import static java.util.Objects.*;

import com.moabam.api.domain.room.Certification;
import com.moabam.api.domain.room.Room;
import com.moabam.global.common.entity.BaseTimeEntity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "report")
@Entity
public class Report extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "reporter_id", nullable = false, updatable = false)
private Long reporterId;

@Column(name = "reported_member_id", nullable = false, updatable = false)
private Long reportedMemberId;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "room_id", updatable = false)
private Room room;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "certification_id", updatable = false)
private Certification certification;

@Column(name = "description")
private String description;

@Builder
private Report(Long reporterId, Long reportedMemberId, Room room, Certification certification, String description) {
this.reporterId = requireNonNull(reporterId);
this.reportedMemberId = requireNonNull(reportedMemberId);
this.room = room;
this.certification = certification;
this.description = description;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.moabam.api.domain.report.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.moabam.api.domain.report.Report;

public interface ReportRepository extends JpaRepository<Report, Long> {

}
12 changes: 12 additions & 0 deletions src/main/java/com/moabam/api/dto/report/ReportRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.moabam.api.dto.report;

import jakarta.validation.constraints.NotNull;

public record ReportRequest(
@NotNull Long reportedId,
Long roomId,
Long certificationId,
String description
) {

}
30 changes: 30 additions & 0 deletions src/main/java/com/moabam/api/presentation/ReportController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.moabam.api.presentation;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.moabam.api.application.report.ReportService;
import com.moabam.api.dto.report.ReportRequest;
import com.moabam.global.auth.annotation.Auth;
import com.moabam.global.auth.model.AuthMember;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("/reports")
@RequiredArgsConstructor
public class ReportController {

private final ReportService reportService;

@PostMapping
@ResponseStatus(HttpStatus.OK)
public void reports(@Auth AuthMember authMember, @Valid @RequestBody ReportRequest reportRequest) {
reportService.report(authMember, reportRequest);
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/moabam/global/error/model/ErrorMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public enum ErrorMessage {
NOT_FOUND_AVAILABLE_PORT("사용 가능한 포트를 찾을 수 없습니다. (10000 ~ 65535)"),
ERROR_EXECUTING_EMBEDDED_REDIS("Embedded Redis 실행 중 오류가 발생했습니다."),

REPORT_REQUEST_ERROR("신고 요청하고자 하는 방이나 대상이 존재하지 않습니다."),

ROOM_NOT_FOUND("존재하지 않는 방 입니다."),
ROOM_MAX_USER_COUNT_MODIFY_FAIL("잘못된 최대 인원수 설정입니다."),
ROOM_MODIFY_UNAUTHORIZED_REQUEST("방장이 아닌 사용자는 방을 수정할 수 없습니다."),
Expand All @@ -26,6 +28,7 @@ public enum ErrorMessage {
ROUTINE_NOT_FOUND("루틴을 찾을 수 없습니다"),
INVALID_REQUEST_URL("잘못된 URL 요청입니다."),
INVALID_CERTIFY_TIME("현재 인증 시간이 아닙니다."),
CERTIFICATION_NOT_FOUND("인증 정보가 없습니다."),

LOGIN_FAILED("로그인에 실패했습니다."),
REQUEST_FAILED("네트워크 접근 실패입니다."),
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/config
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void get_bug_success() {
// given
Long memberId = 1L;
Member member = member();
given(memberService.getById(memberId)).willReturn(member);
given(memberService.findMember(memberId)).willReturn(member);

// when
BugResponse response = bugService.getBug(memberId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ void success() {
PurchaseItemRequest request = new PurchaseItemRequest(BugType.GOLDEN);
Member member = member();
Item item = nightMageSkin();
given(memberService.getById(memberId)).willReturn(member);
given(memberService.findMember(memberId)).willReturn(member);
given(itemRepository.findById(itemId)).willReturn(Optional.of(item));
given(inventorySearchRepository.findOne(memberId, itemId)).willReturn(Optional.empty());

// When
itemService.purchaseItem(memberId, itemId, request);

// Then
verify(memberService).getById(memberId);
verify(memberService).findMember(memberId);
verify(itemRepository).findById(itemId);
verify(inventorySearchRepository).findOne(memberId, itemId);
verify(inventoryRepository).save(any(Inventory.class));
Expand Down
Loading

0 comments on commit da36a04

Please sign in to comment.