Skip to content

Commit

Permalink
Merge pull request #68 from insea-connect/drives
Browse files Browse the repository at this point in the history
Drives
  • Loading branch information
AmimiHamza authored Jun 6, 2024
2 parents d268367 + 0c4dfe9 commit 7f37546
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.requestMatchers(HttpMethod.GET, "/swagger-ui/**").permitAll()
.requestMatchers(HttpMethod.GET, "/api/user/*").hasRole("ADMIN")
.requestMatchers(HttpMethod.POST, "/user.addUser").hasRole("ADMIN")
.anyRequest().permitAll()
.anyRequest().authenticated()
)
.csrf().disable()
.sessionManagement(sess -> sess.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,33 @@


@RestController
@RequestMapping("/drive")
@RequestMapping("/api/v1")
@RequiredArgsConstructor
public class DriveItemController {

private final Functions functions;

@Autowired
private DriveItemServiceImpl driveItemService;
@Autowired
private ma.insea.connect.drive.service.FolderServiceImpl folderService;
@Autowired
private ma.insea.connect.drive.repository.DegreePathRepository degreePathRepository;
@Autowired
private ma.insea.connect.drive.repository.FileRepository fileRepository;

@GetMapping("/degreePaths/{degreePathCode}/items")
public ResponseEntity<List<DriveItemDto>> getDriveItems(@PathVariable Long degreePathCode) {
@Autowired
private ma.insea.connect.drive.repository.DriveItemRepository driveItemRepository;

@GetMapping("drive/{degreePathId}/folders/{parentId}/items")
public ResponseEntity<List<DriveItemDto>> getDriveItems(@PathVariable Long degreePathId, @PathVariable Long parentId) {
List<DriveItem> driveItems;
if(parentId != 0) {
Folder folder = folderService.getFolderById(parentId);
if(folder == null) {return ResponseEntity.notFound().build();}
driveItems = folder.getChildren();
}else{
driveItems = driveItemRepository.findByDegreePathIdAndParent(degreePathId, null);}
List<DriveItemDto> driveItemDtos = new ArrayList<>();
if (driveItemService.getDriveItems(degreePathCode) == null) {
return ResponseEntity.notFound().build();
}
for(DriveItem driveItem : driveItemService.getDriveItems(degreePathCode)){
if (driveItems == null) {return ResponseEntity.notFound().build();}
for(DriveItem driveItem : driveItems){
DriveItemDto driveItemDto = new DriveItemDto();
DriveUserDto driveUserDto = new DriveUserDto();

Expand All @@ -63,32 +68,19 @@ public ResponseEntity<List<DriveItemDto>> getDriveItems(@PathVariable Long degre
driveItemDto.setUpdatedAt(driveItem.getUpdatedAt());
driveItemDto.setCreator(driveUserDto);
driveItemDto.setDegreePath(driveItem.getDegreePath());

driveItemDto.setParent(null);
if(driveItem instanceof Folder) {
driveItemDto.setFolder(true);
}

if(driveItem instanceof Folder) {driveItemDto.setFolder(true);}
driveItemDtos.add(driveItemDto);
}

return ResponseEntity.ok(driveItemDtos);
}

@PreAuthorize("hasRole('CLASS_REP')")
@PostMapping("/degreePaths/{degreePathCode}/folder")
public ResponseEntity<FolderDto> CreateDriveItem(@PathVariable Long degreePathCode, @RequestBody FolderDto folderDto) {
FolderDto folderDTO=driveItemService.createFolder(degreePathCode, folderDto);
if(folderDTO==null){
return ResponseEntity.badRequest().build();
}
return ResponseEntity.ok(folderDTO);
}

@PreAuthorize("hasRole('CLASS_REP')")
@PostMapping("/degreePaths/{degreePathCode}/upload")
public ResponseEntity<DriveItemDto> handleFileUpload(@RequestParam("file") MultipartFile file,@PathVariable Long degreePathCode) throws Exception{
@PostMapping("drive/{degreePathId}/folders/{parentId}/upload")
public ResponseEntity<DriveItemDto> handleFileUpload(@RequestParam("file") MultipartFile file,@PathVariable Long degreePathId,@PathVariable Long parentId) throws Exception{
User user=functions.getConnectedUser();
DegreePath degreePath = degreePathRepository.findById(degreePathCode).get();
DegreePath degreePath = degreePathRepository.findById(degreePathId).get();

DriveItemDto driveItemDto = new DriveItemDto();
DriveUserDto driveUserDto = new DriveUserDto();
Expand All @@ -100,7 +92,6 @@ public ResponseEntity<DriveItemDto> handleFileUpload(@RequestParam("file") Multi
if (file.isEmpty()) {
return new ResponseEntity(HttpStatus.BAD_REQUEST);
}

String fileUrl = functions.uploadFile(file);

fileObj.setCreatedAt(LocalDateTime.now());
Expand All @@ -110,7 +101,6 @@ public ResponseEntity<DriveItemDto> handleFileUpload(@RequestParam("file") Multi
fileObj.setName(file.getOriginalFilename());
fileObj.setSize(file.getSize());
fileObj.setMimeType(file.getContentType());
fileObj.setParent(null);
fileObj.setFileUrl(fileUrl);

driveUserDto.setUsername(user.getUsername());
Expand All @@ -122,40 +112,21 @@ public ResponseEntity<DriveItemDto> handleFileUpload(@RequestParam("file") Multi
driveItemDto.setSize(file.getSize());
driveItemDto.setMimeType(file.getContentType());
driveItemDto.setCreatedAt(LocalDateTime.now());

driveItemDto.setCreator(driveUserDto);

driveItemDto.setParent(null);
driveItemDto.setDegreePath(degreePath);

if(parentId != 0) {
Folder folder = folderService.getFolderById(parentId);
if(folder == null) {return ResponseEntity.notFound().build();}
fileObj.setParent(folder);
}else{
fileObj.setParent(null);
driveItemDto.setParent(null);
}
fileRepository.save(fileObj);

return ResponseEntity.ok(driveItemDto);

}
@PreAuthorize("hasRole('CLASS_REP')")
@PostMapping("/{folderId}/upload")
public File handleFileUploadOnFolder(@RequestParam("file") MultipartFile file, @PathVariable Long folderId) throws Exception{

User user = functions.getConnectedUser();
if(!functions.checkPermission(user, folderService.getFolderById(folderId).getDegreePath())){
return null;
}
if (file.isEmpty()) {return null;}

File fileObj = new File();
fileObj.setFileUrl(functions.uploadFile(file));
fileObj.setName(file.getOriginalFilename());
fileObj.setSize(file.getSize());
fileObj.setMimeType(file.getContentType());
fileObj.setCreatedAt(LocalDateTime.now());
Folder folder = folderService.getFolderById(folderId);
fileObj.setParent(folder);

fileRepository.save(fileObj);

return fileObj;
}
@GetMapping("/degreePaths")
public List<DegreePath> getDegreePaths() {
return degreePathRepository.findAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/drive/files")
@RequestMapping("api/v1/drive/files")
@RequiredArgsConstructor
public class FileController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import ma.insea.connect.drive.model.File;
import ma.insea.connect.drive.service.DriveItemService;
import ma.insea.connect.drive.service.FolderService;
import ma.insea.connect.user.DegreePath;
import ma.insea.connect.user.User;
import ma.insea.connect.utils.Functions;
import org.springframework.web.bind.annotation.*;
Expand All @@ -16,9 +17,13 @@

import ma.insea.connect.drive.model.DriveItem;
import ma.insea.connect.drive.model.Folder;
import ma.insea.connect.drive.repository.DegreePathRepository;
import ma.insea.connect.drive.repository.FileRepository;
import ma.insea.connect.drive.repository.FolderRepository;
import ma.insea.connect.drive.service.FolderServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -29,93 +34,65 @@
import java.util.List;

@RestController
@RequestMapping("/drive/folders")
@RequestMapping("/api/v1")
@RequiredArgsConstructor
public class FolderController {


@Autowired
private FolderServiceImpl folderService;
private final FolderServiceImpl folderService;
private final Functions functions;
private final DriveItemService driveItemService;

@GetMapping("/{folderId}/items")
public ResponseEntity<List<DriveItemDto>> getItems(@PathVariable Long folderId) {
if (folderService.getFolderById(folderId) == null) {
return ResponseEntity.notFound().build();
}

List<DriveItemDto> driveItemDtos = new ArrayList<>();
for(DriveItem driveItem : folderService.getFolderItems(folderId)){
DriveItemDto driveItemDto = new DriveItemDto();
DriveUserDto driveUserDto = new DriveUserDto();
Folder folder = folderService.getFolderById(folderId);
FolderDto folderDto = new FolderDto();

driveUserDto.setId(driveItem.getCreator().getId());
driveUserDto.setEmail(driveItem.getCreator().getEmail());
driveUserDto.setUsername(driveItem.getCreator().getUsername());

driveItemDto.setId(driveItem.getId());
driveItemDto.setName(driveItem.getName());
driveItemDto.setDescription(driveItem.getDescription());
driveItemDto.setCreatedAt(driveItem.getCreatedAt());
driveItemDto.setUpdatedAt(driveItem.getUpdatedAt());
driveItemDto.setCreator(driveUserDto);
driveItemDto.setDegreePath(driveItem.getDegreePath());

folderDto.setName(folder.getName());
folderDto.setDescription(folder.getDescription());
folderDto.setCreator(driveUserDto);
folderDto.setParent(null);


driveItemDto.setParent(folderDto);
if(driveItem instanceof Folder) {
driveItemDto.setFolder(true);
}
driveItemDtos.add(driveItemDto);
}
return ResponseEntity.ok(driveItemDtos);
}
private final DegreePathRepository degreePathRepository;
private final FolderRepository folderRepository;

@PreAuthorize("hasRole('CLASS_REP')")
@PostMapping("/{folderId}/folder")
public ResponseEntity<FolderDto> createItem(@PathVariable Long folderId, @RequestBody FolderDto folderDto) {

@PostMapping("drive/{degreePathId}/folders/{parentId}/items")
public ResponseEntity<FolderDto> createItem(@PathVariable Long degreePathId, @PathVariable Long parentId, @RequestBody FolderDto folderDto) {
User user = functions.getConnectedUser();
DegreePath degreePath = degreePathRepository.findById(degreePathId).get();
if(!functions.checkPermission(user, degreePath)){
return new ResponseEntity(HttpStatus.UNAUTHORIZED);
}
if(parentId==0){
Folder folder = new Folder();
folder.setName(folderDto.getName());
folder.setCreatedAt(LocalDateTime.now());
folder.setDegreePath(degreePathRepository.findById(degreePathId).get());
folder.setDescription(folderDto.getDescription());
folder.setParent(null);
folder.setCreator(functions.getConnectedUser());
folderRepository.save(folder);
return ResponseEntity.ok(folderDto);
}else{
DriveUserDto driveUserDto = new DriveUserDto();
Folder folder = new Folder();
Folder parent = folderService.getFolderById(parentId);

FolderDto parentDto = new FolderDto();
Folder parent = folderService.getFolderById(folderId);

parentDto.setName(parent.getName());
parentDto.setDescription(parent.getDescription());
parentDto.setCreator(driveUserDto);
parentDto.setParent(null);


folderDto.setParent(parentDto);

Folder folder = new Folder();
folder.setParent(parent);
folder.setName(folderDto.getName());
folder.setCreatedAt(LocalDateTime.now());
folder.setDegreePath(folderService.getFolderById(folderId).getDegreePath());
folder.setDegreePath(degreePathRepository.findById(degreePathId).get());
folder.setDescription(folderDto.getDescription());

folder.setParent(parent);
folder.setCreator(functions.getConnectedUser());

folderRepository.save(folder);

driveUserDto.setId(user.getId());
driveUserDto.setEmail(user.getEmail());
driveUserDto.setUsername(user.getUsername());

folderDto.setCreator(driveUserDto);
folderDto.setParent(parentDto);

if (folderService.createFolderItem(folderId, folder) == null) {
if (folderService.createFolderItem(parentId, folder) == null) {
return ResponseEntity.badRequest().build();
}

return ResponseEntity.ok(folderDto);
return ResponseEntity.ok(folderDto);}
}

@GetMapping("/{folderId}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package ma.insea.connect.drive.repository;

import ma.insea.connect.drive.model.DriveItem;
import ma.insea.connect.drive.model.Folder;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface DriveItemRepository extends JpaRepository<DriveItem, Long> {
public List<DriveItem> findByDegreePathId(Long degreePathId);

public List<DriveItem> findByDegreePathIdAndParent(Long degreePathId,Folder object);
}
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,13 @@ private void loadDummyUsers(UserRepository userRepository,GroupRepository groupR

File fileObj = new File();
fileObj.setFileUrl("/uploads/Chapitre1.pdf");
fileObj.setName("cours_complet.pdf");
fileObj.setName("cours complet");
fileObj.setSize((long)134000);
fileObj.setMimeType("application/pdf");
fileObj.setCreatedAt(LocalDateTime.now());
fileObj.setFileUrl("uploads/cours_complet.pdf");
fileObj.setParent(folder3);
fileObj.setCreator(hamza);
fileRepository.save(fileObj);


Expand Down

0 comments on commit 7f37546

Please sign in to comment.