-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
54 changed files
with
1,327 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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
33 changes: 33 additions & 0 deletions
33
src/main/java/com/join/core/bookmark/controller/BookmarkReadController.java
This file contains 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,33 @@ | ||
package com.join.core.bookmark.controller; | ||
|
||
import com.join.core.auth.domain.UserPrincipal; | ||
import com.join.core.bookmark.controller.specification.BookmarkReadApiSpecification; | ||
import com.join.core.bookmark.dto.response.BookmarkStudyReadResponse; | ||
import com.join.core.bookmark.service.BookmarkReadService; | ||
import com.join.core.common.dto.PageParameterRequest; | ||
import com.join.core.common.response.ApiResponse; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("${api.prefix}/bookmarks") | ||
public class BookmarkReadController implements BookmarkReadApiSpecification { | ||
|
||
private final BookmarkReadService bookmarkReadService; | ||
|
||
@GetMapping | ||
@PreAuthorize("isAuthenticated()") | ||
public ApiResponse<Page<BookmarkStudyReadResponse>> getBookmarkStudy( | ||
@AuthenticationPrincipal UserPrincipal principal, | ||
@Valid PageParameterRequest pageParameterRequest | ||
) { | ||
return ApiResponse.ok(bookmarkReadService.getBookmarkStudy(principal.getAvatarId(), pageParameterRequest)); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...in/java/com/join/core/bookmark/controller/specification/BookmarkReadApiSpecification.java
This file contains 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,23 @@ | ||
package com.join.core.bookmark.controller.specification; | ||
|
||
import com.join.core.auth.domain.UserPrincipal; | ||
import com.join.core.bookmark.dto.response.BookmarkStudyReadResponse; | ||
import com.join.core.common.dto.PageParameterRequest; | ||
import com.join.core.common.response.ApiResponse; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.security.SecurityRequirement; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
|
||
public interface BookmarkReadApiSpecification { | ||
|
||
@Tag(name = "${swagger.tag.bookmark}") | ||
@Operation(summary = "북마크한 스터디 조회 - 인증 필수", | ||
description = "북마크한 스터디 조회", | ||
security = {@SecurityRequirement(name = "session-token")}) | ||
ApiResponse<Page<BookmarkStudyReadResponse>> getBookmarkStudy( | ||
@AuthenticationPrincipal UserPrincipal principal, | ||
PageParameterRequest pageParameterRequest | ||
); | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/join/core/bookmark/dto/response/AvatarRatingResponse.java
This file contains 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,4 @@ | ||
package com.join.core.bookmark.dto.response; | ||
|
||
public record AvatarRatingResponse(String nickname, double totalRating) { | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/join/core/bookmark/dto/response/BookmarkStudyReadResponse.java
This file contains 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,11 @@ | ||
package com.join.core.bookmark.dto.response; | ||
|
||
public record BookmarkStudyReadResponse( | ||
String studyToken, | ||
String title, | ||
boolean isBookmark, | ||
int viewCount, | ||
AvatarRatingResponse leader, | ||
double memberAverage | ||
) { | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/join/core/bookmark/mapper/BookmarkMapper.java
This file contains 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,25 @@ | ||
package com.join.core.bookmark.mapper; | ||
|
||
import com.join.core.avatar.domain.Avatar; | ||
import com.join.core.bookmark.dto.response.AvatarRatingResponse; | ||
import com.join.core.bookmark.dto.response.BookmarkStudyReadResponse; | ||
import com.join.core.study.domain.Study; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class BookmarkMapper { | ||
|
||
public BookmarkStudyReadResponse toBookmarkStudyReadResponse(Study study, Avatar studyLeader, double memberAverage, boolean isBookmark) { | ||
return new BookmarkStudyReadResponse( | ||
study.getStudyToken(), | ||
study.getTitle(), | ||
isBookmark, | ||
study.getViewCnt(), | ||
new AvatarRatingResponse( | ||
studyLeader.getNickname(), | ||
studyLeader.getTotalRating() | ||
), | ||
memberAverage | ||
); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/join/core/bookmark/repository/BookmarkReaderImpl.java
This file contains 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,27 @@ | ||
package com.join.core.bookmark.repository; | ||
|
||
import com.join.core.avatar.domain.Avatar; | ||
import com.join.core.bookmark.domain.Bookmark; | ||
import com.join.core.bookmark.service.BookmarkReader; | ||
import com.join.core.study.domain.Study; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Component; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class BookmarkReaderImpl implements BookmarkReader { | ||
|
||
private final BookmarkRepository bookmarkRepository; | ||
|
||
@Override | ||
public Page<Bookmark> getBookmarksByAvatar(Pageable pageable, Avatar avatar) { | ||
return bookmarkRepository.findAllByAvatar(pageable, avatar); | ||
} | ||
|
||
@Override | ||
public boolean isBookmark(Study study, Avatar avatar) { | ||
return bookmarkRepository.existsByAvatarAndStudy(avatar, study); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/join/core/bookmark/repository/BookmarkRepository.java
This file contains 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,14 @@ | ||
package com.join.core.bookmark.repository; | ||
|
||
import com.join.core.avatar.domain.Avatar; | ||
import com.join.core.bookmark.domain.Bookmark; | ||
import com.join.core.study.domain.Study; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface BookmarkRepository extends JpaRepository<Bookmark, Long> { | ||
|
||
Page<Bookmark> findAllByAvatar(Pageable pageable, Avatar avatar); | ||
boolean existsByAvatarAndStudy(Avatar avatar, Study study); | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/join/core/bookmark/service/BookmarkReadService.java
This file contains 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,36 @@ | ||
package com.join.core.bookmark.service; | ||
|
||
import com.join.core.avatar.domain.Avatar; | ||
import com.join.core.avatar.domain.AvatarReader; | ||
import com.join.core.bookmark.dto.response.BookmarkStudyReadResponse; | ||
import com.join.core.bookmark.mapper.BookmarkMapper; | ||
import com.join.core.enrollment.service.EnrollmentReader; | ||
import com.join.core.study.domain.Study; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class BookmarkReadService { | ||
|
||
private final BookmarkReader bookmarkReader; | ||
private final AvatarReader avatarReader; | ||
private final EnrollmentReader enrollmentReader; | ||
private final BookmarkMapper bookmarkMapper; | ||
|
||
@Transactional(readOnly = true) | ||
public Page<BookmarkStudyReadResponse> getBookmarkStudy(Long avatarId, Pageable pageable) { | ||
Avatar avatar = avatarReader.getAvatarById(avatarId); | ||
return bookmarkReader.getBookmarksByAvatar(pageable, avatar) | ||
.map(bookmark -> { | ||
Study study = bookmark.getStudy(); | ||
boolean isBookmark = bookmarkReader.isBookmark(study, avatar); | ||
double averageRating = enrollmentReader.getAverageByStudyId(study.getId()); | ||
Avatar leader = enrollmentReader.getLeaderByStudyId(study.getId()); | ||
return bookmarkMapper.toBookmarkStudyReadResponse(study, leader, averageRating, isBookmark); | ||
}); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/join/core/bookmark/service/BookmarkReader.java
This file contains 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,13 @@ | ||
package com.join.core.bookmark.service; | ||
|
||
import com.join.core.avatar.domain.Avatar; | ||
import com.join.core.bookmark.domain.Bookmark; | ||
import com.join.core.study.domain.Study; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
public interface BookmarkReader { | ||
|
||
Page<Bookmark> getBookmarksByAvatar(Pageable pageable, Avatar avatar); | ||
boolean isBookmark(Study study, Avatar avatar); | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/join/core/common/config/jpa/JPAConfiguration.java
This file contains 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,19 @@ | ||
package com.join.core.common.config.jpa; | ||
|
||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class JPAConfiguration { | ||
|
||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
@Bean | ||
public JPAQueryFactory jpaQueryFactory() { | ||
return new JPAQueryFactory(entityManager); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/main/java/com/join/core/common/dto/PageParameterRequest.java
This file contains 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,72 @@ | ||
package com.join.core.common.dto; | ||
|
||
import jakarta.validation.constraints.Min; | ||
import lombok.Getter; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Sort; | ||
|
||
import java.util.Objects; | ||
|
||
@Getter | ||
public class PageParameterRequest implements Pageable { | ||
|
||
@Min(1) | ||
private final Integer page; | ||
@Min(1) | ||
private final Integer size; | ||
private final Sort sort; | ||
|
||
public PageParameterRequest(Integer page, Integer size, Sort sort) { | ||
this.page = Objects.requireNonNullElse(page, 1); | ||
this.size = Objects.requireNonNullElse(size, 20); | ||
this.sort = Objects.requireNonNullElse(sort, Sort.unsorted()); | ||
} | ||
|
||
@Override | ||
public int getPageNumber() { | ||
return page - 1; | ||
} | ||
|
||
@Override | ||
public int getPageSize() { | ||
return size; | ||
} | ||
|
||
@Override | ||
public long getOffset() { | ||
return (long) (page - 1) * size; | ||
} | ||
|
||
@Override | ||
public Sort getSort() { | ||
return sort; | ||
} | ||
|
||
@Override | ||
public Pageable next() { | ||
return new PageParameterRequest(page + 1, size, sort); | ||
} | ||
|
||
@Override | ||
public Pageable previousOrFirst() { | ||
if (hasPrevious()) { | ||
return new PageParameterRequest(page - 1, size, sort); | ||
} | ||
return first(); | ||
} | ||
|
||
@Override | ||
public Pageable first() { | ||
return new PageParameterRequest(1, size, sort); | ||
} | ||
|
||
@Override | ||
public Pageable withPage(int pageNumber) { | ||
return new PageParameterRequest(pageNumber, this.getPageSize(), this.getSort()); | ||
} | ||
|
||
@Override | ||
public boolean hasPrevious() { | ||
return page > 1; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/join/core/enrollment/repository/EnrollmentQueryRepository.java
This file contains 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,9 @@ | ||
package com.join.core.enrollment.repository; | ||
|
||
import com.join.core.avatar.domain.Avatar; | ||
|
||
public interface EnrollmentQueryRepository { | ||
|
||
Double getMemberAverageByStudyId(Long studyId); | ||
Avatar getLeaderByStudyId(Long studyId); | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/join/core/enrollment/repository/EnrollmentQueryRepositoryImpl.java
This file contains 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,43 @@ | ||
package com.join.core.enrollment.repository; | ||
|
||
import com.join.core.avatar.domain.Avatar; | ||
import com.join.core.enrollment.constant.EnrollmentStatus; | ||
import com.join.core.enrollment.constant.StudyRole; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import static com.join.core.enrollment.domain.QEnrollment.enrollment; | ||
|
||
@RequiredArgsConstructor | ||
@Repository | ||
public class EnrollmentQueryRepositoryImpl implements EnrollmentQueryRepository { | ||
|
||
private final JPAQueryFactory queryFactory; | ||
|
||
@Override | ||
public Double getMemberAverageByStudyId(Long studyId) { | ||
return queryFactory | ||
.select(enrollment.avatar.totalRating.avg().coalesce(0.0)) | ||
.from(enrollment) | ||
.where( | ||
enrollment.study.id.eq(studyId), | ||
enrollment.role.eq(StudyRole.MEMBER), | ||
enrollment.status.eq(EnrollmentStatus.JOINED) | ||
) | ||
.fetchOne(); | ||
} | ||
|
||
@Override | ||
public Avatar getLeaderByStudyId(Long studyId) { | ||
return queryFactory | ||
.select(enrollment.avatar) | ||
.from(enrollment) | ||
.where( | ||
enrollment.study.id.eq(studyId), | ||
enrollment.role.eq(StudyRole.LEADER), | ||
enrollment.status.eq(EnrollmentStatus.JOINED) | ||
) | ||
.fetchOne(); | ||
} | ||
} |
This file contains 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
4 changes: 4 additions & 0 deletions
4
src/main/java/com/join/core/enrollment/service/EnrollmentReader.java
This file contains 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 |
---|---|---|
@@ -1,6 +1,10 @@ | ||
package com.join.core.enrollment.service; | ||
|
||
import com.join.core.avatar.domain.Avatar; | ||
|
||
public interface EnrollmentReader { | ||
|
||
double getAverageByStudyId(Long studyId); | ||
Avatar getLeaderByStudyId(Long studyId); | ||
boolean existEnrollmentByAvatarIdAndStudyId(Long avatarId, Long studyId); | ||
} |
Oops, something went wrong.