Skip to content

Commit

Permalink
feat: 블로그 ID 대신 블로그 이름을 사용하도록 변경 (#102)
Browse files Browse the repository at this point in the history
* refactor: comment controller 인증과 비인증으로 분리

* [#100] refactor: 블로그 ID 대신 블로그 이름 사용하도록 변경
  • Loading branch information
shin-mallang authored Nov 25, 2023
1 parent 32a0b96 commit 111fa71
Show file tree
Hide file tree
Showing 70 changed files with 541 additions and 511 deletions.
10 changes: 5 additions & 5 deletions src/main/java/com/mallang/blog/application/AboutService.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ public class AboutService {

public Long write(WriteAboutCommand command) {
Member member = memberRepository.getById(command.memberId());
Blog blog = blogRepository.getByIdAndOwnerId(command.blogId(), command.memberId());
Blog blog = blogRepository.getByNameAndOwnerId(command.blogName(), command.memberId());
About about = command.toAbout(member, blog);
about.write(aboutValidator);
return aboutRepository.save(about)
.getId();
}

public void update(UpdateAboutCommand command) {
About about = aboutRepository.getByIdAndWriterIdAndBlogId(
command.aboutId(), command.memberId(), command.blogId()
About about = aboutRepository.getByIdAndWriterIdAndBlogName(
command.aboutId(), command.memberId(), command.blogName()
);
about.update(command.content());
}

public void delete(DeleteAboutCommand command) {
About about = aboutRepository.getByIdAndWriterIdAndBlogId(
command.aboutId(), command.memberId(), command.blogId()
About about = aboutRepository.getByIdAndWriterIdAndBlogName(
command.aboutId(), command.memberId(), command.blogName()
);
aboutRepository.delete(about);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ public class BlogSubscribeService {

public Long subscribe(BlogSubscribeCommand command) {
Member member = memberRepository.getById(command.subscriberId());
Blog blog = blogRepository.getById(command.blogId());
Blog blog = blogRepository.getByName(command.blogName());
BlogSubscribe blogSubscribe = new BlogSubscribe(member, blog);
blogSubscribe.subscribe(blogSubscribeValidator);
return blogSubscribeRepository.save(blogSubscribe).getId();
}

public void unsubscribe(BlogUnsubscribeCommand command) {
BlogSubscribe subscribe = blogSubscribeRepository
.findBySubscriberIdAndBlogId(command.subscriberId(), command.blogId())
.findBySubscriberIdAndBlogName(command.subscriberId(), command.blogName())
.orElseThrow(UnsubscribeUnsubscribedBlogException::new);
blogSubscribeRepository.delete(subscribe);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public record BlogSubscribeCommand(
Long subscriberId,
Long blogId
String blogName
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public record BlogUnsubscribeCommand(
Long subscriberId,
Long blogId
String blogName
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
public record DeleteAboutCommand(
Long aboutId,
Long memberId,
Long blogId
String blogName
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public record UpdateAboutCommand(
Long aboutId,
Long memberId,
Long blogId,
String blogName,
String content
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public record WriteAboutCommand(
Long memberId,
Long blogId,
String blogName,
String content
) {
public About toAbout(Member member, Blog blog) {
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/com/mallang/blog/domain/AboutRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@
import com.mallang.blog.exception.NotFoundAboutException;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface AboutRepository extends JpaRepository<About, Long> {

boolean existsByBlog(Blog blog);

default About getByIdAndWriterIdAndBlogId(Long aboutId, Long memberId, Long blogId) {
return findByIdAndWriterIdAndBlogId(aboutId, memberId, blogId)
default About getByIdAndWriterIdAndBlogName(Long aboutId, Long memberId, String blogName) {
return findByIdAndWriterIdAndBlogName(aboutId, memberId, blogName)
.orElseThrow(NotFoundAboutException::new);
}

Optional<About> findByIdAndWriterIdAndBlogId(Long aboutId, Long writerId, Long blogId);
@Query("SELECT a FROM About a WHERE a.id = :aboutId AND a.writer.id = :writerId AND a.blog.name.value = :blogName")
Optional<About> findByIdAndWriterIdAndBlogName(
@Param("aboutId") Long aboutId,
@Param("writerId") Long writerId,
@Param("blogName") String blogName
);
}
19 changes: 14 additions & 5 deletions src/main/java/com/mallang/blog/domain/BlogRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,29 @@
import com.mallang.blog.exception.NotFoundBlogException;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface BlogRepository extends JpaRepository<Blog, Long> {

default Blog getById(Long id) {
return findById(id).orElseThrow(NotFoundBlogException::new);
default Blog getByName(String blogName) {
return findByName(blogName).orElseThrow(NotFoundBlogException::new);
}

default Blog getByIdAndOwnerId(Long id, Long ownerId) {
return findByIdAndOwnerId(id, ownerId)
@Query("SELECT b FROM Blog b WHERE b.name.value = :blogName")
Optional<Blog> findByName(@Param("blogName") String blogName);

default Blog getByNameAndOwnerId(String blogName, Long ownerId) {
return findByNameAndOwnerId(blogName, ownerId)
.orElseThrow(() ->
new NotFoundBlogException("존재하지 않는 블로그거나, 해당 사용자의 블로그가 아닙니다."));
}

Optional<Blog> findByIdAndOwnerId(Long id, Long ownerId);
@Query("SELECT b FROM Blog b WHERE b.name.value = :blogName AND b.owner.id = :ownerId")
Optional<Blog> findByNameAndOwnerId(
@Param("blogName") String blogName,
@Param("ownerId") Long ownerId
);

boolean existsByName(BlogName name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.mallang.blog.exception.NotFoundBlogSubscribeException;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface BlogSubscribeRepository extends JpaRepository<BlogSubscribe, Long> {

Expand All @@ -13,7 +15,11 @@ default BlogSubscribe getById(Long id) {
return findById(id).orElseThrow(() -> new NotFoundBlogSubscribeException(id));
}

Optional<BlogSubscribe> findBySubscriberIdAndBlogId(Long subscriberId, Long blogId);
@Query("SELECT s FROM BlogSubscribe s WHERE s.subscriber.id = :subscriberId AND s.blog.name.value = :blogName")
Optional<BlogSubscribe> findBySubscriberIdAndBlogName(
@Param("subscriberId") Long subscriberId,
@Param("blogName") String blogName
);

boolean existsBySubscriberAndBlog(Member subscriber, Blog blog);
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public ResponseEntity<Void> delete(

@GetMapping
public ResponseEntity<AboutResponse> findByBlogName(
@RequestParam(name = "blogId") Long blogId
@RequestParam(name = "blogName") String blogName
) {
return ResponseEntity.ok(aboutQueryService.findByBlogId(blogId));
return ResponseEntity.ok(aboutQueryService.findByBlogName(blogName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public ResponseEntity<Void> unsubscribe(

@GetMapping("/subscribers")
public ResponseEntity<PageResponse<SubscriberResponse>> findSubscribers(
@RequestParam(name = "blogId", required = true) Long blogId,
@RequestParam(name = "blogName", required = true) String blogName,
@PageableDefault(size = 10) Pageable pageable
) {
return ResponseEntity.ok(PageResponse.from(blogSubscribeQueryService.findSubscribers(blogId, pageable)));
return ResponseEntity.ok(PageResponse.from(blogSubscribeQueryService.findSubscribers(blogName, pageable)));
}

@GetMapping("/subscribing-blogs")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.mallang.blog.application.command.BlogSubscribeCommand;

public record BlogSubscribeRequest(
Long blogId
String blogName
) {
public BlogSubscribeCommand toCommand(Long subscriberId) {
return new BlogSubscribeCommand(subscriberId, blogId);
return new BlogSubscribeCommand(subscriberId, blogName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.mallang.blog.application.command.BlogUnsubscribeCommand;

public record BlogUnsubscribeRequest(
Long blogId
String blogName
) {
public BlogUnsubscribeCommand toCommand(Long subscriberId) {
return new BlogUnsubscribeCommand(subscriberId, blogId);
return new BlogUnsubscribeCommand(subscriberId, blogName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.mallang.blog.application.command.DeleteAboutCommand;

public record DeleteAboutRequest(
Long blogId
String blogName
) {
public DeleteAboutCommand toCommand(Long aboutId, Long memberId) {
return new DeleteAboutCommand(aboutId, memberId, blogId);
return new DeleteAboutCommand(aboutId, memberId, blogName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import com.mallang.blog.application.command.UpdateAboutCommand;

public record UpdateAboutRequest(
Long blogId,
String blogName,
String content
) {
public UpdateAboutCommand toCommand(Long aboutId, Long memberId) {
return new UpdateAboutCommand(aboutId, memberId, blogId, content);
return new UpdateAboutCommand(aboutId, memberId, blogName, content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import com.mallang.blog.application.command.WriteAboutCommand;

public record WriteAboutRequest(
Long blogId,
String blogName,
String content
) {
public WriteAboutCommand toCommand(Long memberId) {
return new WriteAboutCommand(memberId, blogId, content);
return new WriteAboutCommand(memberId, blogName, content);
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/mallang/blog/query/AboutQueryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class AboutQueryService {

private final AboutDao aboutDao;

public AboutResponse findByBlogId(Long blogId) {
return aboutDao.find(blogId);
public AboutResponse findByBlogName(String blogName) {
return aboutDao.find(blogName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class BlogSubscribeQueryService {
private final SubscriberDao subscriberDao;
private final SubscribingBlogDao subscribingBlogDataDao;

public Page<SubscriberResponse> findSubscribers(Long blogId, Pageable pageable) {
return subscriberDao.findSubscribers(blogId, pageable);
public Page<SubscriberResponse> findSubscribers(String blogName, Pageable pageable) {
return subscriberDao.findSubscribers(blogName, pageable);
}

public Page<SubscribingBlogResponse> findSubscribingBlogs(Long memberId, Pageable pageable) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/mallang/blog/query/dao/AboutDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class AboutDao {

private final AboutQuerySupport aboutQuerySupport;

public AboutResponse find(Long blogId) {
return AboutResponse.from(aboutQuerySupport.getByBlogId(blogId));
public AboutResponse find(String blogName) {
return AboutResponse.from(aboutQuerySupport.getByBlogName(blogName));
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/mallang/blog/query/dao/SubscriberDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public class SubscriberDao {

private final JPAQueryFactory query;

public Page<SubscriberResponse> findSubscribers(Long blogId, Pageable pageable) {
public Page<SubscriberResponse> findSubscribers(String blogName, Pageable pageable) {
JPAQuery<Long> countQuery = query.select(blogSubscribe.countDistinct())
.from(blogSubscribe)
.where(blogSubscribe.blog.id.eq(blogId));
.where(blogSubscribe.blog.name.value.eq(blogName));
List<BlogSubscribe> result = query.selectFrom(blogSubscribe)
.distinct()
.join(blogSubscribe.subscriber, member).fetchJoin()
.where(blogSubscribe.blog.id.eq(blogId))
.where(blogSubscribe.blog.name.value.eq(blogName))
.orderBy(blogSubscribe.createdDate.desc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import com.mallang.blog.exception.NotFoundAboutException;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface AboutQuerySupport extends JpaRepository<About, Long> {

default About getByBlogId(Long blogId) {
return findByBlogId(blogId)
default About getByBlogName(String blogName) {
return findByBlogName(blogName)
.orElseThrow(NotFoundAboutException::new);
}

Optional<About> findByBlogId(Long blogId);
@Query("SELECT a FROM About a WHERE a.blog.name.value = :blogName")
Optional<About> findByBlogName(@Param("blogName") String blogName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class CategoryService {

public Long create(CreateCategoryCommand command) {
Member member = memberRepository.getById(command.memberId());
Blog blog = blogRepository.getByIdAndOwnerId(command.blogId(), command.memberId());
Blog blog = blogRepository.getByNameAndOwnerId(command.blogName(), command.memberId());
Category parentCategory =
categoryRepository.getParentByIdAndOwnerId(command.parentCategoryId(), command.memberId());
Category category = Category.create(command.name(), member, blog, parentCategory, categoryValidator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@Builder
public record CreateCategoryCommand(
Long memberId,
Long blogId,
String blogName,
String name,
@Nullable Long parentCategoryId
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public ResponseEntity<Void> delete(

@GetMapping
public ResponseEntity<List<CategoryResponse>> findAllByBlog(
@RequestParam(name = "blogId", required = true) Long blogId
@RequestParam(name = "blogName", required = true) String blogName
) {
List<CategoryResponse> result = categoryQueryService.findAllByBlogId(blogId);
List<CategoryResponse> result = categoryQueryService.findAllByBlogName(blogName);
return ResponseEntity.ok(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import jakarta.annotation.Nullable;

public record CreateCategoryRequest(
Long blogId,
String blogName,
String name,
@Nullable Long parentCategoryId
) {

public CreateCategoryCommand toCommand(Long memberId) {
return CreateCategoryCommand.builder()
.name(name)
.blogId(blogId)
.blogName(blogName)
.memberId(memberId)
.parentCategoryId(parentCategoryId)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class CategoryQueryService {

private final CategoryDao categoryDao;

public List<CategoryResponse> findAllByBlogId(Long blogId) {
return categoryDao.findAllByBlogId(blogId);
public List<CategoryResponse> findAllByBlogName(String blogName) {
return categoryDao.findAllByBlogName(blogName);
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/mallang/category/query/dao/CategoryDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class CategoryDao {

private final CategoryQuerySupport categoryQuerySupport;

public List<CategoryResponse> findAllByBlogId(Long blogId) {
return categoryQuerySupport.findAllRootByBlogId(blogId)
public List<CategoryResponse> findAllByBlogName(String blogName) {
return categoryQuerySupport.findAllRootByBlogName(blogName)
.stream()
.map(CategoryResponse::from)
.toList();
Expand Down
Loading

0 comments on commit 111fa71

Please sign in to comment.