-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from nhnacademy-be5-T3Team/feature/member_gra…
…de_policies Feature/member grade policies #151 회원 등급 정책 CRUD 구현
- Loading branch information
Showing
7 changed files
with
307 additions
and
4 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
src/main/java/com/t3t/bookstoreapi/member/controller/MemberGradePolicyController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
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.service.MemberGradePolicyService; | ||
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; | ||
|
||
/** | ||
* 회원 등급 정책에 관한 HTTP 요청을 처리하는 컨트롤러 | ||
*/ | ||
@RestController | ||
@RequiredArgsConstructor | ||
public class MemberGradePolicyController { | ||
private final MemberGradePolicyService memberGradePolicyService; | ||
|
||
/** | ||
* 모든 회원 등급 정책 조회 | ||
* @return 회원 등급 정책 목록을 포함한 BaseResponse 객체 반환 | ||
* | ||
* @author hydrationn(박수화) | ||
*/ | ||
@GetMapping("/policy") | ||
public ResponseEntity<BaseResponse<List<MemberGradePolicyDto>>> getMemberGradePolicyList() { | ||
List<MemberGradePolicyDto> memberGradePolicyDtoList = memberGradePolicyService.getMemberGradePolicyList(); | ||
return ResponseEntity.ok(new BaseResponse<List<MemberGradePolicyDto>>().data(memberGradePolicyDtoList)); | ||
} | ||
|
||
/** | ||
* 특정 ID를 기반으로 한 회원 등급 정책 조회 | ||
* @param policyId 조회할 회원 등급 정책의 ID | ||
* @return 조회된 회원 등급 정책을 포함한 BaseResponse 객체 반환 | ||
* | ||
* @author hydrationn(박수화) | ||
*/ | ||
@GetMapping("/policy") | ||
public ResponseEntity<BaseResponse<MemberGradePolicyDto>> getMemberGradePolicy(@RequestParam Long policyId) { | ||
MemberGradePolicyDto policy = memberGradePolicyService.getMemberGradePolicy(policyId); | ||
return ResponseEntity.ok(new BaseResponse<MemberGradePolicyDto>().data(policy)); | ||
} | ||
|
||
/** | ||
* 새로운 회원 등급 정책 생성 | ||
* @param request 생성할 회원 등급 정책의 정보를 담은 요청 객체 | ||
* @return 생성된 회원 등급 정책을 포함한 BaseResponse 객체 반환 | ||
* | ||
* @author hydrationn(박수화) | ||
*/ | ||
@PostMapping("/policy") | ||
public ResponseEntity<BaseResponse<MemberGradePolicyDto>> createMemberGradePolicy(@RequestBody CreateMemberGradePolicyRequest request) { | ||
MemberGradePolicyDto createdPolicy = memberGradePolicyService.createMemberGradePolicy(request); | ||
return ResponseEntity.ok(new BaseResponse<MemberGradePolicyDto>().data(createdPolicy)); | ||
} | ||
|
||
/** | ||
* 기존의 회원 등급 정책 업데이트 | ||
* @param policyId 업데이트할 회원 등급 정책의 ID | ||
* @param startAmount 기준 시작 금액 | ||
* @param endAmount 기준 종료 금액 | ||
* @param rate 포인트 적립 비율 | ||
* @return 업데이트된 회원 등급 정책을 포함한 BaseResponse 객체 반환 | ||
* | ||
* @author hydrationn(박수화) | ||
*/ | ||
@PutMapping("/policy/{policyId}") | ||
public ResponseEntity<BaseResponse<MemberGradePolicyDto>> updateMemberGradePolicy(@PathVariable Long policyId, | ||
@RequestParam BigDecimal startAmount, | ||
@RequestParam BigDecimal endAmount, | ||
@RequestParam int rate) { | ||
MemberGradePolicyDto updatedPolicy = memberGradePolicyService.updateMemberGradePolicy(policyId, startAmount, endAmount, rate); | ||
return ResponseEntity.ok(new BaseResponse<MemberGradePolicyDto>().data(updatedPolicy)); | ||
} | ||
|
||
/** | ||
* 특정 ID를 가진 회원 등급 정책 삭제 | ||
* @param policyId 삭제할 회원 등급 정책의 ID | ||
* @return 삭제 성공 메시지를 포함한 BaseResponse 객체 반환 | ||
* | ||
* @author hydrationn(박수화) | ||
*/ | ||
@DeleteMapping("/policy/{policyId}") | ||
public ResponseEntity<BaseResponse<Void>> deleteMemberGradePolicy(@PathVariable Long policyId) { | ||
memberGradePolicyService.deleteMemberGradePolicy(policyId); | ||
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(new BaseResponse<Void>().message("해당 회원 등급 정책이 성공적으로 삭제되었습니다.")); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/t3t/bookstoreapi/member/exception/MemberGradePolicyNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.t3t.bookstoreapi.member.exception; | ||
|
||
/** | ||
* 존재하지 않는 회원 등급 정책(MemberGradePolicy)에 대해 조회를 시도하는 경우 발생하는 예외 | ||
* | ||
* @author hydrationn(박수화) | ||
*/ | ||
public class MemberGradePolicyNotFoundException extends RuntimeException { | ||
private static final String DEFAULT_MESSAGE = "존재하지 않는 회원 등급 정책입니다."; | ||
public MemberGradePolicyNotFoundException() { | ||
super(DEFAULT_MESSAGE); | ||
} | ||
|
||
public MemberGradePolicyNotFoundException(Long forTarget) { | ||
super(String.format("%s (%s)", DEFAULT_MESSAGE, forTarget)); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/t3t/bookstoreapi/member/model/dto/MemberGradePolicyDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.t3t.bookstoreapi.member.model.dto; | ||
|
||
import com.t3t.bookstoreapi.member.model.entity.MemberGradePolicy; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.Getter; | ||
|
||
import javax.validation.constraints.NotNull; | ||
import java.math.BigDecimal; | ||
|
||
/** | ||
* 회원 등급 정책 생성 정보를 전송하는 데 사용되는 Data Transfer Object(DTO) | ||
* @author hydrationn(박수화) | ||
*/ | ||
@Data | ||
@Getter | ||
@Builder | ||
public class MemberGradePolicyDto { | ||
|
||
@NotNull | ||
private Long policyId; | ||
private BigDecimal startAmount; | ||
private BigDecimal endAmount; | ||
private int rate; | ||
|
||
public static MemberGradePolicyDto of(MemberGradePolicy memberGradePolicy) { | ||
return MemberGradePolicyDto.builder() | ||
.policyId(memberGradePolicy.getPolicyId()) | ||
.startAmount(memberGradePolicy.getStartAmount()) | ||
.endAmount(memberGradePolicy.getEndAmount()) | ||
.rate(memberGradePolicy.getRate()) | ||
.build(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/main/java/com/t3t/bookstoreapi/member/model/entity/MemberGrade.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/com/t3t/bookstoreapi/member/model/request/CreateMemberGradePolicyRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.t3t.bookstoreapi.member.model.request; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
|
||
import java.math.BigDecimal; | ||
|
||
/** | ||
* 회원 등급 정책 정보 생성을 위한 요청 데이터 모델 | ||
* 관리자가 정책에 대한 상세 정보를 생성할 때 필요한 정보를 전달하는 데 사용 | ||
* | ||
* @author hydrationn(박수화) | ||
*/ | ||
@Data | ||
@AllArgsConstructor | ||
public class CreateMemberGradePolicyRequest { | ||
|
||
private BigDecimal startAmount; | ||
private BigDecimal endAmount; | ||
private int rate; | ||
|
||
} |
105 changes: 105 additions & 0 deletions
105
src/main/java/com/t3t/bookstoreapi/member/service/MemberGradePolicyService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
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.repository.MemberGradePolicyRepository; | ||
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 MemberGradePolicyService { | ||
private final MemberGradePolicyRepository memberGradePolicyRepository; | ||
|
||
/** | ||
* 모든 회원 등급 정책 조회 | ||
* @return 등록된 모든 회원 등급 정책의 리스트 반환 | ||
* | ||
* @author hydrationn(박수화) | ||
*/ | ||
@Transactional(readOnly = true) | ||
public List<MemberGradePolicyDto> getMemberGradePolicyList() { | ||
List<MemberGradePolicy> policies = memberGradePolicyRepository.findAll(); | ||
return policies.stream() | ||
.map(MemberGradePolicyDto::of) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
/** | ||
* 특정 ID를 가진 회원 등급 정책 조회 | ||
* @param policyId 조회할 회원 등급 정책 ID | ||
* @return 조회된 회원 등급 정책 DTO | ||
* @throws MemberGradePolicyNotFoundException 해당 ID의 정책을 찾을 수 없을 경우 예외 발생 | ||
* | ||
* @author hydrationn(박수화) | ||
*/ | ||
@Transactional(readOnly = true) | ||
public MemberGradePolicyDto getMemberGradePolicy(Long policyId) { | ||
MemberGradePolicy policy = memberGradePolicyRepository.findById(policyId) | ||
.orElseThrow(() -> new MemberGradePolicyNotFoundException(policyId)); | ||
|
||
return MemberGradePolicyDto.of(policy); | ||
} | ||
|
||
/** | ||
* 새로운 회원 등급 정책 생성 | ||
* @param request 회원 등급 정책 생성 요청 정보 | ||
* @return 생성된 회원 등급 정책의 DTO | ||
* | ||
* @author hydrationn(박수화) | ||
*/ | ||
public MemberGradePolicyDto createMemberGradePolicy(CreateMemberGradePolicyRequest request) { | ||
MemberGradePolicy newMemberGradePolicy = MemberGradePolicy.builder() | ||
.startAmount(request.getStartAmount()) | ||
.endAmount(request.getEndAmount()) | ||
.rate(request.getRate()) | ||
.build(); | ||
|
||
return MemberGradePolicyDto.of(memberGradePolicyRepository.save(newMemberGradePolicy)); | ||
} | ||
|
||
/** | ||
* 기존의 회원 등급 정책 수정 | ||
* | ||
* @param policyId 수정할 회원 등급 정책의 ID | ||
* @param startAmount 기준 시작 금액 | ||
* @param endAmount 기준 종료 금액 | ||
* @param rate 포인트 적립 비율 | ||
* @return 수정된 회원 등급 정책의 DTO | ||
* @throws MemberGradePolicyNotFoundException 해당 ID의 정책을 찾을 수 없을 경우 예외 발생 | ||
*/ | ||
public MemberGradePolicyDto updateMemberGradePolicy(Long policyId, BigDecimal startAmount, BigDecimal endAmount, int rate) { | ||
MemberGradePolicy policy = memberGradePolicyRepository.findById(policyId) | ||
.orElseThrow(() -> new MemberGradePolicyNotFoundException(policyId)); | ||
|
||
policy.updateStartAmount(startAmount); | ||
policy.updateEndAmount(endAmount); | ||
policy.updateRate(rate); | ||
|
||
return MemberGradePolicyDto.of(memberGradePolicyRepository.save(policy)); | ||
} | ||
|
||
/** | ||
* 특정 ID를 가진 회원 등급 정책 삭제 | ||
* | ||
* @param policyId 삭제할 회원 등급 정책의 ID | ||
* @throws MemberGradePolicyNotFoundException 해당 ID의 정책을 찾을 수 없을 경우 예외 발생 | ||
*/ | ||
public void deleteMemberGradePolicy(Long policyId) { | ||
memberGradePolicyRepository.delete(memberGradePolicyRepository.findById(policyId) | ||
.orElseThrow(() -> new MemberGradePolicyNotFoundException(policyId))); | ||
} | ||
|
||
} |