Skip to content

Commit

Permalink
[MERGE] Transcation Only one - #307
Browse files Browse the repository at this point in the history
[REFACTOR] Transcation Only one - #307
  • Loading branch information
rlarlgnszx authored Oct 22, 2024
2 parents 792b27c + 0c5bf9a commit 355c041
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.dateroad.course.dto;

import java.util.List;
import lombok.Builder;
import org.dateroad.place.domain.CoursePlace;
import org.dateroad.tag.domain.CourseTag;


@Builder
public record CourseWithPlacesAndTagsDto(List<CoursePlace> coursePlaces, List<CourseTag> courseTags) {
public static CourseWithPlacesAndTagsDto of(List<CoursePlace> coursePlaces, List<CourseTag> courseTags) {
return CourseWithPlacesAndTagsDto.builder().coursePlaces(coursePlaces).courseTags(courseTags).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dateroad.code.FailureCode;
import org.dateroad.course.dto.CourseWithPlacesAndTagsDto;
import org.dateroad.course.dto.request.CourseCreateEvent;
import org.dateroad.course.dto.request.PointUseReq;
import org.dateroad.date.domain.Course;
import org.dateroad.exception.DateRoadException;
import org.dateroad.image.domain.Image;
import org.dateroad.image.service.ImageService;
import org.dateroad.place.domain.CoursePlace;
import org.dateroad.point.event.MessageDto.FreeMessageDTO;
import org.dateroad.point.event.MessageDto.PointMessageDTO;
import org.dateroad.tag.domain.CourseTag;
import org.springframework.dao.QueryTimeoutException;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.core.RedisTemplate;
Expand Down Expand Up @@ -64,11 +67,13 @@ public void publishEventUserFree(final Long userId) {
}

@Transactional
public void runAsyncTasks(CourseCreateEvent event) {
public CourseWithPlacesAndTagsDto runAsyncTasks(CourseCreateEvent event) {
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
CompletableFuture<Void> placeFuture = CompletableFuture.runAsync(() -> coursePlaceService.createCoursePlace(event.getPlaces(), event.getCourse()), executor);
CompletableFuture<Void> tagFuture = CompletableFuture.runAsync(() -> courseTagService.createCourseTags(event.getTags(), event.getCourse()),executor);
CompletableFuture.allOf(placeFuture, tagFuture).join();
CompletableFuture<List<CoursePlace>> placeFuture = CompletableFuture.supplyAsync(() -> coursePlaceService.createCoursePlace(event.getPlaces(), event.getCourse()), executor);
CompletableFuture<List<CourseTag>> tagFuture = CompletableFuture.supplyAsync(() -> courseTagService.createCourseTags(event.getTags(), event.getCourse()), executor);
CompletableFuture<Void> allFutures = CompletableFuture.allOf(placeFuture, tagFuture);
allFutures.join();
return CourseWithPlacesAndTagsDto.of(placeFuture.join(), tagFuture.join());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,19 @@
import org.dateroad.course.dto.request.CoursePlaceGetReq;
import org.dateroad.date.domain.Course;
import org.dateroad.place.domain.CoursePlace;
import org.dateroad.place.repository.CoursePlaceRepository;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public class CoursePlaceService {
private final CoursePlaceRepository coursePlaceRepository;

public float findTotalDurationByCourseId(final Long id) {
return coursePlaceRepository.findTotalDurationByCourseId(id);
}

public void createCoursePlace(final List<CoursePlaceGetReq> places, final Course course) {
List<CoursePlace> coursePlaces = places.stream()
public List<CoursePlace> createCoursePlace(final List<CoursePlaceGetReq> places, final Course course) {
return places.stream()
.map(placeReq -> CoursePlace.create(
placeReq.getTitle(),
placeReq.getDuration(),
course,
placeReq.getSequence()
))
.toList();
coursePlaceRepository.saveAll(coursePlaces);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import lombok.RequiredArgsConstructor;
import org.dateroad.code.FailureCode;
import org.dateroad.common.Constants;
import org.dateroad.course.dto.CourseWithPlacesAndTagsDto;
import org.dateroad.course.dto.request.CourseCreateEvent;
import org.dateroad.course.dto.request.CourseCreateReq;
import org.dateroad.course.dto.request.CourseGetAllReq;
Expand Down Expand Up @@ -43,7 +44,6 @@
import org.dateroad.user.repository.UserRepository;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
Expand All @@ -66,7 +66,6 @@ public class CourseService {
private final ImageRepository imageRepository;
private final CoursePlaceRepository coursePlaceRepository;
private final CourseTagRepository courseTagRepository;
private final ApplicationEventPublisher eventPublisher;
private final CourseRollbackService courseRollbackService;

@Cacheable(value = "courses", key = "#courseGetAllReq")
Expand Down Expand Up @@ -204,21 +203,18 @@ public CourseCreateRes createCourse(final Long userId, final CourseCreateReq cou
totalTime
);
Course newcourse = courseRepository.save(course);
eventPublisher.publishEvent(CourseCreateEvent.of(newcourse, places, tags));
String thumbnail = asyncService.createCourseImages(images, newcourse);// 썸
CourseWithPlacesAndTagsDto courseWithPlacesAndTagsDto = asyncService.runAsyncTasks(CourseCreateEvent.of(newcourse, places, tags));
String thumbnail = asyncService.createCourseImages(images, newcourse);
course.setThumbnail(thumbnail);
courseRepository.save(newcourse); // 최종적으로 썸네일을 반영하여 저장
courseRepository.save(newcourse);
coursePlaceRepository.saveAll(courseWithPlacesAndTagsDto.coursePlaces());
courseTagRepository.saveAll(courseWithPlacesAndTagsDto.courseTags());
RecordId recordId = asyncService.publishEvenUserPoint(userId, PointUseReq.of(Constants.COURSE_CREATE_POINT, TransactionType.POINT_GAINED, "코스 등록하기"));
Long userCourseCount = courseRepository.countByUser(user);
courseRollbackService.rollbackCourse(recordId);
return CourseCreateRes.of(newcourse.getId(), user.getTotalPoint() + Constants.COURSE_CREATE_POINT, userCourseCount);
}

@TransactionalEventListener
public void handleCourseCreatedEvent(CourseCreateEvent event) {
asyncService.runAsyncTasks(event);
}

@Transactional
public DateAccessCreateRes openCourse(final Long userId, final Long courseId, final PointUseReq pointUseReq) {
Course course = getCourse(courseId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,14 @@
import org.dateroad.course.dto.request.TagCreateReq;
import org.dateroad.date.domain.Course;
import org.dateroad.tag.domain.CourseTag;
import org.dateroad.tag.repository.CourseTagRepository;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public class CourseTagService {
private final CourseTagRepository courseTagRepository;

public void createCourseTags(final List<TagCreateReq> tags, final Course course) {
List<CourseTag> coursePlaces = tags.stream()
public List<CourseTag> createCourseTags(final List<TagCreateReq> tags, final Course course) {
return tags.stream()
.map(tag -> CourseTag.create(course, tag.getTag()))
.toList();
courseTagRepository.saveAll(coursePlaces);
}
}

0 comments on commit 355c041

Please sign in to comment.