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: offset pagination을 통한 전체 강의내역 조회 #48

Merged
merged 5 commits into from
Oct 31, 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
Expand Up @@ -2,6 +2,9 @@

import com.teamh.khumon.service.LearningMaterialService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
Expand Down Expand Up @@ -33,5 +36,14 @@ public ResponseEntity<?> getLearningMaterial(@PathVariable(name = "learningMater
}


@PreAuthorize("isAuthenticated()")
@GetMapping("/learning-materials") //
public ResponseEntity<?> getLearningMaterials(Principal principal, @PageableDefault(sort = "id", direction = Sort.Direction.DESC, size = 10) Pageable pageable, @RequestParam(required = false, defaultValue = "") String search){
return learningMaterialService.getLearningMaterials(principal, pageable, search);
}




}

21 changes: 21 additions & 0 deletions src/main/java/com/teamh/khumon/dto/LearningMaterialContent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.teamh.khumon.dto;


import lombok.*;

import java.time.LocalDateTime;

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
@Builder
public class LearningMaterialContent {

private String title;
private String content;
private LocalDateTime createAt;
private LocalDateTime modifiedAt;

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class LearningMaterialResponse {
private String mediaFileType;
private LocalDateTime createdDateTime;
private LocalDateTime modifiedDateTime;
private String script;
private String summary;
private List<QuestionInformation> questionInformations;

Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/teamh/khumon/dto/OffsetPagination.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.teamh.khumon.dto;


import lombok.*;

import java.util.List;

@Builder
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class OffsetPagination {
private List<LearningMaterialContent> content;
private Long totalElements;
private Integer totalPages;
private Integer size;
private Integer pageNumber;
private Integer numberOfElements;
private Boolean isFirst;
private Boolean isLast;
private Boolean isEmpty;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.teamh.khumon.repository;

import com.teamh.khumon.domain.LearningMaterial;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

public interface LearningMaterialRepository extends JpaRepository<LearningMaterial, Long> {
}

Page<LearningMaterial> findByMemberIdAndTitleContainingOrContentContaining(Long memberId, String title, String content, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package com.teamh.khumon.service;


import com.amazonaws.services.s3.AmazonS3Client;
import com.teamh.khumon.domain.LearningMaterial;
import com.teamh.khumon.domain.MediaFileType;
import com.teamh.khumon.domain.Member;
import com.teamh.khumon.domain.Question;
import com.teamh.khumon.dto.AIResponse;
import com.teamh.khumon.dto.LearningMaterialResponse;
import com.teamh.khumon.dto.LearningRequest;
import com.teamh.khumon.dto.QuestionInformation;
import com.teamh.khumon.dto.*;
import com.teamh.khumon.repository.LearningMaterialRepository;
import com.teamh.khumon.repository.MemberRepository;
import com.teamh.khumon.util.AmazonS3Util;
Expand All @@ -18,19 +14,18 @@
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
Expand Down Expand Up @@ -125,4 +120,29 @@ public ResponseEntity<?> getLearningMaterial(Long id, Principal principal) {
}


public ResponseEntity<?> getLearningMaterials(Principal principal, Pageable pageable, String search) {
Member member = memberRepository.findByUsername(principal.getName()).orElseThrow();
Page<LearningMaterial> learningMaterials = learningMaterialRepository.findByMemberIdAndTitleContainingOrContentContaining(member.getId(), search, search, pageable);
List<LearningMaterialContent> learningMaterialContents = learningMaterials.getContent().stream().map(learningMaterial -> LearningMaterialContent.builder()
.title(learningMaterial.getTitle())
.content(learningMaterial.getContent())
.createAt(learningMaterial.getCreatedAt())
.modifiedAt(learningMaterial.getUpdateAt())
.build()).toList();

OffsetPagination offsetPagination = OffsetPagination.builder()
.content(learningMaterialContents)
.isEmpty(learningMaterials.isEmpty())
.isFirst(learningMaterials.isFirst())
.isLast(learningMaterials.isLast())
.numberOfElements(learningMaterials.getNumberOfElements())
.pageNumber(learningMaterials.getNumber())
.size(learningMaterials.getSize())
.totalElements(learningMaterials.getTotalElements())
.totalPages(learningMaterials.getTotalPages())
.build();


return new ResponseEntity<OffsetPagination>(offsetPagination, HttpStatus.OK);
}
}
Loading