Skip to content

Commit

Permalink
feat: Add orderBy ScrapAt DESC (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
chaeeun-Han committed Mar 22, 2024
1 parent d193ecf commit 54e7a8a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public interface PostsRepository extends JpaRepository<Posts, Long> {

Posts findByPostId(Long postId);

@Query("SELECT p FROM Posts p WHERE p.postId IN :postIds")
@Query("SELECT p FROM Posts p JOIN Scrap s ON p.postId = s.postId WHERE s.postId IN :postIds ORDER BY s.scrapAt DESC")
List<Posts> findAllByPostId(List<Long> postIds);

@Query("SELECT p FROM Posts p WHERE p.postId IN :postIds")
@Query("SELECT p FROM Posts p JOIN Scrap s ON p.postId = s.postId WHERE s.postId IN :postIds ORDER BY s.scrapAt DESC")
Page<Posts> findByPostIds(@Param("postIds") List<Long> postIds, PageRequest pageRequest);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.*;

@Slf4j
Expand Down Expand Up @@ -314,9 +316,13 @@ public ResponseEntity<? super PutScrapResponseDto> putScrap(Long postId, Long ka

Scrap scrap = scrapRepo.findByPostIdAndUserId(postId, kakao_uid);

Date now = Date.from(Instant.now());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String scrapDatetime = simpleDateFormat.format(now);

// 존재하지 않는다면 추가. 존재한다면 삭제
if (scrap == null) {
scrap = new Scrap(kakao_uid, postId);
scrap = new Scrap(kakao_uid, postId, scrapDatetime);
scrapRepo.save(scrap);
post.increaseScrapCount();
}
Expand Down

0 comments on commit 54e7a8a

Please sign in to comment.