Skip to content

Commit

Permalink
Merge pull request #27 from kusitms-28th-Meetup-E/feat/contents
Browse files Browse the repository at this point in the history
feat(#12) : 좋아요내림차순 조회
  • Loading branch information
eojinny committed Nov 20, 2023
2 parents e2b797f + d8b0f25 commit ff75972
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package gwangjang.server.domain.like.domain.repository;

import com.querydsl.jpa.impl.JPAQueryFactory;
import gwangjang.server.domain.like.domain.entity.ContentLike;
import org.springframework.data.jpa.repository.support.QuerydslRepositorySupport;

public class LikeRepositoryImpl extends QuerydslRepositorySupport {

private final JPAQueryFactory jpaQueryFactory;

public LikeRepositoryImpl(JPAQueryFactory jpaQueryFactory) {
super(ContentLike.class);
this.jpaQueryFactory = jpaQueryFactory;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package gwangjang.server.domain.morpheme.application.dto.res;

import gwangjang.server.domain.morpheme.domain.entity.constant.ApiType;
import lombok.*;

@Getter
@Builder
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class ContentLikeCountRes {
private Integer contents_id;
private String url;
private String title;
private String description;
ApiType type;
private String issueTitle;
private String keyword;
private String pubDate;
private String topic;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package gwangjang.server.domain.morpheme.domain.repository;

import gwangjang.server.domain.morpheme.application.dto.res.ContentsDataRes;
import gwangjang.server.domain.morpheme.domain.entity.Contents;

import java.util.List;

public interface ContentsCustomRepository {
List<ContentsDataRes> getContentsByIssueId(String issue);
List<Contents> findAllOrderByLikeCountDesc();

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package gwangjang.server.domain.morpheme.domain.repository;

import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.types.dsl.NumberTemplate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import gwangjang.server.domain.like.domain.entity.QContentLike;
import gwangjang.server.domain.morpheme.application.dto.res.ContentLikeCountRes;
import gwangjang.server.domain.morpheme.application.dto.res.ContentsDataRes;
import gwangjang.server.domain.morpheme.domain.entity.Contents;
import gwangjang.server.domain.morpheme.domain.entity.QContents;
import jakarta.persistence.EntityManager;

import java.util.List;
Expand Down Expand Up @@ -34,5 +40,19 @@ public List<ContentsDataRes> getContentsByIssueId(String issue) {
.limit(10).fetch();
}

public List<Contents> findAllOrderByLikeCountDesc() {
QContents contents = QContents.contents;
QContentLike contentLike = QContentLike.contentLike;

List<Contents> result = queryFactory
.select(contents)
.from(contents)
.leftJoin(contentLike).on(contents.contents_id.eq(contentLike.contents.contents_id))
.groupBy(contents.contents_id)
.orderBy(contentLike.likeId.count().desc())
.fetch();
return result;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public interface ContentsService {
List<ContentsRes> getContentsTitle(String issue);
List<ContentsRes> getKeywordAndType(String Keyword, ApiType apiType);
ContentsRes getContentsById(Integer contentsId);
List<ContentsRes> getContentLikeCount();

}

Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package gwangjang.server.domain.morpheme.domain.service;

import gwangjang.server.domain.like.domain.entity.ContentLike;
import gwangjang.server.domain.like.domain.repository.LikeRepository;
import gwangjang.server.domain.morpheme.application.dto.res.ContentLikeCountRes;
import gwangjang.server.domain.morpheme.application.dto.res.ContentsRes;
import gwangjang.server.domain.morpheme.application.mapper.ContentsMapper;
import gwangjang.server.domain.morpheme.domain.entity.Contents;
Expand Down Expand Up @@ -29,14 +32,16 @@ public class ContentsServiceImpl implements ContentsService{
private final ContentsRepository contentsRepository;
private final WebClient webClient;
private final ContentsMapper contentsMapper;
private final LikeRepository contentsLikeRepository;

@Value("${youtube.api.key}")
private String youtubeApiKey;

@Autowired
public ContentsServiceImpl(ContentsRepository contentsRepository, ContentsMapper contentsMapper) {
public ContentsServiceImpl(ContentsRepository contentsRepository, ContentsMapper contentsMapper,LikeRepository likeRepository) {
this.contentsRepository = contentsRepository;
this.contentsMapper = contentsMapper;
this.contentsLikeRepository=likeRepository;
this.webClient = WebClient.builder()
.baseUrl("https://www.googleapis.com/youtube/v3")
.build();
Expand Down Expand Up @@ -163,4 +168,13 @@ public ContentsRes getContentsById(Integer contentsId) {
.orElseThrow(() -> new IllegalArgumentException("해당 콘텐츠가 존재하지 않습니다. id=" + contentsId));
return contentsMapper.toDto(contents);
}

public List<ContentsRes> getContentLikeCount(){
List<Contents> contents = contentsRepository.findAllOrderByLikeCountDesc();

return contents.stream()
.map(contentsMapper::toDto)
.collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public ResponseEntity<SuccessResponse<ContentsRes>> getContentsTitle(@PathVariab
public ResponseEntity<SuccessResponse<String>> getNaverContents() {
return ResponseEntity.ok(SuccessResponse.create(ContentsResponseMessage.GET_CONTENTS_SUCCESS.getMessage(),this.newsAPIService.naverAPI("test")));
}
@GetMapping("/contents/like")
public ResponseEntity<SuccessResponse<List<ContentsRes>>> getContentLikeCount() {
return ResponseEntity.ok(SuccessResponse.create(ContentsResponseMessage.GET_CONTENTS_SUCCESS.getMessage(),this.contentsService.getContentLikeCount()));
}


//컨텐츠 가져오는 API
//유튜브에서 제공해주는 할당량 다 사용해서 어떻게 할지 얘기할 것.
Expand Down

0 comments on commit ff75972

Please sign in to comment.