Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 방/회원/인증 신고 기능 추가 #158

Merged
merged 9 commits into from
Nov 27, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class BugService {
private final PaymentRepository paymentRepository;

public BugResponse getBug(Long memberId) {
Member member = memberService.getById(memberId);
Member member = memberService.findMember(memberId);

return BugMapper.toBugResponse(member.getBug());
}
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 @@ -38,8 +38,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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: report, createReport는 어떤 기능을 하는 서비스인가요?

validateNoReportSubject(reportRequest.roomId(), reportRequest.certificationId());
Report report = createReport(authMember.id(), reportRequest);
reportRepository.save(report);
}

private Report createReport(Long reporterId, ReportRequest reportRequest) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

R: 네이밍 수정쫌... create에 찌들어버렸어 ,,

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
@@ -1,9 +1,6 @@
package com.moabam.api.application.room;

import static com.moabam.global.error.model.ErrorMessage.DUPLICATED_DAILY_MEMBER_CERTIFICATION;
import static com.moabam.global.error.model.ErrorMessage.INVALID_CERTIFY_TIME;
import static com.moabam.global.error.model.ErrorMessage.PARTICIPANT_NOT_FOUND;
import static com.moabam.global.error.model.ErrorMessage.ROUTINE_NOT_FOUND;
import static com.moabam.global.error.model.ErrorMessage.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -61,7 +58,7 @@ public void certifyRoom(Long memberId, Long roomId, List<String> imageUrls) {
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 @@ -93,6 +90,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
28 changes: 13 additions & 15 deletions src/main/java/com/moabam/api/application/room/RoomService.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
package com.moabam.api.application.room;

import static com.moabam.api.domain.room.RoomType.MORNING;
import static com.moabam.api.domain.room.RoomType.NIGHT;
import static com.moabam.global.error.model.ErrorMessage.MEMBER_ROOM_EXCEED;
import static com.moabam.global.error.model.ErrorMessage.PARTICIPANT_NOT_FOUND;
import static com.moabam.global.error.model.ErrorMessage.ROOM_EXIT_MANAGER_FAIL;
import static com.moabam.global.error.model.ErrorMessage.ROOM_MAX_USER_REACHED;
import static com.moabam.global.error.model.ErrorMessage.ROOM_MODIFY_UNAUTHORIZED_REQUEST;
import static com.moabam.global.error.model.ErrorMessage.ROOM_NOT_FOUND;
import static com.moabam.global.error.model.ErrorMessage.WRONG_ROOM_PASSWORD;
import static com.moabam.api.domain.room.RoomType.*;
import static com.moabam.global.error.model.ErrorMessage.*;

import java.util.List;

Expand Down Expand Up @@ -57,7 +50,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 @@ -93,7 +86,7 @@ public void enterRoom(Long memberId, Long roomId, EnterRoomRequest enterRoomRequ
Room room = roomRepository.findById(roomId).orElseThrow(() -> 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 @@ -108,7 +101,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 @@ -130,7 +123,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 @@ -147,7 +140,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 @@ -157,6 +150,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 @@ -180,7 +178,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
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