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

[REFACTOR]MemberService와 GhostService분리작업 완료 #126

Merged
merged 1 commit into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.dontbe.www.DontBeServer.api.ghost.controller;

import com.dontbe.www.DontBeServer.api.ghost.service.GhostCommandService;
import com.dontbe.www.DontBeServer.api.member.dto.request.MemberClickGhostRequestDto;
import com.dontbe.www.DontBeServer.common.response.ApiResponse;
import com.dontbe.www.DontBeServer.common.util.MemberUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
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.RestController;

import java.security.Principal;

import static com.dontbe.www.DontBeServer.common.response.SuccessStatus.CLICK_MEMBER_GHOST_SUCCESS;

@RestController
@RequestMapping("/api/v1/")
@RequiredArgsConstructor
@SecurityRequirement(name = "JWT Auth")
@Tag(name="투명도 관련",description = "Ghost Api Document")
public class GhostController {
private final GhostCommandService ghostCommandServiceService;

@PostMapping("ghost")
@Operation(summary = "투명도 낮추는 API입니다.",description = "MemberGhost")
public ResponseEntity<ApiResponse<Object>> clickMemberGhost(Principal principal, @RequestBody MemberClickGhostRequestDto memberClickGhostRequestDto) {
Long memberId = MemberUtil.getMemberId(principal);
ghostCommandServiceService.clickMemberGhost(memberId, memberClickGhostRequestDto);
return ApiResponse.success(CLICK_MEMBER_GHOST_SUCCESS);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,75 @@
package com.dontbe.www.DontBeServer.api.ghost.service;

import com.dontbe.www.DontBeServer.api.ghost.domain.Ghost;
import com.dontbe.www.DontBeServer.api.ghost.repository.GhostRepository;
import com.dontbe.www.DontBeServer.api.member.domain.Member;
import com.dontbe.www.DontBeServer.api.member.dto.request.MemberClickGhostRequestDto;
import com.dontbe.www.DontBeServer.api.member.repository.MemberRepository;
import com.dontbe.www.DontBeServer.api.notification.domain.Notification;
import com.dontbe.www.DontBeServer.api.notification.repository.NotificationRepository;
import com.dontbe.www.DontBeServer.common.exception.BadRequestException;
import com.dontbe.www.DontBeServer.common.response.ErrorStatus;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

@Service
@RequiredArgsConstructor
@Transactional
public class GhostCommandService {
// 생성/ 수정/ 삭제
private final MemberRepository memberRepository;
private final GhostRepository ghostRepository;
private final NotificationRepository notificationRepository;
public void clickMemberGhost(Long memberId, MemberClickGhostRequestDto memberClickGhostRequestDto) {
Member triggerMember = memberRepository.findMemberByIdOrThrow(memberId);
Member targetMember = memberRepository.findMemberByIdOrThrow(memberClickGhostRequestDto.targetMemberId());

mySelfGhostBlock(triggerMember,targetMember);
isDuplicateMemberGhost(triggerMember,targetMember);

Ghost ghost = Ghost.builder()
.ghostTargetMember(targetMember)
.ghostTriggerMember(triggerMember)
.build();
Ghost savedGhost = ghostRepository.save(ghost);

Notification notification = Notification.builder()
.notificationTargetMember(targetMember)
.notificationTriggerMemberId(memberId)
.notificationTriggerType(memberClickGhostRequestDto.alarmTriggerType())
.notificationTriggerId(memberClickGhostRequestDto.alarmTriggerId()) //2차 스프린트 : 에러수정을 위한 notificationTriggerId에 답글id 저장, 알림 조회시 답글id로 게시글id 반환하도록하기(refineNotificationTriggerId에 추가)
.isNotificationChecked(false)
.notificationText("")
.build();
Notification savedNotification = notificationRepository.save(notification);

targetMember.decreaseGhost();

if(targetMember.getMemberGhost() == -85) {
Notification ghostNotification = Notification.builder()
.notificationTargetMember(targetMember)
.notificationTriggerMemberId(memberId)
.notificationTriggerType("beGhost")
.notificationTriggerId(memberClickGhostRequestDto.alarmTriggerId())
.isNotificationChecked(false)
.notificationText("")
.build();
Notification savedGhostNotification = notificationRepository.save(ghostNotification);
}
}

private void isDuplicateMemberGhost(Member triggerMember, Member targetMember) {
if(ghostRepository.existsByGhostTargetMemberAndGhostTriggerMember(targetMember,triggerMember)) {
throw new BadRequestException(ErrorStatus.DUPLICATION_MEMBER_GHOST.getMessage());
}
}

private void mySelfGhostBlock(Member triggerMember, Member targetMember) {
if(Objects.equals(triggerMember.getId(), targetMember.getId())) {
throw new BadRequestException(ErrorStatus.GHOST_MYSELF_BLOCK.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package com.dontbe.www.DontBeServer.api.member.controller;

import com.dontbe.www.DontBeServer.api.member.dto.request.MemberClickGhostRequestDto;
import com.dontbe.www.DontBeServer.api.member.dto.request.MemberNicknameCheckRequestDto;
import com.dontbe.www.DontBeServer.api.member.dto.request.MemberProfilePatchRequestDto;
import com.dontbe.www.DontBeServer.api.member.dto.response.MemberDetailGetResponseDto;
import com.dontbe.www.DontBeServer.api.member.dto.response.MemberGetProfileResponseDto;
import com.dontbe.www.DontBeServer.api.member.service.MemberService;
import com.dontbe.www.DontBeServer.api.member.service.MemberCommandService;
import com.dontbe.www.DontBeServer.api.member.service.MemberQueryService;
import com.dontbe.www.DontBeServer.common.response.ApiResponse;
import com.dontbe.www.DontBeServer.common.util.MemberUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -26,50 +24,43 @@
@SecurityRequirement(name = "JWT Auth")
@Tag(name="멤버 관련",description = "Member Api Document")
public class MemberController {
private final MemberService memberService;
private final MemberCommandService memberCommandService;
private final MemberQueryService memberQueryService;

@DeleteMapping("withdrawal")
@Operation(summary = "유저 탈퇴 API입니다. 앱잼 기간 미사용",description = "MemberWithdrawal")
public ResponseEntity<ApiResponse<Object>> withdrawalMember(Principal principal)
{
Long memberId = MemberUtil.getMemberId(principal);
memberService.withdrawalMember(memberId);
memberCommandService.withdrawalMember(memberId);
return ApiResponse.success(WITHDRAWAL_SUCCESS);
}

@GetMapping("member-data")
@Operation(summary = "유저 상세 정보 조회 API입니다.",description = "MemberData")
public ResponseEntity<ApiResponse<MemberDetailGetResponseDto>> getMemberDetail(Principal principal){
Long memberId = MemberUtil.getMemberId(principal);
return ApiResponse.success(GET_MEMBER_DETAIL, memberService.getMemberDetail(memberId));
return ApiResponse.success(GET_MEMBER_DETAIL, memberQueryService.getMemberDetail(memberId));
}

@GetMapping("viewmember/{viewmemberId}")
@Operation(summary = "유저 프로필 정보 조회 API입니다.",description = "MemberProfile")
public ResponseEntity<ApiResponse<MemberGetProfileResponseDto>> getMemberProfile(Principal principal,@PathVariable(name = "viewmemberId") Long viewmemberId) {
return ApiResponse.success(GET_PROFILE_SUCCESS, memberService.getMemberProfile(viewmemberId));
}

@PostMapping("ghost")
@Operation(summary = "투명도 낮추는 API입니다.",description = "MemberGhost")
public ResponseEntity<ApiResponse<Object>> clickMemberGhost(Principal principal,@RequestBody MemberClickGhostRequestDto memberClickGhostRequestDto) {
Long memberId = MemberUtil.getMemberId(principal);
memberService.clickMemberGhost(memberId, memberClickGhostRequestDto);
return ApiResponse.success(CLICK_MEMBER_GHOST_SUCCESS);
return ApiResponse.success(GET_PROFILE_SUCCESS, memberQueryService.getMemberProfile(viewmemberId));
}

@PatchMapping("user-profile")
@Operation(summary = "유저 프로필 수정 API입니다.",description = "UserProfilePatch")
public ResponseEntity<ApiResponse<Object>> updateMemberProfile(Principal principal, @RequestBody MemberProfilePatchRequestDto memberProfilePatchRequestDto) {
Long memberId = MemberUtil.getMemberId(principal);
memberService.updateMemberProfile(memberId, memberProfilePatchRequestDto);
memberCommandService.updateMemberProfile(memberId, memberProfilePatchRequestDto);
return ApiResponse.success(PATCH_MEMBER_PROFILE);
}

@GetMapping("nickname-validation")
@Operation(summary = "유저 닉네임 사용 가능 확인 API 입니다.",description = "NicknameValidation")
public ResponseEntity<ApiResponse<Object>> checkMemberNickname(Principal principal,@RequestParam(value = "nickname") String nickname) {
memberService.checkNicknameValidate(nickname);
memberQueryService.checkNicknameValidate(nickname);
return ApiResponse.success(NICKNAME_CHECK_SUCCESS);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.dontbe.www.DontBeServer.api.member.service;

import com.dontbe.www.DontBeServer.api.member.domain.Member;
import com.dontbe.www.DontBeServer.api.member.dto.request.MemberProfilePatchRequestDto;
import com.dontbe.www.DontBeServer.api.member.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional
public class MemberCommandService {
private final MemberRepository memberRepository;

public void withdrawalMember(Long memberId) {
Member member = memberRepository.findMemberByIdOrThrow(memberId);
memberRepository.delete(member);
}

public void updateMemberProfile(Long memberId, MemberProfilePatchRequestDto memberProfilePatchRequestDto) {
Member existingMember = memberRepository.findMemberByIdOrThrow(memberId);

// 업데이트할 속성만 복사
if (memberProfilePatchRequestDto.nickname() != null) {
existingMember.updateNickname(memberProfilePatchRequestDto.nickname());
}
if (memberProfilePatchRequestDto.member_intro() != null) {
existingMember.updateMemberIntro(memberProfilePatchRequestDto.member_intro());
}
if (memberProfilePatchRequestDto.profile_url() != null) {
existingMember.updateProfileUrl(memberProfilePatchRequestDto.profile_url());
}
if (memberProfilePatchRequestDto.is_alarm_allowed() | !memberProfilePatchRequestDto.is_alarm_allowed()) {
existingMember.updateMemberIsAlarmAllowed(memberProfilePatchRequestDto.is_alarm_allowed());
}

// 저장
Member savedMember = memberRepository.save(existingMember);
}
}
Loading
Loading