Skip to content
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

feat: 블로그 이름을 통해 진행되었던 API 요청들을 블로그 ID를 사용하도록 변경 #92

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.getByNameAndOwnerId(command.blogName(), command.memberId());
Blog blog = blogRepository.getByIdAndOwnerId(command.blogId(), command.memberId());
About about = command.toAbout(member, blog);
about.write(aboutValidator);
return aboutRepository.save(about)
.getId();
}

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

public void delete(DeleteAboutCommand command) {
About about = aboutRepository.getByIdAndWriterIdAndBlogName(
command.aboutId(), command.memberId(), command.blogName()
About about = aboutRepository.getByIdAndWriterIdAndBlogId(
command.aboutId(), command.memberId(), command.blogId()
);
aboutRepository.delete(about);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
public record DeleteAboutCommand(
Long aboutId,
Long memberId,
String blogName
Long blogId
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public record UpdateAboutCommand(
Long aboutId,
Long memberId,
String blogName,
Long blogId,
String content
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public record WriteAboutCommand(
Long memberId,
String blogName,
Long blogId,
String content
) {
public About toAbout(Member member, Blog blog) {
Expand Down
13 changes: 3 additions & 10 deletions src/main/java/com/mallang/blog/domain/AboutRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,15 @@
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 getByIdAndWriterIdAndBlogName(Long aboutId, Long memberId, String blogName) {
return findByWriterIdAndBlogName(aboutId, memberId, blogName)
default About getByIdAndWriterIdAndBlogId(Long aboutId, Long memberId, Long blogId) {
return findByIdAndWriterIdAndBlogId(aboutId, memberId, blogId)
.orElseThrow(NotFoundAboutException::new);
}

@Query("SELECT a FROM About a WHERE a.id = :aboutId AND a.writer.id = :writerId AND a.blog.name.value = :blogName")
Optional<About> findByWriterIdAndBlogName(
@Param("aboutId") Long aboutId,
@Param("writerId") Long writerId,
@Param("blogName") String blogName
);
Optional<About> findByIdAndWriterIdAndBlogId(Long aboutId, Long writerId, Long blogId);
}
19 changes: 3 additions & 16 deletions src/main/java/com/mallang/blog/domain/BlogRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,20 @@
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);
}

@Query("SELECT b FROM Blog b WHERE b.name.value = :blogName")
Optional<Blog> findByName(String blogName);

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

@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
);
Optional<Blog> findByIdAndOwnerId(Long id, Long ownerId);

boolean existsByName(BlogName name);

Expand Down
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 = "blogName") String blogName
@RequestParam(name = "blogId") Long blogId
) {
return ResponseEntity.ok(aboutQueryService.findByBlogName(blogName));
return ResponseEntity.ok(aboutQueryService.findByBlogId(blogId));
}
}
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(
String blogName
Long blogId
) {
public DeleteAboutCommand toCommand(Long aboutId, Long memberId) {
return new DeleteAboutCommand(aboutId, memberId, blogName);
return new DeleteAboutCommand(aboutId, memberId, blogId);
}
}
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(
String blogName,
Long blogId,
String content
) {
public UpdateAboutCommand toCommand(Long aboutId, Long memberId) {
return new UpdateAboutCommand(aboutId, memberId, blogName, content);
return new UpdateAboutCommand(aboutId, memberId, blogId, 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(
String blogName,
Long blogId,
String content
) {
public WriteAboutCommand toCommand(Long memberId) {
return new WriteAboutCommand(memberId, blogName, content);
return new WriteAboutCommand(memberId, blogId, 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 findByBlogName(String blogName) {
return aboutDao.find(blogName);
public AboutResponse findByBlogId(Long blogId) {
return aboutDao.find(blogId);
}
}
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(String blogName) {
return AboutResponse.from(aboutQuerySupport.getByBlogName(blogName));
public AboutResponse find(Long blogId) {
return AboutResponse.from(aboutQuerySupport.getByBlogId(blogId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
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 getByBlogName(String blogName) {
return findByBlogName(blogName)
default About getByBlogId(Long blogId) {
return findByBlogId(blogId)
.orElseThrow(NotFoundAboutException::new);
}

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

public Long create(CreateCategoryCommand command) {
Member member = memberRepository.getById(command.memberId());
Blog blog = blogRepository.getByNameAndOwnerId(command.blogName(), command.memberId());
Blog blog = blogRepository.getByIdAndOwnerId(command.blogId(), command.memberId());
Category parentCategory = getParentCategory(command.parentCategoryId(), command.memberId());
Category category = Category.create(command.name(), member, blog, parentCategory, categoryValidator);
return categoryRepository.save(category).getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@Builder
public record CreateCategoryCommand(
Long memberId,
String blogName,
Long blogId,
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 = "blogName", required = true) String blogName
@RequestParam(name = "blogId", required = true) Long blogId
) {
List<CategoryResponse> result = categoryQueryService.findAllByBlogName(blogName);
List<CategoryResponse> result = categoryQueryService.findAllByBlogId(blogId);
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(
String blogName,
Long blogId,
String name,
@Nullable Long parentCategoryId
) {

public CreateCategoryCommand toCommand(Long memberId) {
return CreateCategoryCommand.builder()
.name(name)
.blogName(blogName)
.blogId(blogId)
.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> findAllByBlogName(String blogName) {
return categoryDao.findAllByBlogName(blogName);
public List<CategoryResponse> findAllByBlogId(Long blogId) {
return categoryDao.findAllByBlogId(blogId);
}
}
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> findAllByBlogName(String blogName) {
return categoryQuerySupport.findAllRootByBlogName(blogName)
public List<CategoryResponse> findAllByBlogId(Long blogId) {
return categoryQuerySupport.findAllRootByBlogId(blogId)
.stream()
.map(CategoryResponse::from)
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ default Category getById(Long id) {
return findById(id).orElseThrow(NotFoundCategoryException::new);
}

@Query("SELECT c FROM Category c WHERE c.blog.name.value = :blogName AND c.parent = null")
List<Category> findAllRootByBlogName(@Param("blogName") String blogName);
@Query("SELECT c FROM Category c WHERE c.blog.id = :blogId AND c.parent = null")
List<Category> findAllRootByBlogId(@Param("blogId") Long blogId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class PostService {

public Long create(CreatePostCommand command) {
Member member = memberRepository.getById(command.memberId());
Blog blog = blogRepository.getByNameAndOwnerId(command.blogName(), command.memberId());
Blog blog = blogRepository.getByIdAndOwnerId(command.blogId(), command.memberId());
Category category = getCategoryByIdAndOwnerIdIfPresent(command.categoryId(), command.memberId());
Long postIdInBlog = postOrderInBlogGenerator.generate(blog);
Post post = command.toPost(member, blog, category, postIdInBlog);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@Builder
public record CreatePostCommand(
Long memberId,
String blogName,
Long blogId,
String title,
String content,
@Nullable String postThumbnailImageName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.List;

public record CreatePostRequest(
String blogName,
Long blogId,
String title,
String content,
@Nullable String postThumbnailImageName,
Expand All @@ -20,7 +20,7 @@ public record CreatePostRequest(
public CreatePostCommand toCommand(Long memberId) {
return CreatePostCommand.builder()
.memberId(memberId)
.blogName(blogName)
.blogId(blogId)
.title(title)
.content(content)
.postThumbnailImageName(postThumbnailImageName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Page<PostManageSearchResponse> search(Long memberId, PostManageSearchCond
JPAQuery<Long> countQuery = query.select(post.countDistinct())
.from(post)
.where(
memberAndBlogEq(memberId, cond.blogName()),
memberAndBlogEq(memberId, cond.blogId()),
hasCategory(cond.categoryId()),
titleContains(cond.title()),
contentContains(cond.content()),
Expand All @@ -45,7 +45,7 @@ public Page<PostManageSearchResponse> search(Long memberId, PostManageSearchCond
.distinct()
.leftJoin(post.category, category).fetchJoin()
.where(
memberAndBlogEq(memberId, cond.blogName()),
memberAndBlogEq(memberId, cond.blogId()),
hasCategory(cond.categoryId()),
titleContains(cond.title()),
contentContains(cond.content()),
Expand All @@ -59,8 +59,8 @@ public Page<PostManageSearchResponse> search(Long memberId, PostManageSearchCond
.map(PostManageSearchResponse::from);
}

private BooleanExpression memberAndBlogEq(Long memberId, String blogName) {
return post.writer.id.eq(memberId).and(post.blog.name.value.eq(blogName));
private BooleanExpression memberAndBlogEq(Long memberId, Long blogId) {
return post.writer.id.eq(memberId).and(post.blog.id.eq(blogId));
}

private BooleanExpression hasCategory(@Nullable Long categoryId) {
Expand Down Expand Up @@ -97,7 +97,7 @@ private BooleanExpression visibilityEq(@Nullable Visibility visibility) {

@Builder
public record PostManageSearchCond(
@NotNull String blogName,
@NotNull Long blogId,
@Nullable String title,
@Nullable String content,
@Nullable Long categoryId,
Expand Down
Loading