메인 페이지에서 카테고리 별 게시글 총개수 반환하는 코드 추가
포스트맨 테스트 시 403에러 발생
ex) api/homepostcount --> api/home/postcount --> api/count
public HomeCountryCountDto getCountryCount() {
QPost post = QPost.post;
Long ASIAPostCount = jpaQueryFactory
.select(post.id.count())
.from(post)
.where(post.category.eq(Category.PostCategory.ASIA))
.fetchOne();
Long AFRICAPostCount = jpaQueryFactory
.select(post.id.count())
.from(post)
.where(post.category.eq(Category.PostCategory.AFRICA))
.fetchOne();
Long EUROPEPostCount = jpaQueryFactory
.select(post.id.count())
.from(post)
.where(post.category.eq(Category.PostCategory.EUROPE))
.fetchOne();
Long OCEANIAPostCount = jpaQueryFactory
.select(post.id.count())
.from(post)
.where(post.category.eq(Category.PostCategory.OCEANIA))
.fetchOne();
Long AMERICAPostCount = jpaQueryFactory
.select(post.id.count())
.from(post)
.where(post.category.eq(Category.PostCategory.AMERICA))
.fetchOne();
return new HomeCountryCountDto(ASIAPostCount, AFRICAPostCount, EUROPEPostCount, OCEANIAPostCount, AMERICAPostCount);
}
Map<String, Long> countryCounts = new LinkedHashMap<>();
Long asiaPostCount = getPostCountByCategory(Category.PostCategory.ASIA);
Long africaPostCount = getPostCountByCategory(Category.PostCategory.AFRICA);
Long europePostCount = getPostCountByCategory(Category.PostCategory.EUROPE);
Long oceaniaPostCount = getPostCountByCategory(Category.PostCategory.OCEANIA);
Long americaPostCount = getPostCountByCategory(Category.PostCategory.AMERICA);
countryCounts.put("asiaPostCount", asiaPostCount);
countryCounts.put("africaPostCount", africaPostCount);
countryCounts.put("europePostCount", europePostCount);
countryCounts.put("oceaniaPostCount", oceaniaPostCount);
countryCounts.put("americaPostCount", americaPostCount);
return countryCounts;
}
private Long getPostCountByCategory(Category.PostCategory category) {
return jpaQueryFactory
.select(post.id.count())
.from(post)
.where(post.category.eq(category))
.fetchOne();
}
}
Long asiaPostCount = getPostCountByCategory(Category.PostCategory.ASIA);
Long africaPostCount = getPostCountByCategory(Category.PostCategory.AFRICA);
Long europePostCount = getPostCountByCategory(Category.PostCategory.EUROPE);
Long oceaniaPostCount = getPostCountByCategory(Category.PostCategory.OCEANIA);
Long americaPostCount = getPostCountByCategory(Category.PostCategory.AMERICA);
List<HomeCountryCountDto> countryCounts = new ArrayList<>();
countryCounts.add(new HomeCountryCountDto(
Category.PostCategory.ASIA.getValue(),
asiaPostCount,
africaPostCount,
europePostCount,
oceaniaPostCount,
americaPostCount
));
countryCounts.add(new HomeCountryCountDto(
Category.PostCategory.AFRICA.getValue(),
asiaPostCount,
africaPostCount,
europePostCount,
oceaniaPostCount,
americaPostCount
));
countryCounts.add(new HomeCountryCountDto(
Category.PostCategory.EUROPE.getValue(),
asiaPostCount,
africaPostCount,
europePostCount,
oceaniaPostCount,
americaPostCount
));
countryCounts.add(new HomeCountryCountDto(
Category.PostCategory.OCEANIA.getValue(),
asiaPostCount,
africaPostCount,
europePostCount,
oceaniaPostCount,
americaPostCount
));
countryCounts.add(new HomeCountryCountDto(
Category.PostCategory.AMERICA.getValue(),
asiaPostCount,
africaPostCount,
europePostCount,
oceaniaPostCount,
americaPostCount
));
return countryCounts;
}
private Long getPostCountByCategory(Category.PostCategory category) {
return jpaQueryFactory
.select(post.id.count())
.from(post)
.where(post.category.eq(category))
.fetchOne();
}
.requestMatchers(HttpMethod.GET, "/api/count").permitAll()
에러 코드 확인 잘 하자!!!