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: 백문백답 목록 조회 API를 구현한다. #57

Merged
merged 15 commits into from
Oct 14, 2024
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 @@ -11,6 +11,9 @@
import com.dnd.accompany.domain.auth.dto.jwt.JwtAuthentication;
import com.dnd.accompany.domain.qna100.api.dto.CreateAndUpdateQnaRequest;
import com.dnd.accompany.domain.qna100.api.dto.DeleteQnaRequest;
import com.dnd.accompany.domain.qna100.api.dto.PageRequest;
import com.dnd.accompany.domain.qna100.api.dto.PageResponse;
import com.dnd.accompany.domain.qna100.api.dto.QnaThumbnail;
import com.dnd.accompany.domain.qna100.service.QnaService;

import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -31,7 +34,7 @@ public class QnaController {
public ResponseEntity<Void> createAndUpdate(
@RequestBody @Valid CreateAndUpdateQnaRequest request,
@AuthenticationPrincipal JwtAuthentication user) {
qnaService.saveAndUpdate(user.getId(), request);
qnaService.upsert(user.getId(), request);
return ResponseEntity.ok().build();
}

Expand All @@ -43,4 +46,12 @@ public ResponseEntity<Void> delete(
qnaService.delete(user.getId(), request);
return ResponseEntity.ok().build();
}

@Operation(summary = "백문백답 목록 조회")
@PostMapping("/all")
public ResponseEntity<PageResponse<QnaThumbnail>> readAll(
@RequestBody @Valid PageRequest request,
@AuthenticationPrincipal JwtAuthentication user) {
return ResponseEntity.ok(qnaService.getAllQnas(user.getId(), request));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.dnd.accompany.domain.qna100.api.dto;

import java.util.List;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.SliceImpl;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.experimental.SuperBuilder;

@Getter
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@SuperBuilder
public abstract class FindSlicesResult {
private final Long cursor;

public static <T extends FindSlicesResult> Long getLastCursor(List<T> result) {
if (result.isEmpty())
return null;

return result.get(result.size() - 1).getCursor();
}

public static <T extends FindSlicesResult> Slice<T> createSlice(int size, List<T> content) {
boolean hasNext = content.size() > size;

if (hasNext) {
content.remove(content.size() - 1);
}

return new SliceImpl<>(content, PageRequest.of(0, size), hasNext);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.dnd.accompany.domain.qna100.api.dto;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.dsl.NumberPath;
import com.querydsl.core.types.dsl.StringTemplate;

import io.swagger.v3.oas.annotations.media.Schema;

public record PageRequest(
@Schema(nullable = true) Long cursor,
@Schema(nullable = true) Integer size
) {

public PageRequest {
if (size == null) {
size = 10;
}
}

public static BooleanBuilder cursorCondition(Long cursor, NumberPath<Long> nextCursor) {
BooleanBuilder builder = new BooleanBuilder();

if (cursor == null)
return builder;

builder.and(nextCursor.gt(cursor));

return builder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.dnd.accompany.domain.qna100.api.dto;

import java.util.List;

public record PageResponse<T>(
boolean hasNext,
List<T> data,
Long cursor
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.dnd.accompany.domain.qna100.api.dto;

import com.dnd.accompany.domain.qna100.entity.Qna100;

import lombok.Getter;
import lombok.experimental.SuperBuilder;

@Getter
@SuperBuilder
public class QnaThumbnail extends FindSlicesResult {
private Long id;
private String question;
private String answer;

public QnaThumbnail(Long id, String question, String answer) {
super(id);

this.id = id;
this.question = question;
this.answer = answer;
}

public QnaThumbnail fromEntity(Qna100 qna100) {
return QnaThumbnail.builder()
.id(qna100.getId())
.question(qna100.getQuestion())
.answer(qna100.getAnswer())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package com.dnd.accompany.domain.qna100.infrastructure.querydsl;

import static com.dnd.accompany.domain.qna100.api.dto.FindSlicesResult.*;
import static com.dnd.accompany.domain.qna100.api.dto.PageRequest.*;
import static com.dnd.accompany.domain.qna100.entity.QQna100.*;

import java.util.List;

import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Repository;

import com.dnd.accompany.domain.qna100.api.dto.QnaThumbnail;
import com.dnd.accompany.domain.qna100.infrastructure.querydsl.interfaces.QnaRepositoryCustom;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;

import lombok.RequiredArgsConstructor;
Expand All @@ -13,6 +22,21 @@ public class QnaRepositoryImpl implements QnaRepositoryCustom {

private final JPAQueryFactory queryFactory;



@Override
public Slice<QnaThumbnail> findQnaThumbnails(Long cursor, int size, Long userId) {
List<QnaThumbnail> content = queryFactory
.select(Projections.constructor(QnaThumbnail.class,
qna100.id,
qna100.question,
qna100.answer
))
.from(qna100)
.where(qna100.userId.eq(userId))
.where(cursorCondition(cursor, qna100.id))
.orderBy(qna100.id.asc())
.limit(size + 1)
.fetch();

return createSlice(size, content);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.dnd.accompany.domain.qna100.infrastructure.querydsl.interfaces;

public interface QnaRepositoryCustom {
import org.springframework.data.domain.Slice;

import com.dnd.accompany.domain.qna100.api.dto.QnaThumbnail;

public interface QnaRepositoryCustom {
Slice<QnaThumbnail> findQnaThumbnails(Long cursor, int size, Long userId);
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package com.dnd.accompany.domain.qna100.service;

import static com.dnd.accompany.domain.qna100.api.dto.FindSlicesResult.*;
import static java.util.stream.Collectors.*;

import java.util.List;
import java.util.Map;
import java.util.function.Function;

import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dnd.accompany.domain.qna100.api.dto.CreateAndUpdateQnaRequest;
import com.dnd.accompany.domain.qna100.api.dto.DeleteQnaRequest;
import com.dnd.accompany.domain.qna100.api.dto.PageRequest;
import com.dnd.accompany.domain.qna100.api.dto.PageResponse;
import com.dnd.accompany.domain.qna100.api.dto.QnaThumbnail;
import com.dnd.accompany.domain.qna100.entity.Qna100;
import com.dnd.accompany.domain.qna100.exception.Qna100AccessDeniedException;
import com.dnd.accompany.domain.qna100.exception.Qna100NotFoundException;
Expand All @@ -26,7 +31,7 @@ public class QnaService {
private final QnaRepository qnaRepository;

@Transactional
public void saveAndUpdate(Long userId, CreateAndUpdateQnaRequest request) {
public void upsert(Long userId, CreateAndUpdateQnaRequest request) {
Map<Boolean, List<Qna100>> qnaMap = request.toMap(userId, request.qnas());

List<Qna100> newQnas = qnaMap.get(true);
Expand All @@ -51,10 +56,19 @@ public void delete(Long userId, DeleteQnaRequest request) {
Qna100 qna = qnaRepository.findFirstByUserId(userId)
.orElseThrow(() -> new Qna100NotFoundException(ErrorCode.QNA100_NOT_FOUND));

if(!userId.equals(qna.getUserId())){
if (!userId.equals(qna.getUserId())) {
throw new Qna100AccessDeniedException(ErrorCode.QNA100_ACCESS_DENIED);
}

qnaRepository.deleteByIdIn(request.ids());
}

@Transactional(readOnly = true)
public PageResponse<QnaThumbnail> getAllQnas(Long userId, PageRequest request) {
Slice<QnaThumbnail> sliceResult = qnaRepository.findQnaThumbnails(request.cursor(), request.size(), userId);

List<QnaThumbnail> qnaThumbnails = sliceResult.getContent();

return new PageResponse<>(sliceResult.hasNext(), qnaThumbnails, getLastCursor(qnaThumbnails));
}
}