Skip to content

Commit f123552

Browse files
authored
Merge pull request #39 from DMU-NextLevel/funding-rollback
Funding rollback
2 parents f543f88 + a47ae1f commit f123552

File tree

14 files changed

+165
-25
lines changed

14 files changed

+165
-25
lines changed

src/main/java/NextLevel/demo/exception/CustomExceptionHandler.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.HashMap;
44
import java.util.Map;
5+
6+
import jakarta.validation.ConstraintViolationException;
57
import org.springframework.http.HttpStatus;
68
import org.springframework.http.ResponseEntity;
79
import org.springframework.web.HttpRequestMethodNotSupportedException;
@@ -26,6 +28,13 @@ public ResponseEntity<?> handleValidationException(MethodArgumentNotValidExcepti
2628
map.put("message", ErrorCode.INPUT_REQUIRED_PARAMETER.errorMessage);
2729
return ResponseEntity.status(ErrorCode.INPUT_REQUIRED_PARAMETER.statusCode).body(map);
2830
}
31+
@ExceptionHandler(ConstraintViolationException.class)
32+
public ResponseEntity<?> handleValidationException(ConstraintViolationException e) {
33+
Map<String, Object> map = new HashMap<>();
34+
map.put("code", ErrorCode.INPUT_REQUIRED_PARAMETER.CustomErrorCode);
35+
map.put("message", ErrorCode.INPUT_REQUIRED_PARAMETER.errorMessage);
36+
return ResponseEntity.status(ErrorCode.INPUT_REQUIRED_PARAMETER.statusCode).body(map);
37+
}
2938
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
3039
public ResponseEntity<?> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
3140
return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED)

