Skip to content

Commit

Permalink
[feat] PointListner Type에 따라 로직 변경 - #90
Browse files Browse the repository at this point in the history
  • Loading branch information
rlarlgnszx committed Jul 12, 2024
1 parent 84d1f4b commit 898476f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ public void createCoursePlace(final List<CoursePlaceGetReq> places, final Course
coursePlaceService.createCoursePlace(places, course);
}

public void publishEvenUserPoint(User user, PointUseReq pointUseReq) {
public void publishEvenUserPoint(final Long userId, PointUseReq pointUseReq) {
Map<String, Object> fieldMap = new HashMap<>();
fieldMap.put("userId", user.getId().toString());
fieldMap.put("point", Integer.toString(pointUseReq.point()));
fieldMap.put("type", pointUseReq.type().toString());
fieldMap.put("userId", userId.toString());
fieldMap.put("point", Integer.toString(pointUseReq.getPoint()));
fieldMap.put("type", pointUseReq.getType().toString());
redisTemplate.opsForStream().add("coursePoint", fieldMap);
}

public void publishEventUserFree(User user) {
public void publishEventUserFree(final Long userId) {
Map<String, Object> fieldMap = new HashMap<>();
fieldMap.put("userId", user.getId().toString());
fieldMap.put("userId", userId.toString());
redisTemplate.opsForStream().add("courseFree", fieldMap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.dateroad.course.dto.response.DateAccessGetAllRes;
import org.dateroad.course.facade.AsyncService;
import org.dateroad.date.domain.Course;
import org.dateroad.date.domain.Date;
import org.dateroad.date.dto.response.CourseGetDetailRes;
import org.dateroad.date.repository.CourseRepository;
import org.dateroad.dateAccess.domain.DateAccess;
Expand Down Expand Up @@ -194,9 +193,9 @@ public Course createCourse(final Long userId, final CourseCreateReq courseRegist
public void openCourse(final Long userId, final Long courseId, final PointUseReq pointUseReq) {
User user = getUser(userId);
Course course = getCourse(courseId);
Point point = Point.create(user, pointUseReq.point(), pointUseReq.type(), pointUseReq.description());
CoursePaymentType coursePaymentType = validateUserFreeOrPoint(user, pointUseReq.point());
processCoursePayment(coursePaymentType, user, point, pointUseReq);
Point point = Point.create(user, pointUseReq.getPoint(), pointUseReq.getType(), pointUseReq.getDescription());
CoursePaymentType coursePaymentType = validateUserFreeOrPoint(user, pointUseReq.getPoint());
processCoursePayment(coursePaymentType, userId, point, pointUseReq);
dateAccessRepository.save(DateAccess.create(course, user));
}

Expand All @@ -209,15 +208,15 @@ private CoursePaymentType validateUserFreeOrPoint(final User user, final int req
return CoursePaymentType.POINT;
}

public void processCoursePayment(final CoursePaymentType coursePaymentType, final User user, final Point point,
public void processCoursePayment(final CoursePaymentType coursePaymentType, final Long userId, final Point point,
final PointUseReq pointUseReq) {
switch (coursePaymentType) {
case FREE -> {
asyncService.publishEventUserFree(user);
asyncService.publishEventUserFree(userId);
}
case POINT -> {
pointRepository.save(point);
asyncService.publishEvenUserPoint(user, pointUseReq);
asyncService.publishEvenUserPoint(userId, pointUseReq);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.RequiredArgsConstructor;
import org.dateroad.code.FailureCode;
import org.dateroad.exception.DateRoadException;
import org.dateroad.point.domain.TransactionType;
import org.dateroad.user.domain.User;
import org.dateroad.user.repository.UserRepository;
import org.springframework.data.redis.connection.stream.MapRecord;
Expand All @@ -22,12 +23,23 @@ public class pointEventListener implements StreamListener<String, MapRecord<Stri
public void onMessage(final MapRecord<String, String, String> message) {
Map<String, String> map = message.getValue();
Long userId = Long.valueOf(map.get("userId"));
TransactionType type = TransactionType.valueOf(map.get("type"));
User user = getUser(userId);
int point = Integer.parseInt(map.get("point")); // 감소시킬 포인트
user.setTotalPoint(user.getTotalPoint() - point);
int point = Integer.parseInt(map.get("point"));
switch (type) {
case POINT_GAINED:
user.setTotalPoint(user.getTotalPoint() + point);
break;
case POINT_USED:
user.setTotalPoint(user.getTotalPoint() - point);
break;
default:
throw new IllegalArgumentException("잘못된 TransactionType: " + type);
}
userRepository.save(user);
}


private User getUser(Long userId) {
return userRepository.findById(userId).orElseThrow(
() -> new DateRoadException(FailureCode.USER_NOT_FOUND)
Expand Down

0 comments on commit 898476f

Please sign in to comment.