From 5a4cf9d82b328833393da13b8e4fc8467835a6d7 Mon Sep 17 00:00:00 2001 From: hydrationn Date: Fri, 17 May 2024 14:16:08 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20#189=20=ED=9A=8C=EC=9B=90=20=EB=93=B1?= =?UTF-8?q?=EA=B8=89=20CRUD=20=EC=B4=88=EC=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/MemberGradeController.java | 82 +++++++++++++++++ .../member/model/dto/MemberGradeDto.java | 22 +++++ .../request/MemberGradeCreationRequest.java | 18 ++++ .../model/response/MemberGradeResponse.java | 17 ++++ .../repository/MemberGradeRepository.java | 8 ++ .../member/service/MemberGradeService.java | 89 +++++++++++++++++++ 6 files changed, 236 insertions(+) create mode 100644 src/main/java/com/t3t/bookstoreapi/member/controller/MemberGradeController.java create mode 100644 src/main/java/com/t3t/bookstoreapi/member/model/dto/MemberGradeDto.java create mode 100644 src/main/java/com/t3t/bookstoreapi/member/model/request/MemberGradeCreationRequest.java create mode 100644 src/main/java/com/t3t/bookstoreapi/member/model/response/MemberGradeResponse.java create mode 100644 src/main/java/com/t3t/bookstoreapi/member/service/MemberGradeService.java diff --git a/src/main/java/com/t3t/bookstoreapi/member/controller/MemberGradeController.java b/src/main/java/com/t3t/bookstoreapi/member/controller/MemberGradeController.java new file mode 100644 index 00000000..1c68939b --- /dev/null +++ b/src/main/java/com/t3t/bookstoreapi/member/controller/MemberGradeController.java @@ -0,0 +1,82 @@ +package com.t3t.bookstoreapi.member.controller; + +import com.t3t.bookstoreapi.member.model.dto.MemberGradeDto; +import com.t3t.bookstoreapi.member.model.request.MemberGradeCreationRequest; +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>> getMemberGradeList() { + List memberGradePolicyList = memberGradeService.getMemberGradeList(); + return ResponseEntity.ok(new BaseResponse>().data(memberGradePolicyList)); + } + + /** + * 특정 ID를 기반으로 한 회원 등급 정책 조회 + * @param policyId 조회할 회원 등급 정책의 ID + * @return 조회된 회원 등급 정책을 포함한 BaseResponse 객체 반환 + * + * @author hydrationn(박수화) + */ + @GetMapping("/admin/member-grade-policies/{policyId}") + public ResponseEntity> getMemberGrade(@PathVariable("policyId") Long policyId) { + MemberGradeDto memberGrade = memberGradeService.getMemberGrade(policyId); + return ResponseEntity.ok(new BaseResponse().data(memberGrade)); + } + + /** + * 새로운 회원 등급 정책 생성 + * @param request 생성할 회원 등급 정책의 정보를 담은 요청 객체 + * @return 생성된 회원 등급 정책을 포함한 BaseResponse 객체 반환 + * + * @author hydrationn(박수화) + */ + @PostMapping("/admin/member-grade-policy") + public ResponseEntity> createMemberGrade(@RequestBody MemberGradeCreationRequest request) { + MemberGradeDto memberGrade = memberGradeService.createMemberGrade(request); + return ResponseEntity.ok(new BaseResponse().data(memberGrade)); + } + + /** + * 기존의 회원 등급 정책 업데이트 + * @param policyId 업데이트할 회원 등급 정책의 ID + * @param startAmount 기준 시작 금액 + * @param endAmount 기준 종료 금액 + * @param rate 포인트 적립 비율 + * @return 업데이트된 회원 등급 정책을 포함한 BaseResponse 객체 반환 + * + * @author hydrationn(박수화) + */ + @PutMapping("/admin/member-grade-policy/{policyId}/default") + public ResponseEntity> 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().message("회원 등급 정책 업데이트 요청이 정상적으로 처리되었습니다. ")); + } + + /** + * 특정 ID를 가진 회원 등급 정책 삭제 + * @param policyId 삭제할 회원 등급 정책의 ID + * @return 삭제 성공 메시지를 포함한 BaseResponse 객체 반환 + * + * @author hydrationn(박수화) + */ + @DeleteMapping("/admin/member-grade-policy/{policyId}") + public ResponseEntity> deleteMemberGradePolicy(@PathVariable("policyId") Long policyId) { + memberGradeService.deleteMemberGrade(policyId); + return ResponseEntity.status(HttpStatus.NO_CONTENT).body(new BaseResponse().message("회원 등급 정책 삭제 요청이 성공적으로 처리되었습니다. ")); + } +} diff --git a/src/main/java/com/t3t/bookstoreapi/member/model/dto/MemberGradeDto.java b/src/main/java/com/t3t/bookstoreapi/member/model/dto/MemberGradeDto.java new file mode 100644 index 00000000..c025cc70 --- /dev/null +++ b/src/main/java/com/t3t/bookstoreapi/member/model/dto/MemberGradeDto.java @@ -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(); + } +} diff --git a/src/main/java/com/t3t/bookstoreapi/member/model/request/MemberGradeCreationRequest.java b/src/main/java/com/t3t/bookstoreapi/member/model/request/MemberGradeCreationRequest.java new file mode 100644 index 00000000..320a238b --- /dev/null +++ b/src/main/java/com/t3t/bookstoreapi/member/model/request/MemberGradeCreationRequest.java @@ -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; +} diff --git a/src/main/java/com/t3t/bookstoreapi/member/model/response/MemberGradeResponse.java b/src/main/java/com/t3t/bookstoreapi/member/model/response/MemberGradeResponse.java new file mode 100644 index 00000000..02f616db --- /dev/null +++ b/src/main/java/com/t3t/bookstoreapi/member/model/response/MemberGradeResponse.java @@ -0,0 +1,17 @@ +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; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class MemberGradeResponse { + + private String name; + private MemberGradePolicy policy; +} diff --git a/src/main/java/com/t3t/bookstoreapi/member/repository/MemberGradeRepository.java b/src/main/java/com/t3t/bookstoreapi/member/repository/MemberGradeRepository.java index 81037263..dc884504 100644 --- a/src/main/java/com/t3t/bookstoreapi/member/repository/MemberGradeRepository.java +++ b/src/main/java/com/t3t/bookstoreapi/member/repository/MemberGradeRepository.java @@ -2,9 +2,17 @@ import com.t3t.bookstoreapi.member.model.entity.MemberGrade; 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 { Optional findByName(String name); + @Query("SELECT m.policy FROM MemberGrade m JOIN FETCH MemberGradePolicy mg WHERE m.policy.policyId = mg.policyId") + List findAll(); + + @Query("SELECT m.policy FROM MemberGrade m JOIN FETCH MemberGradePolicy mg WHERE m.policy.policyId = mg.policyId") + Optional findByPolicyId(Long policyId); + } \ No newline at end of file diff --git a/src/main/java/com/t3t/bookstoreapi/member/service/MemberGradeService.java b/src/main/java/com/t3t/bookstoreapi/member/service/MemberGradeService.java new file mode 100644 index 00000000..ab6f7da6 --- /dev/null +++ b/src/main/java/com/t3t/bookstoreapi/member/service/MemberGradeService.java @@ -0,0 +1,89 @@ +package com.t3t.bookstoreapi.member.service; + +import com.t3t.bookstoreapi.member.exception.MemberGradeNotFoundException; +import com.t3t.bookstoreapi.member.exception.MemberGradePolicyNotFoundException; +import com.t3t.bookstoreapi.member.model.dto.MemberGradeDto; +import com.t3t.bookstoreapi.member.model.entity.MemberGrade; +import com.t3t.bookstoreapi.member.model.request.MemberGradeCreationRequest; +import com.t3t.bookstoreapi.member.repository.MemberGradeRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.math.BigDecimal; +import java.util.List; +import java.util.stream.Collectors; + +@Service +@Transactional +@RequiredArgsConstructor +public class MemberGradeService { + private final MemberGradeRepository memberGradeRepository; + + @Transactional(readOnly = true) + public List getMemberGradeList() { + List memberGrades = memberGradeRepository.findAll(); + + return memberGrades.stream() + .map(MemberGradeDto::of) + .collect(Collectors.toList()); + } + + /** + * 특정 ID를 가진 회원 등급 정책 조회 + * @param gradeId 조회할 회원 등급 정책 ID + * @return 조회된 회원 등급 정책 DTO + * @throws MemberGradePolicyNotFoundException 해당 ID의 정책을 찾을 수 없을 경우 예외 발생 + * + * @author hydrationn(박수화) + */ + @Transactional(readOnly = true) + public MemberGradeDto getMemberGrade(Long gradeId) { + return MemberGradeDto.of(memberGradeRepository.findByPolicyId(gradeId) + .orElseThrow(() -> new MemberGradeNotFoundException(gradeId))); + } + + /** + * 새로운 회원 등급 정책 생성 + * @param request 회원 등급 정책 생성 요청 정보 + * @return 생성된 회원 등급 정책의 DTO + * + * @author hydrationn(박수화) + */ + public MemberGradeDto createMemberGrade(MemberGradeCreationRequest request) { + MemberGrade newMemberGrade = MemberGrade.builder() + .name(request.getName()) + .policy(request.getPolicy()) + .build(); + + return MemberGradeDto.of(memberGradeRepository.save(newMemberGrade)); + } + + /** + * 기존의 회원 등급 정책 수정 + * @param policyId 수정할 회원 등급 정책의 ID + * @param startAmount 기준 시작 금액 + * @param endAmount 기준 종료 금액 + * @param rate 포인트 적립 비율 + * @return 수정된 회원 등급 정책의 DTO + * @throws MemberGradePolicyNotFoundException 해당 ID의 정책을 찾을 수 없을 경우 예외 발생 + */ + public void updateMemberGrade(Long policyId, BigDecimal startAmount, BigDecimal endAmount, int rate) { + MemberGrade policy = memberGradeRepository.findByPolicyId(policyId) + .orElseThrow(() -> new MemberGradeNotFoundException(policyId)); + + policy.getPolicy().updateStartAmount(startAmount); + policy.getPolicy().updateEndAmount(endAmount); + policy.getPolicy().updateRate(rate); + } + + /** + * 특정 ID를 가진 회원 등급 정책 삭제 + * @param policyId 삭제할 회원 등급 정책의 ID + * @throws MemberGradePolicyNotFoundException 해당 ID의 정책을 찾을 수 없을 경우 예외 발생 + */ + public void deleteMemberGrade(Long policyId) { + memberGradeRepository.delete(memberGradeRepository.findByPolicyId(policyId) + .orElseThrow(() -> new MemberGradeNotFoundException(policyId))); + } +}