Skip to content

Commit

Permalink
feat: WishCoupon Patch Api
Browse files Browse the repository at this point in the history
  • Loading branch information
2zerozu committed Jul 10, 2023
1 parent e1e5bc8 commit dfb6c11
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected ResponseEntity<Object> handleMethodArgumentNotValid(
HttpStatus status,
WebRequest request
) {
ErrorResponse errorResponse = ErrorResponse.error(ErrorType.VALIDATION_REQUEST_MISSING_EXCEPTION);
ErrorResponse errorResponse = ErrorResponse.error(ErrorType.INVALID_REQUEST_METHOD);
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
}

Expand All @@ -44,7 +44,7 @@ protected ResponseEntity<Object> handleMissingServletRequestParameter(
HttpStatus status,
WebRequest request
) {
ErrorResponse errorResponse = ErrorResponse.error(ErrorType.VALIDATION_REQUEST_MISSING_EXCEPTION);
ErrorResponse errorResponse = ErrorResponse.error(ErrorType.INVALID_REQUEST_METHOD);
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
}

Expand All @@ -55,15 +55,15 @@ protected ResponseEntity<Object> handleMissingPathVariable(
HttpStatus status,
WebRequest request
) {
ErrorResponse errorResponse = ErrorResponse.error(ErrorType.VALIDATION_REQUEST_MISSING_EXCEPTION);
ErrorResponse errorResponse = ErrorResponse.error(ErrorType.INVALID_REQUEST_METHOD);
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(MissingRequestHeaderException.class)
protected ResponseEntity<Object> handleMissingRequestHeaderException(
MissingRequestHeaderException exception
) {
ErrorResponse errorResponse = ErrorResponse.error(ErrorType.VALIDATION_EXCEPTION);
ErrorResponse errorResponse = ErrorResponse.error(ErrorType.INVALID_REQUEST_METHOD);
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.universe.uni.controller;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PatchMapping;
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.service.WishCouponService;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/wish")
public class WishCouponController {

private final WishCouponService wishCouponService;

@PatchMapping
@ResponseStatus(HttpStatus.OK)
public UpdateWishCouponResponseDto updateWishCoupon(
@RequestBody UpdateWishCouponRequestDto requestDto
) {
return wishCouponService.uploadWishCoupon(requestDto);
}
}
30 changes: 27 additions & 3 deletions src/main/java/com/universe/uni/domain/entity/WishCoupon.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.universe.uni.domain.entity;

import static javax.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PROTECTED;

import java.time.LocalDateTime;

import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
Expand All @@ -14,16 +18,16 @@
import com.universe.uni.domain.GameType;
import com.universe.uni.domain.entity.convertor.GameTypeAttributeConverter;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import static javax.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PROTECTED;
import lombok.Setter;

@Entity
@Table(name = "wish_coupon")
@NoArgsConstructor(access = PROTECTED)
@Getter
@Setter
public class WishCoupon {

@Id
Expand Down Expand Up @@ -57,4 +61,24 @@ public class WishCoupon {
@Column(name = "game_type", nullable = false)
@Convert(converter = GameTypeAttributeConverter.class)
private GameType gameType;

@Builder
public WishCoupon(Long id, String image, String content, boolean isVisible, boolean isUsed, LocalDateTime usedAt,
User user, Game game, GameType gameType) {
this.id = id;
this.image = image;
this.content = content;
this.isVisible = isVisible;
this.isUsed = isUsed;
this.usedAt = usedAt;
this.user = user;
this.game = game;
this.gameType = gameType;
}

public static WishCoupon uploadWishCoupon(String content, GameType gameType) {
WishCoupon wishCoupon = WishCoupon.builder().content(content).gameType(gameType).build();
return wishCoupon;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.universe.uni.dto.request;

import lombok.Data;

@Data
public class UpdateWishCouponRequestDto {
private String gameType;
private String content;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.universe.uni.dto.response;

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

@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class UpdateWishCouponResponseDto {
private Long id;
private String image;
private String content;
private boolean isVisible;
private boolean isUsed;
private String usedAt;
private String gameType;

@Builder
public UpdateWishCouponResponseDto(
Long id,
String image,
String content,
boolean isVisible,
boolean isUsed,
String usedAt,
String gameType
) {
this.id = id;
this.image = image;
this.content = content;
this.isVisible = isVisible;
this.isUsed = isUsed;
this.usedAt = usedAt;
this.gameType = gameType;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.universe.uni.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

import com.universe.uni.domain.GameType;
import com.universe.uni.domain.entity.WishCoupon;

public interface WishCouponRepository extends JpaRepository<WishCoupon, Long> {

List<WishCoupon> findByGameTypeAndIsVisibleFalseAndIsUsedFalseAndUsedAtIsNull(GameType gameType);

}
58 changes: 58 additions & 0 deletions src/main/java/com/universe/uni/service/WishCouponService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.universe.uni.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.universe.uni.domain.GameType;
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.exception.BadRequestException;
import com.universe.uni.exception.dto.ErrorType;
import com.universe.uni.repository.WishCouponRepository;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
@Transactional
public class WishCouponService {

private final WishCouponRepository wishCouponRepository;

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

if (wishCouponList.isEmpty()) {
throw new BadRequestException(ErrorType.INVALID_REQUEST_METHOD);
}

WishCoupon wishCoupon = wishCouponList.get(0);

wishCoupon.setContent(requestDto.getContent());
wishCoupon.setVisible(true);

return fromWishCoupon(wishCoupon);
}

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

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

0 comments on commit dfb6c11

Please sign in to comment.