Skip to content

Commit

Permalink
style: #151 회원 등급 정책 로직 수정 중
Browse files Browse the repository at this point in the history
  • Loading branch information
hydrationn committed May 16, 2024
1 parent fa79797 commit 51744d3
Show file tree
Hide file tree
Showing 11 changed files with 306 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.t3t.bookstoreapi.member.controller;

import com.t3t.bookstoreapi.member.model.dto.MemberGradeDto;
import com.t3t.bookstoreapi.member.model.dto.MemberGradePolicyDto;
import com.t3t.bookstoreapi.member.model.entity.MemberGrade;
import com.t3t.bookstoreapi.member.model.request.CreateMemberGradePolicyRequest;
import com.t3t.bookstoreapi.member.model.request.MemberGradeCreationRequest;
import com.t3t.bookstoreapi.member.model.response.MemberGradePolicyResponse;
import com.t3t.bookstoreapi.member.service.MemberGradeService;
import com.t3t.bookstoreapi.model.response.BaseResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequiredArgsConstructor
public class MemberGradeController {
private final MemberGradeService memberGradeService;
@GetMapping("/admin/member-grades")
public ResponseEntity<BaseResponse<List<MemberGradeDto>>> getMemberGradeList() {
List<MemberGradeDto> memberGradePolicyList = memberGradeService.getMemberGradeList();
return ResponseEntity.ok(new BaseResponse<List<MemberGradeDto>>().data(memberGradePolicyList));
}

/**
* 특정 ID를 기반으로 한 회원 등급 정책 조회
* @param policyId 조회할 회원 등급 정책의 ID
* @return 조회된 회원 등급 정책을 포함한 BaseResponse 객체 반환
*
* @author hydrationn(박수화)
*/
@GetMapping("/admin/member-grade-policies/{policyId}")
public ResponseEntity<BaseResponse<MemberGradeDto>> getMemberGrade(@PathVariable("policyId") Long policyId) {
MemberGradeDto memberGrade = memberGradeService.getMemberGrade(policyId);
return ResponseEntity.ok(new BaseResponse<MemberGradeDto>().data(memberGrade));
}

/**
* 새로운 회원 등급 정책 생성
* @param request 생성할 회원 등급 정책의 정보를 담은 요청 객체
* @return 생성된 회원 등급 정책을 포함한 BaseResponse 객체 반환
*
* @author hydrationn(박수화)
*/
@PostMapping("/admin/member-grade-policy")
public ResponseEntity<BaseResponse<MemberGradeDto>> createMemberGrade(@RequestBody MemberGradeCreationRequest request) {
MemberGradeDto memberGrade = memberGradeService.createMemberGrade(request);
return ResponseEntity.ok(new BaseResponse<MemberGradeDto>().data(memberGrade));
}

/**
* 기존의 회원 등급 정책 업데이트
* @param policyId 업데이트할 회원 등급 정책의 ID
* @param startAmount 기준 시작 금액
* @param endAmount 기준 종료 금액
* @param rate 포인트 적립 비율
* @return 업데이트된 회원 등급 정책을 포함한 BaseResponse 객체 반환
*
* @author hydrationn(박수화)
*/
@PutMapping("/admin/member-grade-policy/{policyId}/default")
public ResponseEntity<BaseResponse<Void>> updateMemberGrade(@PathVariable("policyId") Long policyId,
@RequestParam("startAmount") BigDecimal startAmount,
@RequestParam("endAmount") BigDecimal endAmount,
@RequestParam("rate") int rate) {
memberGradeService.updateMemberGrade(policyId, startAmount, endAmount, rate);
return ResponseEntity.ok(new BaseResponse<Void>().message("회원 등급 정책 업데이트 요청이 정상적으로 처리되었습니다. "));
}

/**
* 특정 ID를 가진 회원 등급 정책 삭제
* @param policyId 삭제할 회원 등급 정책의 ID
* @return 삭제 성공 메시지를 포함한 BaseResponse 객체 반환
*
* @author hydrationn(박수화)
*/
@DeleteMapping("/admin/member-grade-policy/{policyId}")
public ResponseEntity<BaseResponse<Void>> deleteMemberGradePolicy(@PathVariable("policyId") Long policyId) {
memberGradeService.deleteMemberGrade(policyId);
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(new BaseResponse<Void>().message("회원 등급 정책 삭제 요청이 성공적으로 처리되었습니다. "));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.t3t.bookstoreapi.member.controller;

import com.t3t.bookstoreapi.member.model.dto.MemberGradePolicyDto;
import com.t3t.bookstoreapi.member.model.request.CreateMemberGradePolicyRequest;
import com.t3t.bookstoreapi.member.model.response.MemberGradePolicyResponse;
import com.t3t.bookstoreapi.member.service.MemberGradePolicyService;
import com.t3t.bookstoreapi.model.response.BaseResponse;
import lombok.RequiredArgsConstructor;
Expand All @@ -27,9 +27,9 @@ public class MemberGradePolicyController {
* @author hydrationn(박수화)
*/
@GetMapping("/admin/member-grade-policies")
public ResponseEntity<BaseResponse<List<MemberGradePolicyDto>>> getMemberGradePolicyList() {
List<MemberGradePolicyDto> memberGradePolicyDtoList = memberGradePolicyService.getMemberGradePolicyList();
return ResponseEntity.ok(new BaseResponse<List<MemberGradePolicyDto>>().data(memberGradePolicyDtoList));
public ResponseEntity<BaseResponse<List<MemberGradePolicyResponse>>> getMemberGradePolicyList() {
List<MemberGradePolicyResponse> memberGradePolicyList = memberGradePolicyService.getMemberGradePolicyList();
return ResponseEntity.ok(new BaseResponse<List<MemberGradePolicyResponse>>().data(memberGradePolicyList));
}

/**
Expand All @@ -40,9 +40,9 @@ public ResponseEntity<BaseResponse<List<MemberGradePolicyDto>>> getMemberGradePo
* @author hydrationn(박수화)
*/
@GetMapping("/admin/member-grade-policies/{policyId}")
public ResponseEntity<BaseResponse<MemberGradePolicyDto>> getMemberGradePolicy(@PathVariable("policyId") Long policyId) {
MemberGradePolicyDto policy = memberGradePolicyService.getMemberGradePolicy(policyId);
return ResponseEntity.ok(new BaseResponse<MemberGradePolicyDto>().data(policy));
public ResponseEntity<BaseResponse<MemberGradePolicyResponse>> getMemberGradePolicy(@PathVariable("policyId") Long policyId) {
MemberGradePolicyResponse policy = memberGradePolicyService.getMemberGradePolicy(policyId);
return ResponseEntity.ok(new BaseResponse<MemberGradePolicyResponse>().data(policy));
}

/**
Expand All @@ -53,9 +53,9 @@ public ResponseEntity<BaseResponse<MemberGradePolicyDto>> getMemberGradePolicy(@
* @author hydrationn(박수화)
*/
@PostMapping("/admin/member-grade-policy")
public ResponseEntity<BaseResponse<MemberGradePolicyDto>> createMemberGradePolicy(@RequestBody CreateMemberGradePolicyRequest request) {
MemberGradePolicyDto createdPolicy = memberGradePolicyService.createMemberGradePolicy(request);
return ResponseEntity.ok(new BaseResponse<MemberGradePolicyDto>().data(createdPolicy));
public ResponseEntity<BaseResponse<MemberGradePolicyResponse>> createMemberGradePolicy(@RequestBody CreateMemberGradePolicyRequest request) {
MemberGradePolicyResponse createdPolicy = memberGradePolicyService.createMemberGradePolicy(request);
return ResponseEntity.ok(new BaseResponse<MemberGradePolicyResponse>().data(createdPolicy));
}

/**
Expand All @@ -69,12 +69,12 @@ public ResponseEntity<BaseResponse<MemberGradePolicyDto>> createMemberGradePolic
* @author hydrationn(박수화)
*/
@PutMapping("/admin/member-grade-policy/{policyId}/default")
public ResponseEntity<BaseResponse<MemberGradePolicyDto>> updateMemberGradePolicy(@PathVariable("policyId") Long policyId,
@RequestParam("startAmount") BigDecimal startAmount,
@RequestParam("endAmount") BigDecimal endAmount,
@RequestParam("rate") int rate) {
MemberGradePolicyDto updatedPolicy = memberGradePolicyService.updateMemberGradePolicy(policyId, startAmount, endAmount, rate);
return ResponseEntity.ok(new BaseResponse<MemberGradePolicyDto>().data(updatedPolicy));
public ResponseEntity<BaseResponse<Void>> updateMemberGradePolicy(@PathVariable("policyId") Long policyId,
@RequestParam("startAmount") BigDecimal startAmount,
@RequestParam("endAmount") BigDecimal endAmount,
@RequestParam("rate") int rate) {
memberGradePolicyService.updateMemberGradePolicy(policyId, startAmount, endAmount, rate);
return ResponseEntity.ok(new BaseResponse<Void>().message("회원 등급 정책 업데이트 요청이 정상적으로 처리되었습니다. "));
}

/**
Expand All @@ -85,8 +85,8 @@ public ResponseEntity<BaseResponse<MemberGradePolicyDto>> updateMemberGradePolic
* @author hydrationn(박수화)
*/
@DeleteMapping("/admin/member-grade-policy/{policyId}")
public ResponseEntity<BaseResponse<Void>> deleteMemberGradePolicy(@PathVariable Long policyId) {
public ResponseEntity<BaseResponse<Void>> deleteMemberGradePolicy(@PathVariable("policyId") Long policyId) {
memberGradePolicyService.deleteMemberGradePolicy(policyId);
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(new BaseResponse<Void>().message("해당 회원 등급 정책이 성공적으로 삭제되었습니다."));
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(new BaseResponse<Void>().message("회원 등급 정책 삭제 요청이 성공적으로 처리되었습니다. "));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
*/
public class MemberGradeNotFoundException extends RuntimeException{
private static final String DEFAULT_MESSAGE = "존재하지 않는 회원 등급입니다.";
public MemberGradeNotFoundException() {
public MemberGradeNotFoundException(Long policyId) {
super(DEFAULT_MESSAGE);
}

protected MemberGradeNotFoundException(String forTarget) {
public MemberGradeNotFoundException(String forTarget) {
super(String.format("%s (%s)", DEFAULT_MESSAGE, forTarget));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.t3t.bookstoreapi.member.model.dto;

import com.t3t.bookstoreapi.member.model.entity.MemberGrade;
import com.t3t.bookstoreapi.member.model.entity.MemberGradePolicy;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class MemberGradeDto {
private int gradeId;
private String name;
private MemberGradePolicy policy;

public static MemberGradeDto of(MemberGrade memberGrade) {
return MemberGradeDto.builder()
.gradeId(memberGrade.getGradeId())
.name(memberGrade.getName())
.policy(memberGrade.getPolicy())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.t3t.bookstoreapi.member.model.request;

import com.t3t.bookstoreapi.member.model.entity.MemberGradePolicy;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class MemberGradeCreationRequest {

private int gradeId;
private String name;
private MemberGradePolicy policy;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.t3t.bookstoreapi.member.model.response;

import com.t3t.bookstoreapi.member.model.entity.MemberGradePolicy;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MemberGradePolicyResponse {
private BigDecimal startAmount;
private BigDecimal endAmount;
private int rate;

public static MemberGradePolicyResponse of(MemberGradePolicy memberGradePolicy) {
return MemberGradePolicyResponse.builder()
.startAmount(memberGradePolicy.getStartAmount())
.endAmount(memberGradePolicy.getEndAmount())
.rate(memberGradePolicy.getRate())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.t3t.bookstoreapi.member.model.response;

import com.t3t.bookstoreapi.member.model.entity.MemberGradePolicy;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;

import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MemberGradeResponse {

private String name;
private MemberGradePolicy policy;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

public interface MemberGradePolicyRepository extends JpaRepository<MemberGradePolicy, Long> {

@Query("SELECT m FROM MemberGrade m JOIN FETCH m.policy")
@Query("SELECT g.policy FROM MemberGrade m JOIN FETCH MemberGrade g")
List<MemberGradePolicy> findAll();

@Query("SELECT m FROM MemberGrade m JOIN FETCH m.policy WHERE m.policy.policyId = :policyId")
Optional<MemberGradePolicy> findById(Long policyId);
Optional<MemberGradePolicy> findByPolicyId(Long policyId);

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package com.t3t.bookstoreapi.member.repository;

import com.t3t.bookstoreapi.member.model.entity.MemberGrade;
import com.t3t.bookstoreapi.member.model.entity.MemberGradePolicy;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface MemberGradeRepository extends JpaRepository<MemberGrade, Integer> {
Optional<MemberGrade> findByName(String name);

@Query("SELECT m.policy FROM MemberGrade m JOIN FETCH MemberGradePolicy mg WHERE m.policy.policyId = mg.policyId")
List<MemberGrade> findAll();

@Query("SELECT m.policy FROM MemberGrade m JOIN FETCH MemberGradePolicy mg WHERE m.policy.policyId = mg.policyId")
Optional<MemberGrade> findByPolicyId(Long policyId);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.t3t.bookstoreapi.member.service;

import com.t3t.bookstoreapi.member.exception.MemberGradePolicyNotFoundException;
import com.t3t.bookstoreapi.member.model.dto.MemberGradePolicyDto;
import com.t3t.bookstoreapi.member.model.entity.MemberGradePolicy;
import com.t3t.bookstoreapi.member.model.request.CreateMemberGradePolicyRequest;
import com.t3t.bookstoreapi.member.model.response.MemberGradePolicyResponse;
import com.t3t.bookstoreapi.member.repository.MemberGradePolicyRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -27,14 +27,14 @@ public class MemberGradePolicyService {
/**
* 모든 회원 등급 정책 조회
* @return 등록된 모든 회원 등급 정책의 리스트 반환
*
* @author hydrationn(박수화)
*/
@Transactional(readOnly = true)
public List<MemberGradePolicyDto> getMemberGradePolicyList() {
public List<MemberGradePolicyResponse> getMemberGradePolicyList() {
List<MemberGradePolicy> policies = memberGradePolicyRepository.findAll();

return policies.stream()
.map(MemberGradePolicyDto::of)
.map(MemberGradePolicyResponse::of)
.collect(Collectors.toList());
}

Expand All @@ -47,8 +47,8 @@ public List<MemberGradePolicyDto> getMemberGradePolicyList() {
* @author hydrationn(박수화)
*/
@Transactional(readOnly = true)
public MemberGradePolicyDto getMemberGradePolicy(Long policyId) {
return MemberGradePolicyDto.of(memberGradePolicyRepository.findById(policyId)
public MemberGradePolicyResponse getMemberGradePolicy(Long policyId) {
return MemberGradePolicyResponse.of(memberGradePolicyRepository.findByPolicyId(policyId)
.orElseThrow(() -> new MemberGradePolicyNotFoundException(policyId)));
}

Expand All @@ -59,14 +59,14 @@ public MemberGradePolicyDto getMemberGradePolicy(Long policyId) {
*
* @author hydrationn(박수화)
*/
public MemberGradePolicyDto createMemberGradePolicy(CreateMemberGradePolicyRequest request) {
public MemberGradePolicyResponse createMemberGradePolicy(CreateMemberGradePolicyRequest request) {
MemberGradePolicy newMemberGradePolicy = MemberGradePolicy.builder()
.startAmount(request.getStartAmount())
.endAmount(request.getEndAmount())
.rate(request.getRate())
.build();

return MemberGradePolicyDto.of(memberGradePolicyRepository.save(newMemberGradePolicy));
return MemberGradePolicyResponse.of(memberGradePolicyRepository.save(newMemberGradePolicy));
}

/**
Expand All @@ -79,15 +79,13 @@ public MemberGradePolicyDto createMemberGradePolicy(CreateMemberGradePolicyReque
* @return 수정된 회원 등급 정책의 DTO
* @throws MemberGradePolicyNotFoundException 해당 ID의 정책을 찾을 수 없을 경우 예외 발생
*/
public MemberGradePolicyDto updateMemberGradePolicy(Long policyId, BigDecimal startAmount, BigDecimal endAmount, int rate) {
MemberGradePolicy policy = memberGradePolicyRepository.findById(policyId)
public void updateMemberGradePolicy(Long policyId, BigDecimal startAmount, BigDecimal endAmount, int rate) {
MemberGradePolicy policy = memberGradePolicyRepository.findByPolicyId(policyId)
.orElseThrow(() -> new MemberGradePolicyNotFoundException(policyId));

policy.updateStartAmount(startAmount);
policy.updateEndAmount(endAmount);
policy.updateRate(rate);

return MemberGradePolicyDto.of(memberGradePolicyRepository.save(policy));
}

/**
Expand All @@ -97,7 +95,7 @@ public MemberGradePolicyDto updateMemberGradePolicy(Long policyId, BigDecimal st
* @throws MemberGradePolicyNotFoundException 해당 ID의 정책을 찾을 수 없을 경우 예외 발생
*/
public void deleteMemberGradePolicy(Long policyId) {
memberGradePolicyRepository.delete(memberGradePolicyRepository.findById(policyId)
memberGradePolicyRepository.delete(memberGradePolicyRepository.findByPolicyId(policyId)
.orElseThrow(() -> new MemberGradePolicyNotFoundException(policyId)));
}

Expand Down
Loading

0 comments on commit 51744d3

Please sign in to comment.