src/main/java/NextLevel/demo/exception/ErrorCode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public enum ErrorCode {
3434

3535
// funding
3636
NOT_ENOUGH_POINT(HttpStatus.BAD_REQUEST, "05001","not enough point left:%s, need:%s"),
37+
ALREADY_USED_COUPON(HttpStatus.BAD_REQUEST, "05002","already used coupon"),
3738

3839
// option
3940

src/main/java/NextLevel/demo/funding/controller/CouponController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import NextLevel.demo.funding.dto.response.ResponseCouponDto;
66
import NextLevel.demo.funding.service.CouponService;
77
import NextLevel.demo.util.jwt.JWTUtil;
8+
import jakarta.validation.Valid;
89
import lombok.RequiredArgsConstructor;
910
import org.springframework.http.ResponseEntity;
1011
import org.springframework.stereotype.Controller;
@@ -21,7 +22,7 @@ public class CouponController {
2122
private final CouponService couponService;
2223

2324
@PostMapping("/admin/coupon/add")
24-
public ResponseEntity addCoupon(@RequestBody RequestAddCouponDto dto) {
25+
public ResponseEntity addCoupon(@RequestBody @Valid RequestAddCouponDto dto) {
2526
if(dto.getUserId()==null)
2627
dto.setUserId(JWTUtil.getUserIdFromSecurityContext());
2728
couponService.addCoupon(dto);

src/main/java/NextLevel/demo/funding/controller/FundingController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ public class FundingController {
2929
public ResponseEntity<?> funding(@RequestBody @Valid RequestFundingDto dto) {
3030
dto.setUserId(JWTUtil.getUserIdFromSecurityContext());
3131

32-
if(dto.getFree() != null)
33-
fundingService.freeFunding(dto.getFree());
3432
if(dto.getOption() != null)
3533
fundingService.optionFunding(dto.getOption());
34+
if(dto.getFree() != null)
35+
fundingService.freeFunding(dto.getFree());
3636

3737
return ResponseEntity.ok().body(new SuccessResponse("success", null));
3838
}

src/main/java/NextLevel/demo/funding/dto/request/RequestFreeFundingDto.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import NextLevel.demo.funding.entity.FreeFundingEntity;
44
import NextLevel.demo.project.project.entity.ProjectEntity;
55
import NextLevel.demo.user.entity.UserEntity;
6+
import com.fasterxml.jackson.annotation.JsonAlias;
7+
import com.fasterxml.jackson.annotation.JsonProperty;
8+
import jakarta.validation.constraints.NotNull;
69
import lombok.Getter;
710
import lombok.NoArgsConstructor;
811
import lombok.Setter;
@@ -12,7 +15,10 @@
1215
@NoArgsConstructor
1316
public class RequestFreeFundingDto {
1417

18+
@JsonAlias("price")
19+
@NotNull
1520
private Long freePrice;
21+
@NotNull
1622
private Long projectId;
1723

1824
private Long userId;

src/main/java/NextLevel/demo/funding/entity/FreeFundingEntity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ public class FreeFundingEntity {
2828
@JoinColumn(name = "project_id")
2929
private ProjectEntity project;
3030

31+
public void updatePrice(long price) {this.price += price;}
32+
3133
}

src/main/java/NextLevel/demo/funding/entity/OptionFundingEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class OptionFundingEntity extends BasedEntity {
3636
@Column
3737
private long count;
3838

39-
public void updateCount(int count) {
39+
public void updateCount(long count) {
4040
this.count += count;
4141
}
4242

src/main/java/NextLevel/demo/funding/repository/FreeFundingRepository.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import org.springframework.data.jpa.repository.Query;
66
import org.springframework.data.repository.query.Param;
77

8+
import java.util.List;
9+
import java.util.Optional;
10+
811
public interface FreeFundingRepository extends JpaRepository<FreeFundingEntity, Long> {
912

1013
@Query("select sum(f.price) from FreeFundingEntity f where f.project.id = :projectId")
@@ -13,4 +16,14 @@ public interface FreeFundingRepository extends JpaRepository<FreeFundingEntity,
1316
@Query("select count(ff) from FreeFundingEntity ff where ff.project.id = :projectId")
1417
Long getFundingCount(@Param("projectId") Long projectId);
1518

19+
@Query("select ff from FreeFundingEntity ff where ff.user.id = :userId and ff.project.id = :projectId")
20+
Optional<FreeFundingEntity> findByProjectIdAndUserId(@Param("projectId") long projectId, @Param("userId") long userId);
21+
22+
//for rollback funding
23+
@Query("select ff from FreeFundingEntity ff left join fetch ff.user where ff.project.id = :projectId")
24+
List<FreeFundingEntity> findAllWithUserByProject(@Param("projectId") Long projectId);
25+
26+
@Query("select ff from FreeFundingEntity ff where ff.user.id = :userId")
27+
List<FreeFundingEntity> findAllByUser(@Param("userId") Long userId);
28+
1629
}

src/main/java/NextLevel/demo/funding/repository/OptionFundingRepository.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import org.springframework.data.repository.query.Param;
77
import org.springframework.stereotype.Repository;
88

9+
import java.util.List;
10+
import java.util.Optional;
11+
912
@Repository
1013
public interface OptionFundingRepository extends JpaRepository<OptionFundingEntity, Long> {
1114

@@ -20,4 +23,16 @@ public interface OptionFundingRepository extends JpaRepository<OptionFundingEnti
2023
"where of.option.project.id = :projectId ")
2124
Long getTotalFundingCount(@Param("projectId") Long projectId);
2225

26+
@Query("select of from OptionFundingEntity of where of.option.id = :optionId and of.user.id = :userId")
27+
Optional<OptionFundingEntity> findByOptionIdAndUserId(@Param("optionId") long optionId, @Param("userId") long userId);
28+
29+
// for rollback funding
30+
@Query("select of from OptionFundingEntity of left join fetch of.user where of.option.id = :optionId")
31+
List<OptionFundingEntity> findAllWithUserByOption(@Param("optionId") Long optionId);
32+
33+
@Query("select of from OptionFundingEntity of left join fetch of.user where of.option.project.id = :projectId")
34+
List<OptionFundingEntity> findAllWithUserByProject(@Param("projectId") Long projectId);
35+
36+
@Query("select of from OptionFundingEntity of where of.user.id = :userId")
37+
List<OptionFundingEntity> findAllByUser(@Param("userId") Long userId);
2338
}

src/main/java/NextLevel/demo/funding/service/CouponService.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,14 @@ public long useCoupon(long userId, long couponId, OptionFundingEntity optionFund
4040
if(!coupon.getUser().getId().equals(userId))
4141
throw new CustomException(ErrorCode.NOT_AUTHOR);
4242

43+
if(coupon.getOptionFunding() != null)
44+
throw new CustomException(ErrorCode.ALREADY_USED_COUPON);
45+
4346
price -= coupon.getPrice();
4447

4548
coupon.updateProjectFundingEntity(optionFunding);
4649

4750
return price>0?price:0;
4851
}
4952

50-
public long rollBackUseCoupon(CouponEntity coupon, long price) {
51-
price -= coupon.getPrice();
52-
coupon.rollBackUseCoupon();
53-
couponRepository.save(coupon);
54-
return price;
55-
}
56-
5753
}

0 commit comments

Comments
 (0)