-
Notifications
You must be signed in to change notification settings - Fork 0
[Feat] 코스 좋아요 토글 API 추가 #157
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/main/java/com/semosan/api/domain/mountain/dto/response/CourseLikeToggleResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package com.semosan.api.domain.mountain.dto.response; | ||
|
|
||
| public record CourseLikeToggleResponse( | ||
| boolean liked | ||
| ) { | ||
| } |
58 changes: 58 additions & 0 deletions
58
src/main/java/com/semosan/api/domain/mountain/entity/CourseLike.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package com.semosan.api.domain.mountain.entity; | ||
|
|
||
| import com.semosan.api.common.base.BaseEntity; | ||
| import com.semosan.api.domain.user.entity.User; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.FetchType; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.Index; | ||
| import jakarta.persistence.JoinColumn; | ||
| import jakarta.persistence.ManyToOne; | ||
| import jakarta.persistence.Table; | ||
| import jakarta.persistence.UniqueConstraint; | ||
| import lombok.AccessLevel; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Table( | ||
| name = "course_likes", | ||
| uniqueConstraints = { | ||
| @UniqueConstraint( | ||
| name = "uk_course_likes_user_id_course_id", | ||
| columnNames = {"user_id", "course_id"} | ||
| ) | ||
| }, | ||
| indexes = { | ||
| @Index(name = "idx_course_likes_user_id_created_at", columnList = "user_id, created_at DESC") | ||
| } | ||
| ) | ||
| @Getter | ||
| @Entity | ||
| @Builder(access = AccessLevel.PROTECTED) | ||
| @AllArgsConstructor(access = AccessLevel.PROTECTED) | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| public class CourseLike extends BaseEntity { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "user_id", nullable = false) | ||
| private User user; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "course_id", nullable = false) | ||
| private Course course; | ||
|
|
||
| public static CourseLike create(User user, Course course) { | ||
| return CourseLike.builder() | ||
| .user(user) | ||
| .course(course) | ||
| .build(); | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/semosan/api/domain/mountain/repository/CourseLikeRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.semosan.api.domain.mountain.repository; | ||
|
|
||
| import com.semosan.api.domain.mountain.entity.CourseLike; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Modifying; | ||
| import org.springframework.data.jpa.repository.Query; | ||
| import org.springframework.data.repository.query.Param; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public interface CourseLikeRepository extends JpaRepository<CourseLike, Long> { | ||
|
|
||
| boolean existsByUser_IdAndCourse_Id(Long userId, Long courseId); | ||
|
|
||
| Optional<CourseLike> findByUser_IdAndCourse_Id(Long userId, Long courseId); | ||
|
|
||
| @Modifying(clearAutomatically = true) | ||
| @Query("DELETE FROM CourseLike cl WHERE cl.user.id = :userId") | ||
| void deleteByUser_Id(@Param("userId") Long userId); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/com/semosan/api/domain/mountain/service/CourseLikeService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package com.semosan.api.domain.mountain.service; | ||
|
|
||
| import com.semosan.api.common.exception.GeneralException; | ||
| import com.semosan.api.common.status.ErrorStatus; | ||
| import com.semosan.api.domain.mountain.dto.response.CourseLikeToggleResponse; | ||
| import com.semosan.api.domain.mountain.entity.Course; | ||
| import com.semosan.api.domain.mountain.entity.CourseLike; | ||
| import com.semosan.api.domain.mountain.repository.CourseLikeRepository; | ||
| import com.semosan.api.domain.mountain.repository.CourseRepository; | ||
| import com.semosan.api.domain.user.entity.User; | ||
| import com.semosan.api.domain.user.service.UserReader; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.dao.DataIntegrityViolationException; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class CourseLikeService { | ||
|
|
||
| private final CourseLikeRepository courseLikeRepository; | ||
| private final CourseRepository courseRepository; | ||
| private final UserReader userReader; | ||
|
|
||
| @Transactional(noRollbackFor = DataIntegrityViolationException.class) | ||
| public CourseLikeToggleResponse toggleCourseLike(Long userId, Long courseId) { | ||
| boolean liked = toggle(userId, courseId); | ||
| return new CourseLikeToggleResponse(liked); | ||
| } | ||
|
|
||
| private boolean toggle(Long userId, Long courseId) { | ||
| User user = userReader.findCompletedOnboardingUserById(userId); | ||
| Course course = findCourseById(courseId); | ||
|
|
||
| return courseLikeRepository.findByUser_IdAndCourse_Id(userId, courseId) | ||
| .map(existing -> { | ||
| courseLikeRepository.delete(existing); | ||
| return false; | ||
| }) | ||
| .orElseGet(() -> createCourseLike(user, course, userId, courseId)); | ||
| } | ||
|
|
||
| private boolean createCourseLike(User user, Course course, Long userId, Long courseId) { | ||
| try { | ||
| courseLikeRepository.save(CourseLike.create(user, course)); | ||
| return true; | ||
| } catch (DataIntegrityViolationException e) { | ||
| log.warn("CourseLike 동시 요청 감지: courseId={}, userId={}", courseId, userId); | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| private Course findCourseById(Long courseId) { | ||
| return courseRepository.findById(courseId) | ||
| .orElseThrow(() -> new GeneralException(ErrorStatus.COURSE_NOT_FOUND)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/resources/db/migration/V26__create_course_likes.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| CREATE TABLE IF NOT EXISTS course_likes | ||
| ( | ||
| id bigint generated by default as identity | ||
| primary key, | ||
| created_at timestamp(6) not null, | ||
| updated_at timestamp(6) not null, | ||
| user_id bigint not null | ||
| constraint fk_course_likes_user | ||
| references users, | ||
| course_id bigint not null | ||
| constraint fk_course_likes_course | ||
| references courses, | ||
| constraint uk_course_likes_user_id_course_id | ||
| unique (user_id, course_id) | ||
| ); | ||
|
|
||
| CREATE INDEX IF NOT EXISTS idx_course_likes_user_id_created_at | ||
| on course_likes (user_id asc, created_at desc); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.