Skip to content

Commit

Permalink
Implement voucher-update-response-dto (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
inh2613 authored Nov 14, 2023
2 parents 718c1f2 + 32beef5 commit 3ed894e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherShareRequestDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherShareResponseDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUpdateRequestDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUpdateResponseDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUseRequestDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUseResponseDto;
import org.swmaestro.repl.gifthub.vouchers.service.PendingVoucherService;
Expand Down Expand Up @@ -132,7 +133,7 @@ public ResponseEntity<Message> listVoucher(HttpServletRequest request, @RequestP
})
public ResponseEntity<Message> updateVoucher(HttpServletRequest request, @PathVariable Long voucherId,
@RequestBody VoucherUpdateRequestDto voucherUpdateRequestDto) throws IOException {
VoucherSaveResponseDto updatedVoucher = voucherService.update(voucherId, voucherUpdateRequestDto);
VoucherUpdateResponseDto updatedVoucher = voucherService.update(voucherId, voucherUpdateRequestDto);
return ResponseEntity.ok(
SuccessMessage.builder()
.path(request.getRequestURI())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.swmaestro.repl.gifthub.vouchers.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Getter
@ToString
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class VoucherUpdateResponseDto {
private Long id;
private Long productId;
private String barcode;
private String expiresAt;
private Integer price;
private Integer balance;
private String imageUrl;
@JsonProperty("is_accessible")
private boolean accessible;
@JsonProperty("is_shared")
private boolean shared;

@Builder
public VoucherUpdateResponseDto(Long id, Long productId, String barcode, String expiresAt, Integer price,
Integer balance, String imageUrl, boolean accessible, boolean shared) {
this.id = id;
this.productId = productId;
this.barcode = barcode;
this.expiresAt = expiresAt;
this.price = price;
this.balance = balance;
this.imageUrl = imageUrl;
this.accessible = accessible;
this.shared = shared;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherShareRequestDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherShareResponseDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUpdateRequestDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUpdateResponseDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUseRequestDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUseResponseDto;
import org.swmaestro.repl.gifthub.vouchers.entity.Brand;
Expand Down Expand Up @@ -164,7 +165,7 @@ public List<Long> list(String username) {
/*
기프티콘 정보 수정 메서드
*/
public VoucherSaveResponseDto update(Long voucherId, VoucherUpdateRequestDto voucherUpdateRequestDto) {
public VoucherUpdateResponseDto update(Long voucherId, VoucherUpdateRequestDto voucherUpdateRequestDto) {
Voucher voucher = voucherRepository.findById(voucherId)
.orElseThrow(() -> new BusinessException("존재하지 않는 상품권 입니다.", StatusEnum.NOT_FOUND));
// Balance 수정
Expand Down Expand Up @@ -211,8 +212,18 @@ public VoucherSaveResponseDto update(Long voucherId, VoucherUpdateRequestDto vou

voucherRepository.save(voucher);

return VoucherSaveResponseDto.builder()
return VoucherUpdateResponseDto.builder()
.id(voucherId)
.accessible(voucher.getDeletedAt() == null)
.id(voucher.getId())
.productId(voucher.getProduct().getId())
.barcode(voucher.getBarcode())
.price(voucher.getProduct().getPrice())
.balance(voucher.getBalance())
.expiresAt(voucher.getExpiresAt().toString())
.imageUrl(voucher.getImageUrl())
.accessible(voucher.getDeletedAt() == null)
.shared(giftCardService.isExist(voucher.getId()))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherShareRequestDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherShareResponseDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUpdateRequestDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUpdateResponseDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUseRequestDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUseResponseDto;
import org.swmaestro.repl.gifthub.vouchers.service.GptService;
Expand Down Expand Up @@ -176,20 +177,20 @@ void voucherUpdateTest() throws Exception {
.expiresAt("2023-06-15")
.build();

VoucherSaveResponseDto voucherSaveResponseDto = VoucherSaveResponseDto.builder()
VoucherUpdateResponseDto voucherUpdateResponseDto = VoucherUpdateResponseDto.builder()
.id(1L)
.build();

// when
when(jwtProvider.resolveToken(any())).thenReturn("my_awesome_access_token");
when(jwtProvider.getUsername(anyString())).thenReturn("이진우");
when(voucherService.update(any(), any(VoucherUpdateRequestDto.class))).thenReturn(voucherSaveResponseDto);
when(voucherService.update(any(), any(VoucherUpdateRequestDto.class))).thenReturn(voucherUpdateResponseDto);

// then
mockMvc.perform(patch("/vouchers/1")
.header("Authorization", "Bearer my_awesome_access_token")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(voucherSaveResponseDto)))
.content(objectMapper.writeValueAsString(voucherUpdateResponseDto)))
.andExpect(status().isOk());
}

Expand Down

0 comments on commit 3ed894e

Please sign in to comment.