Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] 코스 등록시 Point 추가 로직 구현 - #90 #91

Merged
merged 3 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.dateroad.course.service.CourseService;
import org.dateroad.date.domain.Course;
import org.dateroad.date.dto.response.CourseGetDetailRes;
import org.dateroad.point.domain.TransactionType;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -62,6 +63,7 @@ public ResponseEntity<CourseCreateRes> createCourse(
Course course = courseService.createCourse(userId, courseCreateReq, places, images);
asyncService.createCoursePlace(places, course);
asyncService.createCourseTags(tags, course);
asyncService.publishEvenUserPoint(userId, PointUseReq.of(100, TransactionType.POINT_GAINED,"코스 생성"));
return ResponseEntity.status(
HttpStatus.CREATED
).body(CourseCreateRes.of(course.getId()));
Expand Down Expand Up @@ -98,7 +100,7 @@ public ResponseEntity<Void> createCourseLike(@UserId final Long userId,
}

@DeleteMapping("/{courseId}/likes")
public ResponseEntity<Void> deleteCourseLike(@RequestHeader final Long userId,
public ResponseEntity<Void> deleteCourseLike(@UserId final Long userId,
@PathVariable final Long courseId) {
courseService.deleteCourseLike(userId, courseId);
return ResponseEntity.ok().build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package org.dateroad.course.dto.request;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.dateroad.point.domain.TransactionType;

@Getter
@Builder(access = AccessLevel.PROTECTED)
public record PointUseReq(
int point,
TransactionType type,
String description
) {
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class PointUseReq {
@Builder.Default
private final int point = 100;
@Builder.Default
private final TransactionType type = TransactionType.POINT_GAINED;
@Builder.Default
private final String description = "포인트획득";

public static PointUseReq of(int point, TransactionType type, String description) {
return PointUseReq.builder()
.point(point)
Expand Down
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
Loading