From 65bd0d2bf4034c1a21ab95a41bf71a43aca61450 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Tue, 14 Nov 2023 11:29:26 +0900 Subject: [PATCH 1/8] feat: add is_checked column --- .../repl/gifthub/vouchers/entity/Voucher.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/entity/Voucher.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/entity/Voucher.java index e8b66623..3b5bb5b2 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/entity/Voucher.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/entity/Voucher.java @@ -3,6 +3,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; +import org.hibernate.annotations.ColumnDefault; import org.swmaestro.repl.gifthub.auth.entity.User; import org.swmaestro.repl.gifthub.util.BaseTimeEntity; @@ -55,6 +56,10 @@ public class Voucher extends BaseTimeEntity { @Column private LocalDateTime deletedAt; + @Column(name = "is_checked", columnDefinition = "TINYINT", nullable = false) + @ColumnDefault("0") + private boolean checked; + @Builder public Voucher(Long id, Brand brand, Product product, String barcode, Integer balance, LocalDate expiresAt, String imageUrl, User user) { @@ -71,4 +76,13 @@ public Voucher(Long id, Brand brand, Product product, String barcode, Integer ba public boolean isDeleted() { return deletedAt != null; } + + public boolean isChecked() { + return checked; + } + + public Voucher check() { + this.checked = true; + return this; + } } \ No newline at end of file From 42f551461e9f30a064bf4e760b280e43538f65f4 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Tue, 14 Nov 2023 11:29:56 +0900 Subject: [PATCH 2/8] feat: add is_checked to response --- .../repl/gifthub/vouchers/dto/VoucherReadResponseDto.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/dto/VoucherReadResponseDto.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/dto/VoucherReadResponseDto.java index d3f71c1d..c88bd3b8 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/dto/VoucherReadResponseDto.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/dto/VoucherReadResponseDto.java @@ -26,10 +26,12 @@ public class VoucherReadResponseDto { private boolean accessible; @JsonProperty("is_shared") private boolean shared; + @JsonProperty("is_checked") + private boolean checked; @Builder public VoucherReadResponseDto(Long id, Long productId, String barcode, String expiresAt, Integer price, - Integer balance, String imageUrl, boolean accessible, boolean shared) { + Integer balance, String imageUrl, boolean accessible, boolean shared, boolean checked) { this.id = id; this.productId = productId; this.barcode = barcode; @@ -39,5 +41,6 @@ public VoucherReadResponseDto(Long id, Long productId, String barcode, String ex this.imageUrl = imageUrl; this.accessible = accessible; this.shared = shared; + this.checked = checked; } } From 75214d9b47e82dc8fe1fd079fb1858343f37f8dd Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Tue, 14 Nov 2023 11:30:19 +0900 Subject: [PATCH 3/8] feat: add set checked logic --- .../repl/gifthub/vouchers/service/VoucherService.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java index f8659a4b..b9524edb 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java @@ -114,6 +114,8 @@ public VoucherReadResponseDto read(Long id, String username) { // } VoucherReadResponseDto voucherReadResponseDto = mapToDto(voucher.get()); + + voucherRepository.save(voucher.get().check()); return voucherReadResponseDto; } @@ -269,6 +271,7 @@ public VoucherReadResponseDto mapToDto(Voucher voucher) { .imageUrl(voucher.getImageUrl()) .accessible(voucher.getDeletedAt() == null) .shared(giftCardService.isExist(voucher.getId())) + .checked(voucher.isChecked()) .build(); return voucherReadResponseDto; } From c09a3ef5827275f47fbb5b01800b63480f326ee6 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Tue, 14 Nov 2023 11:34:42 +0900 Subject: [PATCH 4/8] fix: modify set checked logic --- .../repl/gifthub/vouchers/service/VoucherService.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java index b9524edb..2dae574e 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java @@ -115,7 +115,10 @@ public VoucherReadResponseDto read(Long id, String username) { VoucherReadResponseDto voucherReadResponseDto = mapToDto(voucher.get()); - voucherRepository.save(voucher.get().check()); + if (!voucher.get().isChecked()) { + voucherRepository.save(voucher.get().check()); + } + return voucherReadResponseDto; } From d45b20f291c2bbf7652cc01d5a4b18a8c4d6eb9e Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Tue, 14 Nov 2023 14:21:10 +0900 Subject: [PATCH 5/8] feat: implement voucher check logic --- .../vouchers/service/VoucherService.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java index 2dae574e..004a162d 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java @@ -115,10 +115,6 @@ public VoucherReadResponseDto read(Long id, String username) { VoucherReadResponseDto voucherReadResponseDto = mapToDto(voucher.get()); - if (!voucher.get().isChecked()) { - voucherRepository.save(voucher.get().check()); - } - return voucherReadResponseDto; } @@ -482,4 +478,20 @@ public void deleteAll(Long userId) { voucherRepository.save(voucher); }); } + + /** + * 기프티콘 읽음 처리 메서드 + * @param username + * @param voucherId + * @return + */ + public Voucher check(String username, Long voucherId) { + Voucher voucher = read(voucherId); + + if (!voucher.getUser().getUsername().equals(username)) { + throw new BusinessException("상품권을 읽음 처리할 권한이 없습니다.", StatusEnum.FORBIDDEN); + } + + return voucherRepository.save(voucher.check()); + } } \ No newline at end of file From e7a15a4b04d322430d8adc7acbc2ab45b072bf96 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Tue, 14 Nov 2023 14:21:21 +0900 Subject: [PATCH 6/8] feat: implement voucher-check api --- .../vouchers/controller/VoucherController.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherController.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherController.java index deac3bea..9e62503f 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherController.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherController.java @@ -245,4 +245,19 @@ public ResponseEntity readVoucherImage(HttpServletRequest request, @Pat .data(presignedUrlResponseDto) .build()); } + + @PostMapping("/{voucherId}/check") + @Operation(summary = "Voucher 확인(읽음) 처리 메서드", description = "클라이언트에서 요청한 기프티콘을 확인(읽음) 처리하기 위한 메서드입니다.") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "기프티콘 확인(읽음) 처리 성공"), + @ApiResponse(responseCode = "400(404)", description = "존재하지 않는 기프티콘 확인(읽음) 처리 시도"), + }) + public ResponseEntity checkVoucher(HttpServletRequest request, @PathVariable Long voucherId) { + String username = jwtProvider.getUsername(jwtProvider.resolveToken(request).substring(7)); + voucherService.check(username, voucherId); + return ResponseEntity.ok( + SuccessMessage.builder() + .path(request.getRequestURI()) + .build()); + } } \ No newline at end of file From 9bf116d8eabe85cd0332483a118ec3bcb3141646 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Tue, 14 Nov 2023 14:21:36 +0900 Subject: [PATCH 7/8] test: write voucher-check api test --- .../controller/VoucherControllerTest.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/test/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherControllerTest.java b/src/test/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherControllerTest.java index 4d2dfa17..0269b8f3 100644 --- a/src/test/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherControllerTest.java +++ b/src/test/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherControllerTest.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.List; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; @@ -32,6 +33,7 @@ import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUpdateRequestDto; import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUseRequestDto; import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUseResponseDto; +import org.swmaestro.repl.gifthub.vouchers.entity.Voucher; import org.swmaestro.repl.gifthub.vouchers.service.GptService; import org.swmaestro.repl.gifthub.vouchers.service.PendingVoucherService; import org.swmaestro.repl.gifthub.vouchers.service.SearchService; @@ -363,4 +365,29 @@ void shareCancel() throws Exception { .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()); } + + @Test + @WithMockUser(username = "이진우", roles = "USER") + void check() throws Exception { + // Given + Voucher voucher = Voucher.builder() + .id(1L) + .user(User.builder() + .id(1L) + .username("이진우") + .build()) + .build(); + + // When + when(jwtProvider.resolveToken(any())).thenReturn("my_awesome_access_token"); + when(jwtProvider.getUsername(anyString())).thenReturn("이진우"); + when(voucherService.check(anyString(), eq(voucher.getId()))).thenReturn(voucher.check()); + + // Then + Assertions.assertTrue(voucher.isChecked()); + mockMvc.perform(post("/vouchers/1/check") + .header("Authorization", "my_awesome_access_token") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + } } \ No newline at end of file From 2f70f956c8c490e08fe059c4dbcb29197a920f59 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Tue, 14 Nov 2023 16:35:12 +0900 Subject: [PATCH 8/8] fix: remove voucher-check api --- .../controller/VoucherController.java | 15 ----------- .../vouchers/dto/VoucherUpdateRequestDto.java | 4 ++- .../vouchers/service/VoucherService.java | 20 +++----------- .../controller/VoucherControllerTest.java | 27 ------------------- 4 files changed, 7 insertions(+), 59 deletions(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherController.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherController.java index 9e62503f..deac3bea 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherController.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherController.java @@ -245,19 +245,4 @@ public ResponseEntity readVoucherImage(HttpServletRequest request, @Pat .data(presignedUrlResponseDto) .build()); } - - @PostMapping("/{voucherId}/check") - @Operation(summary = "Voucher 확인(읽음) 처리 메서드", description = "클라이언트에서 요청한 기프티콘을 확인(읽음) 처리하기 위한 메서드입니다.") - @ApiResponses({ - @ApiResponse(responseCode = "200", description = "기프티콘 확인(읽음) 처리 성공"), - @ApiResponse(responseCode = "400(404)", description = "존재하지 않는 기프티콘 확인(읽음) 처리 시도"), - }) - public ResponseEntity checkVoucher(HttpServletRequest request, @PathVariable Long voucherId) { - String username = jwtProvider.getUsername(jwtProvider.resolveToken(request).substring(7)); - voucherService.check(username, voucherId); - return ResponseEntity.ok( - SuccessMessage.builder() - .path(request.getRequestURI()) - .build()); - } } \ No newline at end of file diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/dto/VoucherUpdateRequestDto.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/dto/VoucherUpdateRequestDto.java index aa8647a6..466316a8 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/dto/VoucherUpdateRequestDto.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/dto/VoucherUpdateRequestDto.java @@ -19,13 +19,15 @@ public class VoucherUpdateRequestDto { private String productName; private String brandName; private Integer balance; + private Boolean isChecked; @Builder - public VoucherUpdateRequestDto(String barcode, String expiresAt, String productName, String brandName, int balance) { + public VoucherUpdateRequestDto(String barcode, String expiresAt, String productName, String brandName, int balance, boolean isChecked) { this.barcode = barcode; this.expiresAt = expiresAt; this.productName = productName; this.brandName = brandName; this.balance = balance; + this.isChecked = isChecked; } } diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java index 004a162d..0902770e 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java @@ -205,6 +205,10 @@ public VoucherSaveResponseDto update(Long voucherId, VoucherUpdateRequestDto vou voucher.setBalance(voucherUpdateRequestDto.getBalance() == null ? voucher.getBalance() : voucherUpdateRequestDto.getBalance()); + // isChecked 수정 + voucher.setChecked(voucherUpdateRequestDto.getIsChecked() == null ? voucher.isChecked() : + voucherUpdateRequestDto.getIsChecked()); + voucherRepository.save(voucher); return VoucherSaveResponseDto.builder() @@ -478,20 +482,4 @@ public void deleteAll(Long userId) { voucherRepository.save(voucher); }); } - - /** - * 기프티콘 읽음 처리 메서드 - * @param username - * @param voucherId - * @return - */ - public Voucher check(String username, Long voucherId) { - Voucher voucher = read(voucherId); - - if (!voucher.getUser().getUsername().equals(username)) { - throw new BusinessException("상품권을 읽음 처리할 권한이 없습니다.", StatusEnum.FORBIDDEN); - } - - return voucherRepository.save(voucher.check()); - } } \ No newline at end of file diff --git a/src/test/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherControllerTest.java b/src/test/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherControllerTest.java index 0269b8f3..4d2dfa17 100644 --- a/src/test/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherControllerTest.java +++ b/src/test/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherControllerTest.java @@ -7,7 +7,6 @@ import java.util.ArrayList; import java.util.List; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; @@ -33,7 +32,6 @@ import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUpdateRequestDto; import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUseRequestDto; import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUseResponseDto; -import org.swmaestro.repl.gifthub.vouchers.entity.Voucher; import org.swmaestro.repl.gifthub.vouchers.service.GptService; import org.swmaestro.repl.gifthub.vouchers.service.PendingVoucherService; import org.swmaestro.repl.gifthub.vouchers.service.SearchService; @@ -365,29 +363,4 @@ void shareCancel() throws Exception { .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()); } - - @Test - @WithMockUser(username = "이진우", roles = "USER") - void check() throws Exception { - // Given - Voucher voucher = Voucher.builder() - .id(1L) - .user(User.builder() - .id(1L) - .username("이진우") - .build()) - .build(); - - // When - when(jwtProvider.resolveToken(any())).thenReturn("my_awesome_access_token"); - when(jwtProvider.getUsername(anyString())).thenReturn("이진우"); - when(voucherService.check(anyString(), eq(voucher.getId()))).thenReturn(voucher.check()); - - // Then - Assertions.assertTrue(voucher.isChecked()); - mockMvc.perform(post("/vouchers/1/check") - .header("Authorization", "my_awesome_access_token") - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()); - } } \ No newline at end of file