Skip to content

Commit

Permalink
#27 [feat] �소원권 상세보기 Api 구현 (#28)
Browse files Browse the repository at this point in the history
* feat: Add WishCouponResponseDto

* feat: Add JsonProperty

* chore: Change function name

* feat: WishCoupon Get Api
  • Loading branch information
2zerozu authored Jul 12, 2023
1 parent ce56c34 commit 652612f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.universe.uni.controller;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.universe.uni.dto.request.UpdateWishCouponRequestDto;
import com.universe.uni.dto.response.UpdateWishCouponResponseDto;
import com.universe.uni.dto.response.WishCouponResponseDto;
import com.universe.uni.service.WishCouponService;

import lombok.RequiredArgsConstructor;
Expand All @@ -22,9 +25,13 @@ public class WishCouponController {

@PatchMapping
@ResponseStatus(HttpStatus.OK)
public UpdateWishCouponResponseDto updateWishCoupon(
@RequestBody UpdateWishCouponRequestDto requestDto
) {
public UpdateWishCouponResponseDto updateWishCoupon(@RequestBody UpdateWishCouponRequestDto requestDto) {
return wishCouponService.uploadWishCoupon(requestDto);
}

@GetMapping("/{wishCouponId}")
@ResponseStatus(HttpStatus.OK)
public WishCouponResponseDto wishCouponResponseDto(@PathVariable Long wishCouponId) {
return wishCouponService.getWishCoupon(wishCouponId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.universe.uni.dto.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import lombok.Builder;

@JsonPropertyOrder({"id", "isMine", "image", "content", "isVisible", "isUsed", "usedAt", "gameType"})
@Builder
public record WishCouponResponseDto(
Long id,
boolean isMine,
String image,
String content,
@JsonProperty("isVisible") boolean visible,
@JsonProperty("isUsed") boolean used,
String usedAt,
String gameType
) {
}
30 changes: 28 additions & 2 deletions src/main/java/com/universe/uni/service/WishCouponService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import com.universe.uni.domain.entity.WishCoupon;
import com.universe.uni.dto.request.UpdateWishCouponRequestDto;
import com.universe.uni.dto.response.UpdateWishCouponResponseDto;
import com.universe.uni.dto.response.WishCouponResponseDto;
import com.universe.uni.exception.BadRequestException;
import com.universe.uni.exception.NotFoundException;
import com.universe.uni.exception.dto.ErrorType;
import com.universe.uni.repository.WishCouponRepository;

Expand Down Expand Up @@ -37,10 +39,17 @@ public UpdateWishCouponResponseDto uploadWishCoupon(UpdateWishCouponRequestDto r
wishCoupon.updateContent(requestDto.content());
wishCoupon.makeVisible();

return fromWishCoupon(wishCoupon);
return fromWishCouponToUpdateWishCouponResponseDto(wishCoupon);
}

private UpdateWishCouponResponseDto fromWishCoupon(WishCoupon wishCoupon) {
@Transactional
public WishCouponResponseDto getWishCoupon(Long wishCouponId) {
WishCoupon wishCoupon = wishCouponRepository.findById(wishCouponId)
.orElseThrow(() -> new NotFoundException(ErrorType.INVALID_ENDPOINT_EXCEPTION));
return fromWishCouponToWishCouponResponseDto(wishCoupon);
}

private UpdateWishCouponResponseDto fromWishCouponToUpdateWishCouponResponseDto(WishCoupon wishCoupon) {
String usedAt = wishCoupon.getUsedAt() != null ? wishCoupon.getUsedAt().toString() : null;

return UpdateWishCouponResponseDto.builder()
Expand All @@ -53,4 +62,21 @@ private UpdateWishCouponResponseDto fromWishCoupon(WishCoupon wishCoupon) {
.gameType(String.valueOf(wishCoupon.getGameType()))
.build();
}

private WishCouponResponseDto fromWishCouponToWishCouponResponseDto(WishCoupon wishCoupon) {
/** TODO 영주 : 추후 1L 내 userId로 바꾸기*/
boolean isMine = wishCoupon.getUser().getId() == 1L;
String usedAt = wishCoupon.getUsedAt() != null ? wishCoupon.getUsedAt().toString() : null;

return WishCouponResponseDto.builder()
.id(wishCoupon.getId())
.isMine(isMine)
.image(wishCoupon.getImage())
.content(wishCoupon.getContent())
.visible(wishCoupon.isVisible())
.used(wishCoupon.isUsed())
.usedAt(usedAt)
.gameType(String.valueOf(wishCoupon.getGameType()))
.build();
}
}

0 comments on commit 652612f

Please sign in to comment.