Skip to content

Commit

Permalink
카테고리 아이디
Browse files Browse the repository at this point in the history
  • Loading branch information
JONG-KYEONG committed Nov 30, 2023
1 parent 6c0c7fc commit 353a6be
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ public ResponseEntity<String> deletePosts(@CurrentUser UserPrincipal userPrincip
}

@GetMapping ("/category/sidebar/{blogId}")
public ResponseEntity<SidebarDtos> getSidebarByBlog(@PathVariable Long blogId){
public ResponseEntity<SidebarDtos> getSidebarByBlog(@CurrentUser UserPrincipal userPrincipal,
@PathVariable Long blogId){

//해당 블로그의 사이드바를 읽어 온다
SidebarDtos sidebarResponse = categoryService.getSideBarByBlog(blogId);
SidebarDtos sidebarResponse = categoryService.getSideBarByBlog(userPrincipal, blogId);

return new ResponseEntity<>(sidebarResponse,HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ public class SidebarDto {
private Long categoryId;
private String categoryName;
private Boolean isPrCategory;
private Boolean isMyPage;
private List<PostTitleDto> postTitleDtos = new ArrayList<>(); //TODO 일급 컬렉션으로 수정

public SidebarDto(Category category, List<Post> posts){
public SidebarDto(Category category, List<Post> posts, Boolean isMyPages){
categoryId = category.getId();
categoryName = category.getCategoryName();
isPrCategory = category.getIsPrcategory();
isMyPage = isMyPages;
for(Post post : posts){
postTitleDtos.add(PostTitleDto.of(post));
}
Expand Down
13 changes: 11 additions & 2 deletions server/src/main/java/com/project/Glog/service/CategoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.project.Glog.domain.Blog;
import com.project.Glog.domain.Category;
import com.project.Glog.domain.Post;
import com.project.Glog.domain.User;
import com.project.Glog.dto.request.category.CategoryCreateRequest;
import com.project.Glog.dto.request.category.CategoryUpdateRequest;
import com.project.Glog.dto.response.category.CategoryDto;
Expand Down Expand Up @@ -44,15 +45,23 @@ public void delete(Long uid, Long categoryId) {
categoryRepository.delete(categoryRepository.findById(categoryId).get());
}

public SidebarDtos getSideBarByBlog(Long blogId) {
public SidebarDtos getSideBarByBlog(UserPrincipal userPrincipal, Long blogId) {
//해당 블로그의 카테고리를 모두 불러온다.
List<Category> categories = categoryRepository.findAllByBlogId(blogId);
Boolean isMyPage;

if (userPrincipal == null){
isMyPage = false;
}
else{
isMyPage = (blogRepository.findById(blogId).get().getUser().getId() == userPrincipal.getId());
}

//모든 카테고리를 순회하며, 각 카테고리당 게시글을 모두 불러와서 SidebarDto에 담는다.
List<SidebarDto> sidebarDtos = new ArrayList<>();
for(Category category : categories){
List<Post> posts = postRepository.findAllByCategoryId(category.getId());
sidebarDtos.add(new SidebarDto(category, posts));
sidebarDtos.add(new SidebarDto(category, posts,isMyPage));
}

return new SidebarDtos(sidebarDtos);
Expand Down

0 comments on commit 353a6be

Please sign in to comment.