Skip to content
Extraits de code Groupes Projets
Valider 830fe64e rédigé par youssef.achkir's avatar youssef.achkir
Parcourir les fichiers

Rename Document Implementation

parent 283f978b
2 requêtes de fusion!40feature/VSN-1166 - Document Service Refactoring: Folders Part,!38feature/VSN-1160 - Rename Document Implementation
## [0.0.26]
- Add cors configuration
- Allow null values for parent id
- Rename Document Implementation
## [0.0.25]
-
## [0.0.24]
......
......@@ -55,5 +55,6 @@ public class Paths {
public static final String FAVORITE_DOCS = "/favorites";
public static final String MOVE_DOCUMENT = "/move/{documentId}";
public static final String BULK_MOVE_DOCUMENTS = "/bulk-move";
public static final String DOC_RENAME = "/rename";
}
......@@ -174,5 +174,15 @@ public class DocumentController {
return ResponseEntity.ok().build();
}
@PostMapping(Paths.DOC_RENAME)
public ResponseEntity<Void> renameDocument(
@RequestParam Long documentId,
@RequestParam String newName,
@RequestParam Long ownerId
) throws FunctionalException, S3FunctionalException {
documentService.renameDocument(documentId, newName, ownerId);
return ResponseEntity.ok().build();
}
}
......@@ -29,6 +29,7 @@ public class DocumentMapper {
dto.setOwnerId(document.getOwnerId());
dto.setDocumentType(document.getDocumentType());
dto.setFavorite(document.isFavorite());
dto.setCreatedAt(document.getCreatedAt());
try {
String presignedUrl = documentStorageService.getFileSharableLink(bucketName, document.getS3Key());
......
......@@ -46,4 +46,5 @@ public interface DocumentService {
List<DocumentDTO> getFavoriteDocuments(Long ownerId);
void moveDocument(Long documentId, Long targetFolderId, Long ownerId) throws FunctionalException, S3FunctionalException;
void bulkMoveDocuments(List<Long> documentIds, Long targetFolderId, Long ownerId) throws FunctionalException, S3FunctionalException;
public void renameDocument(Long documentId, String newName, Long ownerId) throws FunctionalException, S3FunctionalException;
}
......@@ -473,6 +473,39 @@ public class DocumentServiceImpl implements DocumentService {
}
}
@Override
@Transactional
public void renameDocument(Long documentId, String newName, Long ownerId) throws FunctionalException, S3FunctionalException {
Document doc = documentRepository.findById(documentId)
.orElseThrow(() -> new FunctionalException(MessageConstants.DOCUMENT_NOT_FOUND));
if (doc.getOwnerId() != ownerId) {
throw new FunctionalException(MessageConstants.UNAUTHORIZED_ACCESS);
}
// Vérifier l'unicité du nom dans le même dossier
Long folderId = doc.getFolder() != null ? doc.getFolder().getId() : null;
if (!isDocumentNameUnique(newName, folderId, ownerId)) {
throw new FunctionalException(MessageConstants.DUPLICATE_NAME);
}
// Construire le nouveau chemin et S3Key
Folder folder = doc.getFolder(); // peut être null (dossier racine)
String newPath = DocumentUtils.constructDocumentPath(newName, folder);
String newS3Key = DocumentUtils.constructS3KeyForRename(doc, newName, folder);
// Renommer le fichier dans S3
amazonS3Client.copyObject(bucketName, doc.getS3Key(), bucketName, newS3Key);
s3FileService.deleteSingleFileInBucket(bucketName, doc.getS3Key());
// Mettre à jour le document
doc.setName(newName);
doc.setPath(newPath);
doc.setS3Key(newS3Key);
documentRepository.save(doc);
}
private Folder getOrCreateSharedWithMe(Long targetUserId) {
return folderRepository.findByOwnerIdAndName(targetUserId, "sharedWithMe")
.orElseGet(() -> {
......
......@@ -48,7 +48,13 @@ public class DocumentUtils {
s3Key.append(doc.getName());
return s3Key.toString();
}
public static String constructS3KeyForRename(Document document, String newName, Folder folder) {
String basePath = folder != null
? "document_service_folder/" + document.getOwnerId() + "/" + folder.getName() + "/"
: "document_service_folder/" + document.getOwnerId() + "/";
return basePath + newName;
}
public static ContentType detectContentType(MultipartFile file) throws FunctionalException {
......
0% ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter