-
Notifications
You must be signed in to change notification settings - Fork 7
Pagination and sorting for file listings #17
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| package cloudpage.controller; | ||
|
|
||
| import cloudpage.service.FileEntityService; | ||
| import cloudpage.service.FileService; | ||
| import cloudpage.service.FolderService; | ||
| import cloudpage.service.UserService; | ||
|
|
@@ -10,6 +11,11 @@ | |
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
| import cloudpage.dto.FileDto; | ||
| import cloudpage.model.File; | ||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.data.domain.Sort; | ||
| import org.springframework.data.domain.Pageable; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
|
|
@@ -24,6 +30,8 @@ public class FileController { | |
| private final FileService fileService; | ||
| private final UserService userService; | ||
| private final FolderService folderService; | ||
| private final FileEntityService fileEntityService; | ||
| ; | ||
|
|
||
| @PostMapping("/upload") | ||
| public void uploadFile(@RequestParam String folderPath, @RequestParam MultipartFile file) throws IOException { | ||
|
|
@@ -88,4 +96,23 @@ public ResponseEntity<Resource> viewFile(@RequestParam String path) throws IOExc | |
| .header(HttpHeaders.CONTENT_TYPE, mimeType) | ||
| .body(resource); | ||
| } | ||
| @GetMapping("/list") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new /list endpoint introduces database-backed pagination via FileEntityService. |
||
| public ResponseEntity<FileDto<File>> listFilesWithPagination( | ||
| @RequestParam(defaultValue = "0") int page, | ||
| @RequestParam(defaultValue = "10") int size, | ||
| @RequestParam(defaultValue = "name,asc") String[] sort | ||
| ) { | ||
| // Example sort param: ["name", "asc"] | ||
| Sort.Direction direction = sort[1].equalsIgnoreCase("desc") ? Sort.Direction.DESC : Sort.Direction.ASC; | ||
| Pageable pageable = PageRequest.of(page, size, Sort.by(direction, sort[0])); | ||
| var filePage = fileEntityService.getAllFiles(pageable); | ||
|
|
||
| FileDto<File> response = new FileDto<>( | ||
| filePage.getContent(), | ||
| filePage.getNumber(), | ||
| filePage.getTotalElements(), | ||
| filePage.getTotalPages() | ||
| ); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that its smart to save the files in the db. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package cloudpage.model; | ||
|
|
||
| import javax.persistence.Entity; | ||
| import javax.persistence.Table; | ||
| import javax.persistence.Id; | ||
| import javax.persistence.GeneratedValue; | ||
| import javax.persistence.GenerationType; | ||
|
|
||
| @Entity | ||
| @Table(name = "files") | ||
| public class File { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| private String name; | ||
|
|
||
| private String path; | ||
|
|
||
| private Long size; | ||
|
|
||
| private String fileType; | ||
|
|
||
| // Constructors | ||
| public File() {} | ||
|
|
||
| public File(String name, String path, Long size, String fileType) { | ||
| this.name = name; | ||
| this.path = path; | ||
| this.size = size; | ||
| this.fileType = fileType; | ||
| } | ||
|
|
||
| // Getters and setters | ||
| public Long getId() { return id; } | ||
| public void setId(Long id) { this.id = id; } | ||
|
|
||
| public String getName() { return name; } | ||
| public void setName(String name) { this.name = name; } | ||
|
|
||
| public String getPath() { return path; } | ||
| public void setPath(String path) { this.path = path; } | ||
|
|
||
| public Long getSize() { return size; } | ||
| public void setSize(Long size) { this.size = size; } | ||
|
|
||
| public String getFileType() { return fileType; } | ||
| public void setFileType(String fileType) { this.fileType = fileType; } | ||
| } |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn’t exist unless the project’s architecture explicitly shifts from filesystem-based storage to database-indexed file metadata. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package cloudpage.repository; | ||
|
|
||
| import cloudpage.model.File; | ||
| import org.springframework.data.repository.PagingAndSortingRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| @Repository | ||
| public interface FileRepository extends PagingAndSortingRepository<File, Long> { | ||
| // Define custom queries here (if needed) | ||
| } | ||
|
|
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you dont need this. There already exists a FileService |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package cloudpage.service; | ||
|
|
||
| import cloudpage.model.File; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
|
|
||
| public interface FileEntityService { | ||
| Page<File> getAllFiles(Pageable pageable); | ||
| } |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This FileEntityService and DB-based approach is unnecessary—FileService already handles all filesystem operations, and pagination should be implemented in FolderService directly! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package cloudpage.service.impl; | ||
|
|
||
| import cloudpage.model.File; | ||
| import cloudpage.repository.FileRepository; | ||
| import cloudpage.service.FileEntityService; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
| public class FileEntityServiceImpl implements FileEntityService { | ||
|
|
||
| @Autowired | ||
| private FileRepository fileRepository; | ||
|
|
||
| @Override | ||
| public Page<File> getAllFiles(Pageable pageable) { | ||
| return fileRepository.findAll(pageable); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove these lines from the readme