-
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
1 parent
297ab35
commit 3bd4bd7
Showing
8 changed files
with
185 additions
and
38 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
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
19 changes: 19 additions & 0 deletions
19
src/main/java/com/mallang/blog/query/BlogQueryService.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.mallang.blog.query; | ||
|
||
import com.mallang.blog.query.dao.BlogDao; | ||
import com.mallang.blog.query.response.BlogResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
@Service | ||
public class BlogQueryService { | ||
|
||
private final BlogDao blogDao; | ||
|
||
public BlogResponse findByName(String blogName) { | ||
return blogDao.find(blogName); | ||
} | ||
} |
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.mallang.blog.query.dao; | ||
|
||
import com.mallang.blog.query.response.BlogResponse; | ||
import com.mallang.blog.query.support.BlogQuerySupport; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
@Component | ||
public class BlogDao { | ||
|
||
private final BlogQuerySupport blogQuerySupport; | ||
|
||
public BlogResponse find(String blogName) { | ||
return BlogResponse.from(blogQuerySupport.getWithOwnerByName(blogName)); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/mallang/blog/query/response/BlogResponse.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.mallang.blog.query.response; | ||
|
||
import com.mallang.auth.domain.Member; | ||
import com.mallang.blog.domain.Blog; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record BlogResponse( | ||
Long id, | ||
String name, | ||
OwnerResponse owner | ||
) { | ||
|
||
public static BlogResponse from(Blog blog) { | ||
return BlogResponse.builder() | ||
.id(blog.getId()) | ||
.name(blog.getName()) | ||
.owner(OwnerResponse.from(blog.getOwner())) | ||
.build(); | ||
} | ||
|
||
@Builder | ||
public record OwnerResponse( | ||
Long memberId, | ||
String nickname, | ||
String profileImageUrl | ||
) { | ||
public static OwnerResponse from(Member owner) { | ||
return OwnerResponse.builder() | ||
.memberId(owner.getId()) | ||
.nickname(owner.getNickname()) | ||
.profileImageUrl(owner.getProfileImageUrl()) | ||
.build(); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/mallang/blog/query/support/BlogQuerySupport.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,17 @@ | ||
package com.mallang.blog.query.support; | ||
|
||
import com.mallang.blog.domain.Blog; | ||
import com.mallang.blog.exception.NotFoundBlogException; | ||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
public interface BlogQuerySupport extends JpaRepository<Blog, Long> { | ||
|
||
default Blog getWithOwnerByName(String blogName) { | ||
return findWithOwnerByName(blogName).orElseThrow(NotFoundBlogException::new); | ||
} | ||
|
||
@Query("SELECT b FROM Blog b JOIN FETCH b.owner WHERE b.name.value = :blogName") | ||
Optional<Blog> findWithOwnerByName(String blogName); | ||
} |
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
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