com.sippnex.fileblade.controllers.UploadRequestController Maven / Gradle / Ivy
package com.sippnex.fileblade.controllers;
import com.sippnex.fileblade.dtos.UploadRequestDto;
import com.sippnex.fileblade.entities.Directory;
import com.sippnex.fileblade.entities.FilebladeItem;
import com.sippnex.fileblade.entities.UploadRequest;
import com.sippnex.fileblade.services.FilebladeItemService;
import com.sippnex.fileblade.services.UploadRequestService;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.Optional;
@RestController
@RequestMapping("fileblade/upload-request")
public class UploadRequestController {
final UploadRequestService uploadRequestService;
final FilebladeItemService filebladeItemService;
public UploadRequestController(UploadRequestService uploadRequestService, FilebladeItemService filebladeItemService) {
this.uploadRequestService = uploadRequestService;
this.filebladeItemService = filebladeItemService;
}
@PostMapping("")
public Long uploadRequest(@RequestBody UploadRequestDto uploadRequestDto) {
UploadRequest uploadRequest = new UploadRequest();
uploadRequest.setFileName(uploadRequestDto.getFileName());
uploadRequest.setCreationDate(new Date());
if(uploadRequestDto.getParentDirectoryId() != null && uploadRequestDto.getParentDirectoryId() > 0 ) { // Subfolder File (with parent directory)
Optional optionalParentDirectory = filebladeItemService.findById(uploadRequestDto.getParentDirectoryId());
if(!optionalParentDirectory.isPresent() || !(optionalParentDirectory.get() instanceof Directory)) {
System.out.println("Upload-Request failed: parent-directory with id " + uploadRequestDto.getParentDirectoryId() + " not found");
return (long)0;
}
uploadRequest.setParentDirectory((Directory)optionalParentDirectory.get());
}
uploadRequest = uploadRequestService.save(uploadRequest);
return uploadRequest.getId();
}
}