-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPointFacade.java
More file actions
68 lines (59 loc) · 3.02 KB
/
PointFacade.java
File metadata and controls
68 lines (59 loc) · 3.02 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
58
59
60
61
62
63
64
65
66
67
68
package org.festimate.team.api.facade;
import lombok.RequiredArgsConstructor;
import org.festimate.team.api.point.dto.PointHistoryResponse;
import org.festimate.team.api.point.dto.RechargePointRequest;
import org.festimate.team.domain.festival.entity.Festival;
import org.festimate.team.domain.festival.service.FestivalService;
import org.festimate.team.domain.participant.entity.Participant;
import org.festimate.team.domain.participant.service.ParticipantService;
import org.festimate.team.domain.point.entity.TransactionType;
import org.festimate.team.domain.point.service.PointService;
import org.festimate.team.domain.user.entity.User;
import org.festimate.team.domain.user.service.UserService;
import org.festimate.team.global.exception.FestimateException;
import org.festimate.team.global.response.ResponseError;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@Component
@RequiredArgsConstructor
public class PointFacade {
private final UserService userService;
private final FestivalService festivalService;
private final ParticipantService participantService;
private final PointService pointService;
@Transactional(readOnly = true)
public PointHistoryResponse getMyPointHistory(Long userId, Long festivalId) {
User user = userService.getUserByIdOrThrow(userId);
Festival festival = festivalService.getFestivalByIdOrThrow(festivalId);
Participant participant = participantService.getParticipantOrThrow(user, festival);
return pointService.getPointHistory(participant);
}
@Transactional(readOnly = true)
public PointHistoryResponse getParticipantPointHistory(Long userId, Long festivalId, Long participantId) {
User user = userService.getUserByIdOrThrow(userId);
Festival festival = festivalService.getFestivalByIdOrThrow(festivalId);
isHost(user, festival);
Participant participant = participantService.getParticipantById(participantId);
return pointService.getPointHistory(participant);
}
@Transactional
public void rechargePoints(Long userId, Long festivalId, RechargePointRequest request) {
User user = userService.getUserByIdOrThrow(userId);
Festival festival = festivalService.getFestivalByIdOrThrow(festivalId);
isHost(user, festival);
Participant participant = participantService.getParticipantById(request.participantId());
if (participant.getFestival() != festival) {
throw new FestimateException(ResponseError.FORBIDDEN_RESOURCE);
}
if(request.type().equals(TransactionType.CREDIT)) {
pointService.rechargePoint(participant, request.point());
}else if (request.type().equals(TransactionType.DEBIT)) {
pointService.dischargePoint(participant, request.point());
}
}
private void isHost(User user, Festival festival) {
if (!festivalService.isHost(user, festival)) {
throw new FestimateException(ResponseError.FORBIDDEN_RESOURCE);
}
}
}