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

Feat/contents #45

Merged
merged 2 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package gwangjang.server.domain.contents.application.dto.res;

import gwangjang.server.domain.contents.domain.entity.constant.ApiType;
import io.swagger.models.auth.In;
import lombok.*;

@Getter
@Builder
@Setter
@NoArgsConstructor
public class ContentsLikeCount {
private Integer contentsId;
private String url;
private String title;
private String description;
private ApiType type;
private String issueTitle;
private String keyword;
private String pubDate;
private String topic;
private String imgUrl;
private long likeCount; // Change from int to long
private boolean userLiked;

public ContentsLikeCount(
Integer contentsId,
String url,
String title,
String description,
ApiType type,
String issueTitle,
String keyword,
String pubDate,
String topic,
String imgUrl,
long likeCount, // Change from int to long
boolean userLiked
) {
this.contentsId = contentsId;
this.url = url;
this.title = title;
this.description = description;
this.type = type;
this.issueTitle = issueTitle;
this.keyword = keyword;
this.pubDate = pubDate;
this.topic = topic;
this.imgUrl = imgUrl;
this.likeCount = likeCount;
this.userLiked = userLiked;
}

// Add getters for all fields
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ public class ContentsRes {
private String topic;
private String imgUrl;

private Long likeCount;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package gwangjang.server.domain.contents.application.dto.res;

import com.fasterxml.jackson.annotation.JsonProperty;
import gwangjang.server.domain.contents.domain.entity.Contents;
import gwangjang.server.domain.contents.domain.entity.constant.ApiType;

public class ContentsWithLikeCount {
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;
private String imgUrl;

private Long likeCount;

private long likeStatus;


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

import gwangjang.server.domain.contents.application.dto.res.BubbleChartRes;
import gwangjang.server.domain.contents.application.dto.res.ContentsDataRes;
import gwangjang.server.domain.contents.application.dto.res.ContentsRes;
import gwangjang.server.domain.contents.application.dto.res.ContentsWithLikeCount;
import gwangjang.server.domain.contents.domain.entity.Contents;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface ContentsCustomRepository {
List<ContentsDataRes> getContentsByIssueId(String issue);
List<Contents> findAllOrderByLikeCountDesc();
List<ContentsWithLikeCount> findAllOrderByLikeCountDesc();
List<Contents> findContentsByLoginId(String loginId);
void updateContentsImageUrl(Integer contentsId, String newImageUrl);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQueryFactory;
import gwangjang.server.domain.contents.application.dto.res.BubbleChartRes;
import gwangjang.server.domain.contents.application.dto.res.*;
import gwangjang.server.domain.contents.domain.entity.constant.ApiType;
import gwangjang.server.domain.like.domain.entity.QContentLike;
import gwangjang.server.domain.contents.application.dto.res.ContentsDataRes;
import gwangjang.server.domain.contents.domain.entity.Contents;
import gwangjang.server.domain.contents.domain.entity.QContents;
import jakarta.persistence.EntityManager;
Expand Down Expand Up @@ -47,12 +46,16 @@ public List<ContentsDataRes> getContentsByIssueId(String issue) {
.limit(10).fetch();
}

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

List<Contents> result = queryFactory
.select(contents)
List<ContentsWithLikeCount> result = queryFactory
.select(Projections.constructor(
ContentsWithLikeCount.class,
contents,
contentLike.likeId.count().as("likeCount")
))
.from(contents)
.leftJoin(contentLike).on(contents.contents_id.eq(contentLike.contents.contents_id))
.groupBy(contents.contents_id)
Expand Down Expand Up @@ -93,9 +96,51 @@ public void updateContentsImageUrl(Integer contentsId, String newImageUrl) {
}





// public List<ContentsLikeCount> searchContentsWithLikeCount(String keyword, ApiType type, String loginId) {
// QContents contents = QContents.contents;
// QContentLike contentLike = QContentLike.contentLike;
//
// List<Tuple> result = queryFactory
// .select(
// contents.contents_id,
// contents.url,
// contents.title,
// contents.description,
// contents.type,
// contents.issueTitle,
// contents.keyword,
// contents.pubDate,
// contents.topic,
// contents.imgUrl,
// contentLike.likeId.count().coalesce(0L).as("likeCount"),
// contentLike.likeId.count().coalesce(0L)
// .as("userLiked")
// )
// .from(contents)
// .leftJoin(contentLike).on(contents.contents_id.eq(contentLike.contents.contents_id).and(contentLike.loginId.eq(loginId)))
// .where(contents.keyword.containsIgnoreCase(keyword).and(contents.type.eq(type)))
// .groupBy(contents.contents_id, contents.url, contents.title, contents.description, contents.type, contents.issueTitle,
// contents.keyword, contents.pubDate, contents.topic, contents.imgUrl)
// .fetch();
//
// return result.stream()
// .map(tuple -> new ContentsLikeCount(
// tuple.get(contents.contents_id),
// tuple.get(contents.url),
// tuple.get(contents.title),
// tuple.get(contents.description),
// tuple.get(contents.type),
// tuple.get(contents.issueTitle),
// tuple.get(contents.keyword),
// tuple.get(contents.pubDate),
// tuple.get(contents.topic),
// tuple.get(contents.imgUrl),
// tuple.get("likeCount", Integer.class), // Change to Integer.class
// tuple.get("userLiked", Boolean.class)
// ))
// .collect(Collectors.toList());
//
// }

}

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gwangjang.server.domain.contents.domain.service;
import gwangjang.server.domain.contents.application.dto.res.ContentsRes;
import gwangjang.server.domain.contents.application.dto.res.ContentsWithLikeCount;
import gwangjang.server.domain.contents.domain.entity.constant.ApiType;
import gwangjang.server.global.annotation.DomainService;
import reactor.core.publisher.Mono;
Expand All @@ -13,7 +14,7 @@ public interface ContentsService {
List<ContentsRes> getContentsTitle(String issue, ApiType type);
List<ContentsRes> getKeywordAndType(String Keyword, ApiType apiType);
ContentsRes getContentsById(Integer contentsId);
List<ContentsRes> getContentLikeCount();
List<ContentsWithLikeCount> getContentLikeCount();
List<ContentsRes> findContentsByLoginId(String loginId);

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gwangjang.server.domain.contents.domain.service;

import gwangjang.server.domain.contents.application.dto.res.ContentsWithLikeCount;
import gwangjang.server.domain.like.domain.repository.LikeRepository;
import gwangjang.server.domain.contents.application.dto.res.ContentsRes;
import gwangjang.server.domain.contents.application.mapper.ContentsMapper;
Expand Down Expand Up @@ -162,7 +163,7 @@ public List<ContentsRes> getContents(ApiType type) {
}

public List<ContentsRes> getContentsTitle(String issue, ApiType type) {
List<Contents> contents = contentsRepository.findByIssueTitleLikeAndType("%" + issue + "%", type);
List<Contents> contents = contentsRepository.findByIssueTitleLikeAndType("%" + issue + "%", type).subList(0,20);
return contents.stream()
.map(contentsMapper::toDto)
.collect(Collectors.toList());
Expand All @@ -181,12 +182,10 @@ public ContentsRes getContentsById(Integer contentsId) {
return contentsMapper.toDto(contents);
}

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

return contents.stream()
.map(contentsMapper::toDto)
.collect(Collectors.toList());
return contents;
}
public List<ContentsRes> findContentsByLoginId(String loginId){
List<Contents> contents = contentsRepository.findContentsByLoginId(loginId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package gwangjang.server.domain.contents.presentation;

import gwangjang.server.domain.contents.application.dto.res.BubbleChartRes;
import gwangjang.server.domain.contents.application.dto.res.BubbleFrontRes;
import gwangjang.server.domain.contents.application.dto.res.ContentsDataRes;
import gwangjang.server.domain.contents.application.dto.res.ContentsRes;
import gwangjang.server.domain.contents.application.dto.res.*;
import gwangjang.server.domain.contents.application.service.ContentsSubscribeUseCase;

import gwangjang.server.domain.contents.domain.entity.constant.ApiType;
Expand Down Expand Up @@ -64,7 +61,7 @@ public ResponseEntity<SuccessResponse<String>> getNaverContents() {
return ResponseEntity.ok(SuccessResponse.create(GET_CONTENTS_SUCCESS.getMessage(),this.newsAPIService.naverAPI("test")));
}
@GetMapping("/contents/like")
public ResponseEntity<SuccessResponse<List<ContentsRes>>> getContentLikeCount() {
public ResponseEntity<SuccessResponse<List<ContentsWithLikeCount>>> getContentLikeCount() {
return ResponseEntity.ok(SuccessResponse.create(GET_CONTENTS_SUCCESS.getMessage(),this.contentsService.getContentLikeCount()));
}
@PostMapping("/my-page/like")
Expand Down