-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
179 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/com/universe/uni/controller/WishCouponController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/com/universe/uni/dto/request/UpdateWishCouponRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/universe/uni/dto/response/UpdateWishCouponResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/universe/uni/repository/WishCouponRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
58
src/main/java/com/universe/uni/service/WishCouponService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |