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

🔀 :: (#118) 인기 게시글 목록조회 api #119

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import com.example.moizaspringserver.domain.category.entity.FeedCategory;
import com.example.moizaspringserver.domain.category.entity.FeedCategoryId;
import com.example.moizaspringserver.domain.feed.entity.Feed;
import java.util.List;
import org.springframework.data.repository.CrudRepository;

public interface FeedCategoryRepository extends CrudRepository<FeedCategory, FeedCategoryId> {

void deleteByFeed(Feed feed);

List<FeedCategory> findById_Feed(Integer feedId);
glay415 marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.moizaspringserver.domain.feed.presenstation;

import com.example.moizaspringserver.domain.feed.presenstation.dto.request.LocalFeedRequest;
import com.example.moizaspringserver.domain.feed.presenstation.dto.response.PopularFeedListResponse;
import com.example.moizaspringserver.domain.feed.service.DeleteFeedService;
import com.example.moizaspringserver.domain.feed.service.GetPopularFeedListService;
import com.example.moizaspringserver.domain.feed.presenstation.dto.request.LocalFeedRequest;
import com.example.moizaspringserver.domain.feed.service.UpdateLocalFeedService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand All @@ -15,6 +17,7 @@
public class FeedController {

private final DeleteFeedService deleteFeedService;
private final GetPopularFeedListService getPopularFeedListService;
private final UpdateLocalFeedService updateLocalFeedService;

@ResponseStatus(HttpStatus.NO_CONTENT)
Expand All @@ -23,6 +26,11 @@ public void deleteFeed(@PathVariable("feed-id") Integer feedId) {
deleteFeedService.execute(feedId);
}

@GetMapping("/lists/populars")
public PopularFeedListResponse popularFeedList() {
return getPopularFeedListService.execute();
}

@ResponseStatus(HttpStatus.NO_CONTENT)
@PatchMapping("/temporaries/{feed-id}")
public void updateLocalFeed(@RequestBody @Valid LocalFeedRequest request, @PathVariable("feed-id") Integer feedId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.example.moizaspringserver.domain.feed.presenstation.dto.response;

import com.example.moizaspringserver.domain.feed.type.FeedType;
import java.time.LocalDateTime;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class PopularFeedListResponse {

private final List<PopularFeed> feedList;

@Getter
@Builder
public static class PopularFeed {
private final Integer id;

private final String title;

private final FeedType type;

private final LocalDateTime createdAt;

private final String authorName;

private final boolean isLike;

private final Integer likeCount;

private final String content;

private final String category;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import com.example.moizaspringserver.domain.feed.entity.Feed;
import com.example.moizaspringserver.domain.feed.entity.PublicFeed;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PublicFeedRepository extends JpaRepository<PublicFeed, Integer> {

void deleteByFeed(Feed feed);
List<PublicFeed> findTop8ByCreatedAtBetweenOrderByLikeCountDesc(LocalDateTime start, LocalDateTime end);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.example.moizaspringserver.domain.feed.service;

import com.example.moizaspringserver.domain.category.repository.FeedCategoryRepository;
import com.example.moizaspringserver.domain.feed.entity.PublicFeed;
import com.example.moizaspringserver.domain.feed.presenstation.dto.response.PopularFeedListResponse;
import com.example.moizaspringserver.domain.feed.presenstation.dto.response.PopularFeedListResponse.PopularFeed;
import com.example.moizaspringserver.domain.feed.respository.PublicFeedRepository;
import com.example.moizaspringserver.domain.like.entity.FeedLikeId;
import com.example.moizaspringserver.domain.like.repository.FeedLikeRepository;
import com.example.moizaspringserver.domain.user.facade.UserFacade;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@RequiredArgsConstructor
@Service
public class GetPopularFeedListService {

private final PublicFeedRepository publicFeedRepository;
private final FeedLikeRepository feedLikeRepository;
private final FeedCategoryRepository feedCategoryRepository;
private final UserFacade userFacade;

@Transactional(readOnly = true)
public PopularFeedListResponse execute() {
glay415 marked this conversation as resolved.
Show resolved Hide resolved

List<PublicFeed> publicFeedList = publicFeedRepository
.findTop8ByCreatedAtBetweenOrderByLikeCountDesc(
LocalDateTime.now().minusDays(1), LocalDateTime.now()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now 따로 빼서 하나로 쓰는거 어때?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

굳이 할필욘 없을듯

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

안이 now 호출을 두 번 하잖어

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ㅇㅋ

);

List<PopularFeed> popularFeedList = publicFeedList
.stream()
.map(this::buildPopularFeed)
.collect(Collectors.toList());

return new PopularFeedListResponse(popularFeedList);
}

public PopularFeed buildPopularFeed(PublicFeed publicFeed) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

쿼리가 몇 방이야..ㄷ

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

슬픈게지


boolean isLike = feedLikeRepository.findById(FeedLikeId.builder()
.user(userFacade.queryCurrentUser().getId())
.feed(publicFeed.getFeedId())
.build()).isPresent();

String category = feedCategoryRepository.findById_Feed(publicFeed.getFeedId())
.get(0).getCategory().getCategoryName();

return PopularFeed.builder()
.id(publicFeed.getFeedId())
.title(publicFeed.getTitle())
.type(publicFeed.getFeed().getFeedType())
.createdAt(publicFeed.getCreatedAt())
.authorName(publicFeed.getFeed().getUser().getName())
.isLike(isLike)
.likeCount(publicFeed.getLikeCount())
.content(publicFeed.getContent())
.category(category)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ protected void configure(HttpSecurity http) throws Exception {

// feeds
.antMatchers(HttpMethod.DELETE, "/feeds/{feed-id}").authenticated()
.antMatchers(HttpMethod.GET, "/feeds/lists/populars").hasAnyAuthority(UserType.ROLE_STUDENT.name(), UserType.ROLE_GRADUATE.name(), UserType.ROLE_USER.name())
.antMatchers(HttpMethod.PATCH, "/feeds/temporaries/{feed-id}").hasAnyAuthority(UserType.ROLE_STUDENT.name(), UserType.ROLE_GRADUATE.name())

// notice
Expand Down