diff --git a/src/main/java/com/teamh/khumon/controller/LearningMaterialController.java b/src/main/java/com/teamh/khumon/controller/LearningMaterialController.java index 7ce43fa..d288f79 100644 --- a/src/main/java/com/teamh/khumon/controller/LearningMaterialController.java +++ b/src/main/java/com/teamh/khumon/controller/LearningMaterialController.java @@ -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; @@ -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); + } + + + + } diff --git a/src/main/java/com/teamh/khumon/dto/LearningMaterialContent.java b/src/main/java/com/teamh/khumon/dto/LearningMaterialContent.java new file mode 100644 index 0000000..911ea5b --- /dev/null +++ b/src/main/java/com/teamh/khumon/dto/LearningMaterialContent.java @@ -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; + +} diff --git a/src/main/java/com/teamh/khumon/dto/LearningMaterialResponse.java b/src/main/java/com/teamh/khumon/dto/LearningMaterialResponse.java index f5d63a4..509ea08 100644 --- a/src/main/java/com/teamh/khumon/dto/LearningMaterialResponse.java +++ b/src/main/java/com/teamh/khumon/dto/LearningMaterialResponse.java @@ -19,7 +19,6 @@ public class LearningMaterialResponse { private String mediaFileType; private LocalDateTime createdDateTime; private LocalDateTime modifiedDateTime; - private String script; private String summary; private List questionInformations; diff --git a/src/main/java/com/teamh/khumon/dto/OffsetPagination.java b/src/main/java/com/teamh/khumon/dto/OffsetPagination.java new file mode 100644 index 0000000..8a5bd97 --- /dev/null +++ b/src/main/java/com/teamh/khumon/dto/OffsetPagination.java @@ -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 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; +} diff --git a/src/main/java/com/teamh/khumon/repository/LearningMaterialRepository.java b/src/main/java/com/teamh/khumon/repository/LearningMaterialRepository.java index 4ab94f4..bf71023 100644 --- a/src/main/java/com/teamh/khumon/repository/LearningMaterialRepository.java +++ b/src/main/java/com/teamh/khumon/repository/LearningMaterialRepository.java @@ -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 { -} + + Page findByMemberIdAndTitleContainingOrContentContaining(Long memberId, String title, String content, Pageable pageable); +} \ No newline at end of file diff --git a/src/main/java/com/teamh/khumon/service/LearningMaterialService.java b/src/main/java/com/teamh/khumon/service/LearningMaterialService.java index 43dcec7..3946e08 100644 --- a/src/main/java/com/teamh/khumon/service/LearningMaterialService.java +++ b/src/main/java/com/teamh/khumon/service/LearningMaterialService.java @@ -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; @@ -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 @@ -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 learningMaterials = learningMaterialRepository.findByMemberIdAndTitleContainingOrContentContaining(member.getId(), search, search, pageable); + List 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, HttpStatus.OK); + } } \ No newline at end of file