Skip to content

Commit

Permalink
feat: Use wishcoupon api
Browse files Browse the repository at this point in the history
  • Loading branch information
2zerozu committed Jul 12, 2023
1 parent ce56c34 commit 37f1fc9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.http.HttpStatus;
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;
Expand All @@ -27,4 +28,10 @@ public UpdateWishCouponResponseDto updateWishCoupon(
) {
return wishCouponService.uploadWishCoupon(requestDto);
}

@PatchMapping("/{wishCouponId}")
@ResponseStatus(HttpStatus.OK)
public void useWishCoupon(@PathVariable Long wishCouponId) {
wishCouponService.useWishCoupon(wishCouponId);
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/universe/uni/domain/entity/WishCoupon.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,9 @@ public void updateContent(String content) {
public void makeVisible() {
this.isVisible = true;
}

public void useWishCoupon() {
this.isUsed = true;
this.usedAt = LocalDateTime.now();
}
}
13 changes: 10 additions & 3 deletions src/main/java/com/universe/uni/service/WishCouponService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.universe.uni.dto.request.UpdateWishCouponRequestDto;
import com.universe.uni.dto.response.UpdateWishCouponResponseDto;
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 All @@ -22,11 +23,10 @@ public class WishCouponService {

private final WishCouponRepository wishCouponRepository;

@Transactional
public UpdateWishCouponResponseDto uploadWishCoupon(UpdateWishCouponRequestDto requestDto) {
GameType gameType = GameType.valueOf(requestDto.gameType());
List<WishCoupon> wishCouponList = wishCouponRepository.findByGameTypeAndIsVisibleFalseAndIsUsedFalseAndUsedAtIsNull(
gameType);
List<WishCoupon> wishCouponList = wishCouponRepository
.findByGameTypeAndIsVisibleFalseAndIsUsedFalseAndUsedAtIsNull(gameType);

if (wishCouponList.isEmpty()) {
throw new BadRequestException(ErrorType.INVALID_REQUEST_METHOD);
Expand All @@ -40,6 +40,13 @@ public UpdateWishCouponResponseDto uploadWishCoupon(UpdateWishCouponRequestDto r
return fromWishCoupon(wishCoupon);
}

public void useWishCoupon(Long wishCouponId) {
WishCoupon wishCoupon = wishCouponRepository.findById(wishCouponId)
.orElseThrow(() -> new NotFoundException(ErrorType.INVALID_ENDPOINT_EXCEPTION));

wishCoupon.useWishCoupon();
}

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

Expand Down

0 comments on commit 37f1fc9

Please sign in to comment.