Skip to content
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
Expand Up @@ -2,17 +2,16 @@

import lombok.*;

import java.util.List;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ContentResponseDto {

private String contentId;
private String contentTitle;
private int contentView;
private int contentLike;
private int contentLevel;
private String writer;
private int total;
private List<ContentTitleDto> contentList;

}
18 changes: 18 additions & 0 deletions src/main/java/com/example/prdoit/dto/content/ContentTitleDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.prdoit.dto.content;

import lombok.*;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ContentTitleDto {

private String contentId;
private String contentTitle;
private int contentView;
private int contentLike;
private int contentLevel;
private String writer;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public class TotalContentDto {
private int totalView;
private int totalLike;

private List<ContentResponseDto> ContentList;
private List<ContentTitleDto> ContentList;

}
1 change: 1 addition & 0 deletions src/main/java/com/example/prdoit/model/ContentTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class ContentTable {

@ManyToOne
@JoinColumn(name = "userId")
@JsonIgnore
private IdTable userId;

@OneToMany(mappedBy = "contentId", cascade = CascadeType.REMOVE, orphanRemoval = true, fetch = FetchType.EAGER)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,67 +1,88 @@
package com.example.prdoit.repository;

import com.example.prdoit.dto.content.ContentResponseDto;
import com.example.prdoit.dto.content.ContentTitleDto;
import com.example.prdoit.model.ContentTable;
import com.example.prdoit.model.IdTable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import javax.swing.text.AbstractDocument;
import java.util.List;

public interface ContentTableRepository extends JpaRepository<ContentTable, String> {

@Query("SELECT new com.example.prdoit.dto.content.ContentResponseDto" +
@Query("SELECT new com.example.prdoit.dto.content.ContentTitleDto" +
"(c.contentId, c.contentTitle, c.contentView, c.contentLike, c.contentLevel, c.userId.nickname) " +
"FROM ContentTable c LEFT JOIN CommentTable cc ON c.contentId = cc.contentId.contentId " +
"WHERE c.contentTitle LIKE %:query% GROUP BY c.contentId, c.contentTitle, c.contentView, c.contentLike, c.contentLevel " +
"ORDER BY (c.contentView * 0.33) + (c.contentLike * 0.33) + (COUNT(cc.commentId) * 0.33) DESC " +
"LIMIT :size OFFSET :offset")
List<ContentResponseDto> findAllByQueryContaining(@Param("query") String query, @Param("size") int size, @Param("offset") int offset);
List<ContentTitleDto> findAllByQueryContaining(@Param("query") String query, @Param("size") int size, @Param("offset") int offset);

@Query("SELECT new com.example.prdoit.dto.content.ContentResponseDto" +
@Query("SELECT c FROM ContentTable c WHERE c.contentTitle LIKE %:query% OR c.contentContent LIKE %:query%")
List<ContentTable> findAllByContentTitleContaining(String query);

@Query("SELECT new com.example.prdoit.dto.content.ContentTitleDto" +
"(c.contentId, c.contentTitle, c.contentView, c.contentLike, c.contentLevel, c.userId.nickname) " +
"FROM ContentTable c LEFT JOIN CommentTable cc ON c.contentId = cc.contentId.contentId " +
"GROUP BY c.contentId, c.contentTitle, c.contentView, c.contentLike, c.contentLevel " +
"ORDER BY (c.contentView * 0.33) + (c.contentLike * 0.33) + (COUNT(cc.commentId) * 0.33) DESC " +
"LIMIT :size OFFSET :offset")
List<ContentResponseDto> findAllList(@Param("size") int size, @Param("offset") int offset);
List<ContentTitleDto> findAllList(@Param("size") int size, @Param("offset") int offset);


@Query("SELECT new com.example.prdoit.dto.content.ContentResponseDto" +
@Query("SELECT new com.example.prdoit.dto.content.ContentTitleDto" +
"(c.contentId, c.contentTitle, c.contentView, c.contentLike, c.contentLevel, c.userId.nickname) " +
"FROM ContentTable c LEFT JOIN CommentTable cc on c.contentId = cc.contentId.contentId WHERE c.contentTitle LIKE %:query% AND c.contentLevel = :level " +
"GROUP BY c.contentId, c.contentTitle, c.contentView, c.contentLike, c.contentLevel " +
"ORDER BY (c.contentView * 0.33) + (c.contentLike * 0.33) + (COUNT(cc.commentId) * 0.33) DESC " +
"LIMIT :size OFFSET :offset")
List<ContentResponseDto> findAllQueryContainingAndContentLevel(@Param("query")String query, @Param("level")int level, @Param("size") int size, @Param("offset") int offset);
List<ContentTitleDto> findAllQueryContainingAndContentLevel(@Param("query")String query, @Param("level")int level, @Param("size") int size, @Param("offset") int offset);

@Query("SELECT new com.example.prdoit.dto.content.ContentResponseDto" +
@Query("SELECT c FROM ContentTable c WHERE (c.contentTitle LIKE %:query% OR c.contentContent LIKE %:query%) AND c.contentLevel = :level")
List<ContentTable> findAllByQueryContainingAndContentLevel(String query, int level);

@Query("SELECT new com.example.prdoit.dto.content.ContentTitleDto" +
"(c.contentId, c.contentTitle, c.contentView, c.contentLike, c.contentLevel, c.userId.nickname) " +
"FROM ContentTable c LEFT JOIN CommentTable cc ON c.contentId = cc.contentId.contentId WHERE c.contentLevel = :level " +
"GROUP BY c.contentId, c.contentTitle, c.contentView, c.contentLike, c.contentLevel " +
"ORDER BY (c.contentView * 0.33) + (c.contentLike * 0.33) + (COUNT(cc.commentId) * 0.33) DESC " +
"LIMIT :size OFFSET :offset")
List<ContentResponseDto> findAllByContentLevel(@Param("level") int level, @Param("size") int size, @Param("offset") int offset);
List<ContentTitleDto> findAllByContentLevel(@Param("level") int level, @Param("size") int size, @Param("offset") int offset);

List<ContentTable> findAllByContentLevel(int level);

@Query("SELECT new com.example.prdoit.dto.content.ContentResponseDto" +
@Query("SELECT new com.example.prdoit.dto.content.ContentTitleDto" +
"(c.contentId, c.contentTitle, c.contentView, c.contentLike, c.contentLevel, c.userId.nickname) " +
"FROM ContentTable c WHERE c.userId.id = :userId")
Page<ContentResponseDto> findAllByUserIdPagination(@Param("userId") String userId, Pageable pageable);
Page<ContentTitleDto> findAllByUserIdPagination(@Param("userId") String userId, Pageable pageable);

@Query("SELECT new com.example.prdoit.dto.content.ContentResponseDto" +
List<ContentTable> findAllByUserId(IdTable userId);

@Query("SELECT new com.example.prdoit.dto.content.ContentTitleDto" +
"(c.contentId, c.contentTitle, c.contentView, c.contentLike, c.contentLevel, c.userId.nickname) " +
"FROM ContentTable c WHERE c.userId.id = :userId AND c.contentLevel = :level")
Page<ContentResponseDto> findAllByUserIdAndContentLevel(String userId, int level, Pageable pageable);
Page<ContentTitleDto> findAllByUserIdAndContentLevel(String userId, int level, Pageable pageable);

List<ContentTable> findAllByUserIdAndContentLevel(IdTable userId, int level);

@Query("SELECT new com.example.prdoit.dto.content.ContentResponseDto" +
@Query("SELECT new com.example.prdoit.dto.content.ContentTitleDto" +
"(c.contentId, c.contentTitle, c.contentView, c.contentLike, c.contentLevel, c.userId.nickname) " +
"FROM ContentTable c WHERE c.userId.id = :userId AND c.contentTitle LIKE %:query%")
Page<ContentResponseDto> findAllByUserIdAndQueryContaining(String userId, String query, Pageable pageable);
Page<ContentTitleDto> findAllByUserIdAndQueryContaining(String userId, String query, Pageable pageable);

@Query("SELECT new com.example.prdoit.dto.content.ContentResponseDto" +
@Query("SELECT c FROM ContentTable c WHERE c.userId = :userId AND (c.contentTitle LIKE %:query% or c.contentContent LIKE %:query%)")
List<ContentTable> findAllByUserIdAndQueryContaining(IdTable userId, String query);

@Query("SELECT new com.example.prdoit.dto.content.ContentTitleDto" +
"(c.contentId, c.contentTitle, c.contentView, c.contentLike, c.contentLevel, c.userId.nickname) " +
"FROM ContentTable c WHERE c.userId.id = :userId AND c.contentLevel = :level AND c.contentTitle LIKE %:query%")
Page<ContentResponseDto> findAllByUserIdAndContentLevelAndQueryContaining(String userId, int level, String query, Pageable pageable);
Page<ContentTitleDto> findAllByUserIdAndContentLevelAndQueryContaining(String userId, int level, String query, Pageable pageable);

@Query("SELECT c FROM ContentTable c WHERE c.userId = :userId AND c.contentLevel = :level AND (c.contentTitle LIKE %:query% or c.contentContent LIKE %:query%)")
List<ContentTable> findAllByUserIdAndContentLevelAndQueryContaining(IdTable userId, int level, String query);

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
//
public interface ContentService {

List<ContentResponseDto> getContentQuery(String query, int size, int page);
ContentResponseDto getContentQuery(String query, int size, int page);

List<ContentResponseDto> getContentQueryWithLevel(String query, int level, int size, int page);
ContentResponseDto getContentQueryWithLevel(String query, int level, int size, int page);

List<ContentResponseDto> getContentWithUserId(String userId, String query, int level, int size, int page);
ContentResponseDto getContentWithUserId(String userId, String query, int level, int size, int page);

void updateContent(ContentUpdateDto contentUpdateDto);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.swing.text.AbstractDocument;
import java.util.List;
import java.util.UUID;

Expand All @@ -24,7 +25,7 @@ public class ContentServiceImpl implements ContentService {
private final IdTableRepository IdTableRepository;

@Override
public List<ContentResponseDto> getContentQuery(String query, int size, int page){
public ContentResponseDto getContentQuery(String query, int size, int page){
log.info("[getContentQuery] 쿼리 조회 로직 시작");
List<ContentTable> contentTableList = contentRepository.findAll();
int offset = (page - 1) * size;
Expand All @@ -33,18 +34,25 @@ public List<ContentResponseDto> getContentQuery(String query, int size, int page
}
try {
if(!query.equals("all")){
List<ContentResponseDto> contentResponseDtoList = contentRepository.findAllByQueryContaining(query, size, offset);
if (contentResponseDtoList.isEmpty()) {
List<ContentTable> contentTableListQuery = contentRepository.findAllByContentTitleContaining(query);
List<ContentTitleDto> contentTitleDtoList = contentRepository.findAllByQueryContaining(query, size, offset);
if (contentTitleDtoList.isEmpty()) {
throw new RuntimeException("검색 결과가 없습니다.");
} else {
return contentResponseDtoList;
return ContentResponseDto.builder()
.total(contentTableListQuery.size())
.contentList(contentTitleDtoList)
.build();
}
} else{
List<ContentResponseDto> contentResponseDtoList = contentRepository.findAllList(size, offset);
List<ContentTitleDto> contentResponseDtoList = contentRepository.findAllList(size, offset);
if (contentResponseDtoList.isEmpty()) {
throw new RuntimeException("등록된 컨텐츠가 없습니다.");
} else {
return contentResponseDtoList;
return ContentResponseDto.builder()
.total(contentTableList.size())
.contentList(contentResponseDtoList)
.build();
}
}
} catch (Exception e) {
Expand All @@ -54,26 +62,34 @@ public List<ContentResponseDto> getContentQuery(String query, int size, int page
}

@Override
public List<ContentResponseDto> getContentQueryWithLevel(String query, int level, int size, int page){
public ContentResponseDto getContentQueryWithLevel(String query, int level, int size, int page){
List<ContentTable> contentTableList = contentRepository.findAll();
if(contentTableList.isEmpty()) {
throw new RuntimeException("등록된 컨텐츠가 없습니다.");
}
int offset = (page - 1) * size;
try{
if(!query.equals("all")){
List<ContentResponseDto> contentResponseDtoList = contentRepository.findAllQueryContainingAndContentLevel(query, level, size, offset);
List<ContentTable> contentTableListAll = contentRepository.findAllByQueryContainingAndContentLevel(query, level);
List<ContentTitleDto> contentResponseDtoList = contentRepository.findAllQueryContainingAndContentLevel(query, level, size, offset);
if(contentResponseDtoList.isEmpty()){
throw new RuntimeException("검색 결과가 없습니다.");
} else {
return contentResponseDtoList;
return ContentResponseDto.builder()
.total(contentTableListAll.size())
.contentList(contentResponseDtoList)
.build();
}
} else {
List<ContentResponseDto> contentResponseDtoList = contentRepository.findAllByContentLevel(level, size, offset);
List<ContentTable> contentTableListAll = contentRepository.findAllByContentLevel(level);
List<ContentTitleDto> contentResponseDtoList = contentRepository.findAllByContentLevel(level, size, offset);
if(contentResponseDtoList.isEmpty()){
throw new RuntimeException("등록된 컨텐츠가 없습니다.");
} else {
return contentResponseDtoList;
return ContentResponseDto.builder()
.total(contentTableListAll.size())
.contentList(contentResponseDtoList)
.build();
}
}
} catch (Exception e) {
Expand All @@ -83,7 +99,7 @@ public List<ContentResponseDto> getContentQueryWithLevel(String query, int level
}

@Override
public List<ContentResponseDto> getContentWithUserId(String userId, String query, int level, int size, int page){
public ContentResponseDto getContentWithUserId(String userId, String query, int level, int size, int page){
IdTable idTable = IdTableRepository.findById(userId).orElseThrow
(() -> new RuntimeException("존재하지 않는 사용자입니다."));
if(idTable.getContentTable().isEmpty()) {
Expand All @@ -92,32 +108,48 @@ public List<ContentResponseDto> getContentWithUserId(String userId, String query
int offset = (page - 1) * size;
try{
if(query.equals("all") && level == -1){
List<ContentResponseDto> contentResponseDtoList = contentRepository.findAllByUserIdPagination(userId, PageRequest.of(page-1, size)).stream().toList();
List<ContentTable> contentTableList = contentRepository.findAllByUserId(idTable);
List<ContentTitleDto> contentResponseDtoList = contentRepository.findAllByUserIdPagination(userId, PageRequest.of(page-1, size)).stream().toList();
if(contentResponseDtoList.isEmpty()){
throw new CustomException("검색 결과가 없습니다.");
} else {
return contentResponseDtoList;
return ContentResponseDto.builder()
.total(contentTableList.size())
.contentList(contentResponseDtoList)
.build();
}
} else if(!query.equals("all") && level == -1){
List<ContentResponseDto> contentResponseDtoList = contentRepository.findAllByUserIdAndQueryContaining(userId, query, PageRequest.of(page-1, size)).stream().toList();
List<ContentTable> contentTableList = contentRepository.findAllByUserIdAndQueryContaining(idTable, query);
List<ContentTitleDto> contentResponseDtoList = contentRepository.findAllByUserIdAndQueryContaining(userId, query, PageRequest.of(page-1, size)).stream().toList();
if(contentResponseDtoList.isEmpty()){
throw new CustomException("검색 결과가 없습니다.");
} else {
return contentResponseDtoList;
return ContentResponseDto.builder()
.total(contentTableList.size())
.contentList(contentResponseDtoList)
.build();
}
} else if(query.equals("all")){
List<ContentResponseDto> contentResponseDtoList = contentRepository.findAllByUserIdAndContentLevel(userId, level, PageRequest.of(page-1, size)).stream().toList();
List<ContentTable> contentTableList = contentRepository.findAllByUserIdAndContentLevel(idTable, level);
List<ContentTitleDto> contentResponseDtoList = contentRepository.findAllByUserIdAndContentLevel(userId, level, PageRequest.of(page-1, size)).stream().toList();
if(contentResponseDtoList.isEmpty()){
throw new CustomException("검색 결과가 없습니다.");
} else {
return contentResponseDtoList;
return ContentResponseDto.builder()
.total(contentTableList.size())
.contentList(contentResponseDtoList)
.build();
}
} else {
List<ContentResponseDto> contentResponseDtoList = contentRepository.findAllByUserIdAndContentLevelAndQueryContaining(userId, level, query, PageRequest.of(page-1, size)).stream().toList();
List<ContentTable> contentTableList = contentRepository.findAllByUserIdAndContentLevelAndQueryContaining(idTable, level, query);
List<ContentTitleDto> contentResponseDtoList = contentRepository.findAllByUserIdAndContentLevelAndQueryContaining(userId, level, query, PageRequest.of(page-1, size)).stream().toList();
if(contentResponseDtoList.isEmpty()){
throw new CustomException("검색 결과가 없습니다.");
} else {
return contentResponseDtoList;
return ContentResponseDto.builder()
.total(contentTableList.size())
.contentList(contentResponseDtoList)
.build();
}
}
} catch (CustomException e) {
Expand Down Expand Up @@ -203,7 +235,7 @@ public TotalContentDto getTotalContent(String userId, int size, int page){
int totalContent = idTable.getContentTable().size();
int totalView = idTable.getContentTable().stream().mapToInt(ContentTable::getContentView).sum();
int totalLike = idTable.getContentTable().stream().mapToInt(ContentTable::getContentLike).sum();
List<ContentResponseDto> contentResponseDtoList = contentRepository.findAllByUserIdPagination(userId, pageable).stream().toList();
List<ContentTitleDto> contentResponseDtoList = contentRepository.findAllByUserIdPagination(userId, pageable).stream().toList();
return TotalContentDto.builder()
.totalContent(totalContent)
.totalView(totalView)
Expand Down