Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…e-mono-be into dev
  • Loading branch information
choihyeongjun committed Nov 11, 2022
2 parents daba957 + a6bcf6d commit 8ed8dec
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.feelmycode.parabole.dto.CouponSellerResponseDto;
import com.feelmycode.parabole.dto.CouponUseAndAssignRequestDto;
import com.feelmycode.parabole.dto.CouponUserResponseDto;
import com.feelmycode.parabole.enumtype.CouponType;
import com.feelmycode.parabole.global.api.ParaboleResponse;
import com.feelmycode.parabole.global.error.exception.NoDataException;
import com.feelmycode.parabole.global.error.exception.ParaboleException;
Expand All @@ -25,11 +26,11 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -52,12 +53,12 @@ public class CouponController {
private final static int DEFAULT_PAGE = 0;
private final static int DEFAULT_SIZE = 20;

@PostMapping("/create")
public ResponseEntity<ParaboleResponse> addCoupon(@RequestAttribute Long userId,
@PostMapping("/new")
public ResponseEntity<ParaboleResponse> addCoupon(@RequestAttribute Long sellerId,
@RequestBody CouponCreateRequestDto dto) {

/** addCoupon, addUserCoupon 이 모두 발생한다. */
CouponCreateResponseDto response = couponService.addCoupon(userId, dto);
CouponCreateResponseDto response = couponService.addCoupon(sellerId, dto);
return ParaboleResponse.CommonResponse(HttpStatus.OK, true, "쿠폰 등록 성공", response);
}

Expand Down Expand Up @@ -98,18 +99,15 @@ public ResponseEntity<ParaboleResponse> assignCoupon(
@GetMapping("/seller/list")
public ResponseEntity<ParaboleResponse> getSellerCouponList(@RequestAttribute Long sellerId) {

Pageable getPageable = PageRequest.of(DEFAULT_PAGE, DEFAULT_SIZE);

Page<CouponSellerResponseDto> sellerCouponList = couponService.getSellerCouponListBySellerId(sellerId);

return ParaboleResponse.CommonResponse(HttpStatus.OK,
true, "셀러 쿠폰 목록", sellerCouponList);
}

@GetMapping("/user/list")
public ResponseEntity<ParaboleResponse> getUserCouponList(@RequestAttribute Long userId) {

Pageable getPageable = PageRequest.of(DEFAULT_PAGE, DEFAULT_SIZE);

Page<CouponUserResponseDto> userCouponList = couponService.getUserCouponList(userId);
return ParaboleResponse.CommonResponse(HttpStatus.OK,
true, "유저 쿠폰 목록", userCouponList);
Expand All @@ -118,19 +116,18 @@ public ResponseEntity<ParaboleResponse> getUserCouponList(@RequestAttribute Long
@GetMapping("/list")
public ResponseEntity<ParaboleResponse> getCouponList(@RequestAttribute Long userId) {

Pageable getPageable = PageRequest.of(DEFAULT_PAGE, DEFAULT_SIZE);

if(userService.isSeller(userId)){
Page<CouponUserResponseDto> userCouponList = couponService.getUserCouponList(userId);
Seller seller = sellerService.getSellerByUserId(userId);
Page<CouponSellerResponseDto> sellerCouponList = couponService.getSellerCouponList(seller.getId());
return ParaboleResponse.CommonResponse(HttpStatus.OK,
true, "유저 쿠폰 목록", userCouponList);
true, "셀러 쿠폰 목록", sellerCouponList);
}
Seller seller = sellerService.getSellerByUserId(userId);
Page<CouponSellerResponseDto> sellerCouponList = couponService.getSellerCouponList(seller.getId());
Page<CouponUserResponseDto> userCouponList = couponService.getUserCouponList(userId);
return ParaboleResponse.CommonResponse(HttpStatus.OK,
true, "셀러 쿠폰 목록", sellerCouponList);
true, "유저 쿠폰 목록", userCouponList);
}


@GetMapping("/info")
public ResponseEntity<ParaboleResponse> getCouponInfo(@RequestParam String couponSNo) {

Expand All @@ -140,9 +137,11 @@ public ResponseEntity<ParaboleResponse> getCouponInfo(@RequestParam String coupo
}

@GetMapping("/data")
public CouponDto getCouponData(@RequestParam Long couponId){
public CouponDto getCouponData(@RequestParam Long couponId) {
Coupon coupon = couponService.getCouponById(couponId);
CouponDto couponDto = new CouponDto(coupon.getId(), coupon.getDetail(), coupon.getDiscountValue(), coupon.getExpiresAt());
CouponDto couponDto = new CouponDto(coupon.getId(), coupon.getName(),
CouponType.returnNameByValue(coupon.getType().getValue()), coupon.getDetail(),
coupon.getDiscountValue(), coupon.getExpiresAt());
return couponDto;
}

Expand All @@ -153,4 +152,11 @@ public ResponseEntity<ParaboleResponse> useUserCoupon(@RequestAttribute Long use
couponService.useUserCoupon(dto.getCouponSNo(), userId);
return ParaboleResponse.CommonResponse(HttpStatus.OK, true, "쿠폰이 정상적으로 사용되었습니다.");
}

@PatchMapping("/{couponId}/stock/{stock}")
public Boolean setProductRemains(@PathVariable("couponId") Long couponId,
@PathVariable("stock") Integer stock) {
log.info("Set Coupon Remains By Event Server : {} ", couponId);
return couponService.setCouponStock(couponId, stock);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -92,4 +94,10 @@ public ResponseEntity<ParaboleResponse> getProductBySellerId(@RequestAttribute L
return ParaboleResponse.CommonResponse(HttpStatus.OK, true, "판매자가 등록한 상품 목록", response);
}

@PatchMapping("/{productId}/stock/{stock}")
public Boolean setProductRemains(@PathVariable("productId") Long productId,
@PathVariable("stock") Integer stock) {
log.info("Set Product Remains By Event Server : {} ", productId);
return productService.setProductRemains(productId, Long.valueOf(stock));
}
}
27 changes: 7 additions & 20 deletions src/main/java/com/feelmycode/parabole/domain/Coupon.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.feelmycode.parabole.domain;

import com.feelmycode.parabole.domain.Seller;
import com.feelmycode.parabole.enumtype.CouponType;
import com.feelmycode.parabole.enumtype.CouponUseState;
import com.feelmycode.parabole.service.SellerService;
import com.sun.istack.NotNull;
import java.io.Serializable;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -62,14 +60,6 @@ public class Coupon extends BaseEntity implements Serializable {
@NotNull
private LocalDateTime expiresAt;

@Column(name = "coupon_max_discount_amount")
@NotNull
private Long maxDiscountAmount;

@Column(name = "coupon_min_payment_amount")
@NotNull
private Long minPaymentAmount;

@Column(name = "coupon_details")
@NotNull
private String detail;
Expand All @@ -81,22 +71,15 @@ public class Coupon extends BaseEntity implements Serializable {
@OneToMany(mappedBy = "coupon", cascade = CascadeType.ALL)
private List<UserCoupon> userCoupons = new ArrayList<>();

// public void setSeller(Seller seller) {
// this.seller = seller;
// }

public Coupon(String name, Seller seller, CouponType type, Integer discountValue,
LocalDateTime validAt, LocalDateTime expiresAt,
Long maxDiscountAmount, Long minPaymentAmount, String detail, Integer cnt) {
LocalDateTime validAt, LocalDateTime expiresAt, String detail, Integer cnt) {

this.name = name;
this.seller = seller;
this.type = type;
this.discountValue = discountValue;
this.validAt = validAt;
this.expiresAt = expiresAt;
this.maxDiscountAmount = maxDiscountAmount;
this.minPaymentAmount = minPaymentAmount;
this.detail = detail;
this.cnt = cnt;
}
Expand Down Expand Up @@ -149,10 +132,14 @@ public List<UserCoupon> getNotAssignedUserCouponList() {
}

public void setCouponForEvent(Integer inputStock) {
userCoupons.stream().limit(inputStock).forEach(UserCoupon::setEventEnrolled);
userCoupons.stream().limit(inputStock)
.filter(userCoupon -> userCoupon.getUseState().equals(CouponUseState.NotUsed))
.forEach(UserCoupon::setEventEnrolled);
}

public void cancelCouponEvent(Integer inputStock) {
userCoupons.stream().limit(inputStock).forEach(UserCoupon::setNotUsed);
userCoupons.stream().limit(inputStock)
.filter(userCoupon -> userCoupon.getUseState().equals(CouponUseState.EventEnrolled))
.forEach(UserCoupon::setNotUsed);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public class CouponCreateRequestDto {
private Integer discountValue;
private LocalDateTime validAt;
private LocalDateTime expiresAt;
private Long maxDiscountAmount;
private Long minPaymentAmount;
private String detail;
private Integer cnt;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
public class CouponCreateResponseDto {

private String couponName;
private String sellerName;
private String sellerStorename;
private String type;
private Integer cnt;

public CouponCreateResponseDto(String couponName, String sellerName, String type,
public CouponCreateResponseDto(String couponName, String sellerStorename, String type,
Integer cnt) {
this.couponName = couponName;
this.sellerName = sellerName;
this.sellerStorename = sellerStorename;
this.type = type;
this.cnt = cnt;
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/feelmycode/parabole/dto/CouponDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
public class CouponDto {

private Long couponId;
private String couponName;
private String couponType;
private String couponDetail;
private Integer couponDiscountValue;
private LocalDateTime expiresAt;

public CouponDto(Long couponId, String couponDetail, Integer couponDiscountValue,
public CouponDto(Long couponId, String couponName, String couponType, String couponDetail, Integer couponDiscountValue,
LocalDateTime expiresAt) {
this.couponId = couponId;
this.couponName = couponName;
this.couponType = couponType;
this.couponDetail = couponDetail;
this.couponDiscountValue = couponDiscountValue;
this.expiresAt = expiresAt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ public class CouponSellerResponseDto {
private String name;
private Integer type;
private Integer discountValue;
private LocalDateTime createdAt;
private LocalDateTime validAt;
private LocalDateTime expiresAt;
private Long maxDiscountAmount;
private Long minPaymentAmount;
private String detail;
private Integer cnt;
private Integer remains;
Expand All @@ -28,11 +24,7 @@ public CouponSellerResponseDto(Coupon coupon) {
this.name = coupon.getName();
this.type = coupon.getType().getValue();
this.discountValue = coupon.getDiscountValue();
this.createdAt = coupon.getCreatedAt();
this.validAt = coupon.getValidAt();
this.expiresAt = coupon.getExpiresAt();
this.maxDiscountAmount = coupon.getMaxDiscountAmount();
this.minPaymentAmount = coupon.getMinPaymentAmount();
this.detail = coupon.getDetail();
this.cnt = coupon.getCnt();
this.remains = coupon.getNotAssignedUserCouponList().size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.feelmycode.parabole.domain.Coupon;
import com.feelmycode.parabole.domain.UserCoupon;
import com.feelmycode.parabole.enumtype.CouponType;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
Expand All @@ -18,13 +16,8 @@ public class CouponUserResponseDto {
private Integer discountValue;
private String useState;
private LocalDateTime useDate;
private LocalDateTime acquiredDate;
private LocalDateTime validAt;
private LocalDateTime expiresAt;

private Long maxDiscountAmount;
private Long minPaymentAmount;

public CouponUserResponseDto(Coupon coupon, UserCoupon userCoupon, String sellerName) {

this.name = coupon.getName();
Expand All @@ -33,12 +26,8 @@ public CouponUserResponseDto(Coupon coupon, UserCoupon userCoupon, String seller
this.type = coupon.getType().getName();
this.discountValue = coupon.getDiscountValue();
this.useState = userCoupon.getUseState().getState();
this.acquiredDate = userCoupon.getAcquiredDate();
this.useDate = userCoupon.getUseDate();
this.validAt = coupon.getValidAt();
this.expiresAt = coupon.getExpiresAt();
this.maxDiscountAmount = coupon.getMaxDiscountAmount();
this.minPaymentAmount = coupon.getMinPaymentAmount();
}

}
29 changes: 23 additions & 6 deletions src/main/java/com/feelmycode/parabole/service/CouponService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.feelmycode.parabole.enumtype.CouponType;
import com.feelmycode.parabole.enumtype.CouponUseState;
import com.feelmycode.parabole.global.error.exception.NoDataException;
import com.feelmycode.parabole.global.error.exception.NotSellerException;
import com.feelmycode.parabole.global.error.exception.ParaboleException;
import com.feelmycode.parabole.repository.CouponRepository;
import com.feelmycode.parabole.repository.SellerRepository;
Expand Down Expand Up @@ -46,19 +47,19 @@ public class CouponService {
private final UserCouponRepository userCouponRepository;

@Transactional
public CouponCreateResponseDto addCoupon(Long userId, @NotNull CouponCreateRequestDto dto) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new NoDataException());
public CouponCreateResponseDto addCoupon(Long sellerId, @NotNull CouponCreateRequestDto dto) {

Coupon coupon = new Coupon(dto.getName(), user.getSeller(), CouponType.returnNameToValue(dto.getType()), dto.getDiscountValue(), dto.getValidAt(),
dto.getExpiresAt(), dto.getMaxDiscountAmount(), dto.getMinPaymentAmount(), dto.getDetail(), dto.getCnt());
Seller seller = sellerRepository.findById(sellerId).orElseThrow(() -> new NotSellerException());

Coupon coupon = new Coupon(dto.getName(), seller, CouponType.returnNameToValue(dto.getType()), dto.getDiscountValue(), dto.getValidAt(),
dto.getExpiresAt(), dto.getDetail(), dto.getCnt());

couponRepository.save(coupon);

for (int i = 0; i < dto.getCnt(); i++) {
coupon.addUserCoupon(new UserCoupon(coupon));
}
return new CouponCreateResponseDto(coupon.getName(), user.getUsername(), coupon.getType().getName(), coupon.getCnt());
return new CouponCreateResponseDto(coupon.getName(), seller.getStoreName(), coupon.getType().getName(), coupon.getCnt());
}

// public void giveoutUserCoupon(String couponSNo, Long userId) {
Expand All @@ -72,6 +73,22 @@ public CouponCreateResponseDto addCoupon(Long userId, @NotNull CouponCreateReque
// userCoupon.setUser(user);
// }

@Transactional
public Boolean setCouponStock(Long couponId, Integer stock) {
Coupon getCoupon = this.getCouponById(couponId);
try {
if (stock < 0) {
getCoupon.setCouponForEvent(stock * -1);
} else {
getCoupon.cancelCouponEvent(stock);
}
couponRepository.save(getCoupon);
} catch (Exception e) {
log.error(e.getMessage());
}
return true;
}

public Coupon getCouponById(Long couponId) {
return couponRepository.findById(couponId).orElseThrow(() -> new NoDataException());
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/feelmycode/parabole/service/ProductService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.feelmycode.parabole.repository.ProductRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
Expand All @@ -19,6 +20,7 @@
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
@Slf4j
public class ProductService {

private final ProductRepository productRepository;
Expand Down Expand Up @@ -48,6 +50,22 @@ public Long updateProduct(Long userId, Product product) {
return product.getId();
}

@Transactional
public Boolean setProductRemains(Long productId, Long stock) {
Product getProduct = this.getProduct(productId);
try {
if (stock < 0) {
getProduct.removeRemains(stock * -1);
} else {
getProduct.addRemains(stock);
}
productRepository.save(getProduct);
} catch (Exception e) {
log.error(e.getMessage());
}
return true;
};

public Product getProduct(Long productId) {
return productRepository.findById(productId)
.orElseThrow(() -> new ParaboleException(HttpStatus.BAD_REQUEST, "상품이 존재하지 않습니다."));
Expand Down
Loading

0 comments on commit 8ed8dec

Please sign in to comment.