Skip to content

Commit

Permalink
merge develop into
Browse files Browse the repository at this point in the history
  • Loading branch information
hongdosan committed Nov 29, 2023
2 parents f41285a + 458e67b commit 1bfb7c8
Show file tree
Hide file tree
Showing 24 changed files with 156 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ public void purchaseItem(Long memberId, Long itemId, PurchaseItemRequest request

@Transactional
public void selectItem(Long memberId, Long itemId) {
Member member = memberService.findMember(memberId);
Inventory inventory = getInventory(memberId, itemId);

inventorySearchRepository.findDefault(memberId, inventory.getItemType())
.ifPresent(Inventory::deselect);
inventory.select();
inventory.select(member);
}

private Item getItem(Long itemId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import com.moabam.api.domain.member.Member;
import com.moabam.api.domain.member.repository.MemberRepository;
import com.moabam.api.domain.member.repository.MemberSearchRepository;
import com.moabam.api.domain.room.Participant;
import com.moabam.api.domain.room.repository.ParticipantRepository;
import com.moabam.api.domain.room.repository.ParticipantSearchRepository;
import com.moabam.api.dto.auth.AuthorizationTokenInfoResponse;
import com.moabam.api.dto.auth.LoginResponse;
import com.moabam.api.dto.member.MemberInfo;
Expand All @@ -42,6 +45,8 @@ public class MemberService {
private final InventoryRepository inventoryRepository;
private final ItemRepository itemRepository;
private final MemberSearchRepository memberSearchRepository;
private final ParticipantSearchRepository participantSearchRepository;
private final ParticipantRepository participantRepository;
private final ClockHolder clockHolder;

public Member findMember(Long memberId) {
Expand Down Expand Up @@ -69,6 +74,12 @@ public Member findMemberToDelete(Long memberId) {

@Transactional
public void delete(Member member) {
List<Participant> participants = participantRepository.findAllByMemberId(member.getId());

if (!participants.isEmpty()) {
throw new BadRequestException(NEED_TO_EXIT_ALL_ROOMS);
}

member.delete(clockHolder.times());
memberRepository.flush();
memberRepository.delete(member);
Expand All @@ -92,14 +103,29 @@ public void modifyInfo(AuthMember authMember, ModifyMemberRequest modifyMemberRe
Member member = memberSearchRepository.findMember(authMember.id())
.orElseThrow(() -> new NotFoundException(MEMBER_NOT_FOUND));

member.changeNickName(modifyMemberRequest.nickname());
boolean nickNameChanged = member.changeNickName(modifyMemberRequest.nickname());
member.changeIntro(modifyMemberRequest.intro());
member.changeProfileUri(newProfileUri);

memberRepository.save(member);

if (nickNameChanged) {
changeNickname(authMember.id(), modifyMemberRequest.nickname());
}
}

private void changeNickname(Long memberId, String changedName) {
List<Participant> participants = participantSearchRepository.findAllRoomMangerByMemberId(memberId);

for (Participant participant : participants) {
participant.getRoom().changeManagerNickname(changedName);
}
}

private void validateNickname(String nickname) {
if (Objects.isNull(nickname)) {
return;
}
if (StringUtils.isEmpty(nickname) && memberRepository.existsByNickname(nickname)) {
throw new ConflictException(NICKNAME_CONFLICT);
}
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/com/moabam/api/application/report/ReportService.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,30 @@ public class ReportService {

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

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

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

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

Room room = roomService.findRoom(reportRequest.roomId());
Room room = null;
if (nonNull(reportRequest.roomId())) {
room = roomService.findRoom(reportRequest.roomId());
}

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

private void validateNoReportSubject(Long roomId, Long certificationId) {
if (isNull(roomId) && isNull(certificationId)) {
private void validateNoReportSubject(Long reportedId) {
if (isNull(reportedId)) {
throw new BadRequestException(ErrorMessage.REPORT_REQUEST_ERROR);
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/moabam/api/application/room/RoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class RoomService {
private final MemberService memberService;

@Transactional
public Long createRoom(Long memberId, String nickname, CreateRoomRequest createRoomRequest) {
public Long createRoom(Long memberId, CreateRoomRequest createRoomRequest) {
Room room = RoomMapper.toRoomEntity(createRoomRequest);
List<Routine> routines = RoutineMapper.toRoutineEntities(room, createRoomRequest.routines());
Participant participant = ParticipantMapper.toParticipant(room, memberId);
Expand All @@ -55,7 +55,7 @@ public Long createRoom(Long memberId, String nickname, CreateRoomRequest createR
Member member = memberService.findMember(memberId);
member.enterRoom(room.getRoomType());
participant.enableManager();
room.changeManagerNickname(nickname);
room.changeManagerNickname(member.getNickname());

Room savedRoom = roomRepository.save(room);
routineRepository.saveAll(routines);
Expand Down Expand Up @@ -135,6 +135,7 @@ public void mandateManager(Long managerId, Long roomId, Long memberId) {

@Transactional
public void deportParticipant(Long managerId, Long roomId, Long memberId) {
validateDeportParticipant(managerId, memberId);
Participant managerParticipant = getParticipant(managerId, roomId);
Participant memberParticipant = getParticipant(memberId, roomId);
validateManagerAuthorization(managerParticipant);
Expand Down Expand Up @@ -172,6 +173,12 @@ private Participant getParticipant(Long memberId, Long roomId) {
.orElseThrow(() -> new NotFoundException(PARTICIPANT_NOT_FOUND));
}

private void validateDeportParticipant(Long managerId, Long memberId) {
if (managerId.equals(memberId)) {
throw new BadRequestException(PARTICIPANT_DEPORT_ERROR);
}
}

private void validateManagerAuthorization(Participant participant) {
if (!participant.isManager()) {
throw new ForbiddenException(ROOM_MODIFY_UNAUTHORIZED_REQUEST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public ManageRoomResponse getRoomForModification(Long memberId, Long roomId) {
participantResponses.add(ParticipantMapper.toParticipantResponse(member, contributionPoint));
}

return RoomMapper.toManageRoomResponse(room, routineResponses, participantResponses);
return RoomMapper.toManageRoomResponse(room, memberId, routineResponses, participantResponses);
}

public GetAllRoomsResponse getAllRooms(@Nullable RoomType roomType, @Nullable Long roomId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,12 @@ public static RoomsHistoryResponse toRoomsHistoryResponse(List<RoomHistoryRespon
.build();
}

public static ManageRoomResponse toManageRoomResponse(Room room, List<RoutineResponse> routines,
public static ManageRoomResponse toManageRoomResponse(Room room, Long managerId, List<RoutineResponse> routines,
List<ParticipantResponse> participantResponses) {
return ManageRoomResponse.builder()
.roomId(room.getId())
.title(room.getTitle())
.managerId(managerId)
.announcement(room.getAnnouncement())
.roomType(room.getRoomType())
.certifyTime(room.getCertifyTime())
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/moabam/api/domain/item/Inventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.hibernate.annotations.ColumnDefault;

import com.moabam.api.domain.member.Member;
import com.moabam.global.common.entity.BaseTimeEntity;

import jakarta.persistence.Column;
Expand Down Expand Up @@ -54,8 +55,9 @@ public ItemType getItemType() {
return this.item.getType();
}

public void select() {
public void select(Member member) {
this.isDefault = true;
member.changeDefaultSkintUrl(this.item);
}

public void deselect() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/moabam/api/domain/member/BadgeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public enum BadgeType {
public static List<BadgeResponse> memberBadgeMap(Set<BadgeType> badgeTypes) {
return Arrays.stream(BadgeType.values())
.map(badgeType -> BadgeResponse.builder()
.badge(badgeType)
.badge(badgeType.korean)
.unlock(badgeTypes.contains(badgeType))
.build())
.toList();
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/moabam/api/domain/member/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static java.util.Objects.*;

import java.time.LocalDateTime;
import java.util.Objects;

import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.SQLDelete;
Expand Down Expand Up @@ -47,7 +48,7 @@ public class Member extends BaseTimeEntity {
@Column(name = "social_id", nullable = false, unique = true)
private String socialId;

@Column(name = "nickname", nullable = false, unique = true)
@Column(name = "nickname", unique = true)
private String nickname;

@Column(name = "intro", length = 30)
Expand Down Expand Up @@ -134,10 +135,15 @@ public void increaseTotalCertifyCount() {

public void delete(LocalDateTime now) {
socialId = deleteSocialId(now);
nickname = null;
}

public void changeNickName(String nickname) {
this.nickname = requireNonNullElse(nickname, this.nickname);
public boolean changeNickName(String nickname) {
if (Objects.isNull(nickname)) {
return false;
}
this.nickname = nickname;
return true;
}

public void changeIntro(String intro) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.moabam.api.domain.room.repository;

import java.util.List;

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

import com.moabam.api.domain.room.Participant;

public interface ParticipantRepository extends JpaRepository<Participant, Long> {

List<Participant> findAllByMemberId(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public List<Participant> findAllParticipantsByMemberId(Long memberId) {
.where(
participant.memberId.eq(memberId)
)
.orderBy(participant.createdAt.desc())
.fetch();
}

Expand All @@ -75,4 +76,15 @@ public List<Participant> findAllByRoomCertifyTime(int certifyTime) {
)
.fetch();
}

public List<Participant> findAllRoomMangerByMemberId(Long memberId) {
return jpaQueryFactory
.selectFrom(participant)
.join(participant.room, room).fetchJoin()
.where(
participant.memberId.eq(memberId),
participant.isManager.isTrue()
)
.fetch();
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/moabam/api/dto/member/BadgeResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

@Builder
public record BadgeResponse(
BadgeType badge,
String badge,
boolean unlock
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public record ManageRoomResponse(
Long roomId,
String title,
Long managerId,
String announcement,
RoomType roomType,
int certifyTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class RoomController {
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Long createRoom(@Auth AuthMember authMember, @Valid @RequestBody CreateRoomRequest createRoomRequest) {
return roomService.createRoom(authMember.id(), authMember.nickname(), createRoomRequest);
return roomService.createRoom(authMember.id(), createRoomRequest);
}

@GetMapping
Expand Down
2 changes: 2 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 @@ -29,6 +29,8 @@ public enum ErrorMessage {
INVALID_REQUEST_URL("잘못된 URL 요청입니다."),
INVALID_CERTIFY_TIME("현재 인증 시간이 아닙니다."),
CERTIFICATION_NOT_FOUND("인증 정보가 없습니다."),
NEED_TO_EXIT_ALL_ROOMS("모든 방에서 나가야 회원 탈퇴가 가능합니다."),
PARTICIPANT_DEPORT_ERROR("방장은 자신을 추방할 수 없습니다."),

LOGIN_FAILED("로그인에 실패했습니다."),
REQUEST_FAILED("네트워크 접근 실패입니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ void success() {
Inventory inventory = inventory(memberId, nightMageSkin());
Inventory defaultInventory = inventory(memberId, nightMageSkin());
ItemType itemType = inventory.getItemType();
given(memberService.findMember(memberId)).willReturn(member());
given(inventorySearchRepository.findOne(memberId, itemId)).willReturn(Optional.of(inventory));
given(inventorySearchRepository.findDefault(memberId, itemType)).willReturn(Optional.of(defaultInventory));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.moabam.api.domain.member.Member;
import com.moabam.api.domain.member.repository.MemberRepository;
import com.moabam.api.domain.member.repository.MemberSearchRepository;
import com.moabam.api.domain.room.repository.ParticipantRepository;
import com.moabam.api.domain.room.repository.ParticipantSearchRepository;
import com.moabam.api.dto.auth.AuthorizationTokenInfoResponse;
import com.moabam.api.dto.auth.LoginResponse;
import com.moabam.api.dto.member.MemberInfo;
Expand Down Expand Up @@ -54,6 +56,12 @@ class MemberServiceTest {
@Mock
MemberSearchRepository memberSearchRepository;

@Mock
ParticipantRepository participantRepository;

@Mock
ParticipantSearchRepository participantSearchRepository;

@Mock
InventorySearchRepository inventorySearchRepository;

Expand Down Expand Up @@ -204,6 +212,8 @@ void modify_success_test(@WithMember AuthMember authMember) {
Member member = MemberFixture.member();
ModifyMemberRequest modifyMemberRequest = ModifyImageFixture.modifyMemberRequest();
given(memberSearchRepository.findMember(authMember.id())).willReturn(Optional.ofNullable(member));
given(participantSearchRepository.findAllRoomMangerByMemberId(any()))
.willReturn(List.of());

// when
memberService.modifyInfo(authMember, modifyMemberRequest, "/main");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ReportServiceTest {
@Test
void no_report_subject_fail(@WithMember AuthMember authMember) {
// given
ReportRequest reportRequest = new ReportRequest(5L, null, null, "st");
ReportRequest reportRequest = new ReportRequest(null, null, null, "st");

// When + Then
assertThatThrownBy(() -> reportService.report(authMember, reportRequest))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void create_room_no_password_success() {
given(memberService.findMember(1L)).willReturn(member);

// when
Long result = roomService.createRoom(1L, "닉네임", createRoomRequest);
Long result = roomService.createRoom(1L, createRoomRequest);

// then
verify(roomRepository).save(any(Room.class));
Expand Down Expand Up @@ -98,7 +98,7 @@ void create_room_with_password_success() {
given(memberService.findMember(1L)).willReturn(member);

// when
Long result = roomService.createRoom(1L, "닉네임", createRoomRequest);
Long result = roomService.createRoom(1L, createRoomRequest);

// then
verify(roomRepository).save(any(Room.class));
Expand Down
Loading

0 comments on commit 1bfb7c8

Please sign in to comment.