-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathPointService.java
More file actions
57 lines (46 loc) · 2.06 KB
/
PointService.java
File metadata and controls
57 lines (46 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.loopers.domain.point;
import com.loopers.support.error.CoreException;
import com.loopers.support.error.ErrorType;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@RequiredArgsConstructor
@Component
public class PointService {
private final PointRepository pointRepository;
@Transactional(readOnly = true)
public Point findPointByUserId(String userId) {
return pointRepository.findByUserId(userId).orElse(null);
}
@Transactional
public Point initPoint(String userId) {
return pointRepository.findByUserId(userId)
.orElseGet(() -> pointRepository.save(Point.create(userId, 0L)));
}
@Transactional
public Point chargePoint(String userId, Long chargeAmount) {
Point point = pointRepository.findByUserId(userId).orElseThrow(() -> new CoreException(ErrorType.NOT_FOUND, "포인트를 충전할수 없는 사용자입니다."));
point.charge(chargeAmount);
return pointRepository.save(point);
}
@Transactional
public Point usePoint(String userId, Long useAmount) {
Point point = pointRepository.findByUserId(userId)
.orElseThrow(() -> new CoreException(ErrorType.NOT_FOUND, "포인트 정보를 찾을 수 없습니다."));
if (useAmount == null || useAmount <= 0) {
throw new CoreException(ErrorType.BAD_REQUEST, "차감할 포인트는 1 이상이어야 합니다.");
}
if (point.getBalance() < useAmount) {
throw new CoreException(ErrorType.BAD_REQUEST, "포인트가 부족합니다.");
}
point.use(useAmount);
return pointRepository.save(point);
}
@Transactional
public void refundPoint(String userId, Long amount) {
Point point = pointRepository.findByUserId(userId)
.orElseThrow(() -> new CoreException(ErrorType.NOT_FOUND, "포인트 정보를 찾을 수 없습니다."));
point.refund(amount);
pointRepository.save(point);
}
}