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

use ownerUuid for storage info service

parent 6411b619
Branches
Étiquettes
1 requête de fusion!42feature/VSN-1166 - use ownerUuid for storage info service
## [0.0.28]
- Document service refactoring : Files part
- use ownerUuid for storage info service
## [0.0.28]
-
## [0.0.27]
......
......@@ -7,6 +7,7 @@ import com.marketingconfort.starter.core.exceptions.FunctionalException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
@RestController
......@@ -18,9 +19,10 @@ public class StorageQuotaController {
private final StorageQuotaService storageQuotaService;
@GetMapping(Paths.STORAGE_QUOTA_INFO)
public ResponseEntity<StorageQuotaInfoDTO> getStorageQuotaInfo(@PathVariable Long ownerId) {
@PreAuthorize("@authorizationService.isTokenSubjectOwner(#requestAuthorization, #ownerUuid)")
public ResponseEntity<StorageQuotaInfoDTO> getStorageQuotaInfo(@PathVariable String ownerUuid) {
try {
StorageQuotaInfoDTO quotaInfo = storageQuotaService.getStorageQuotaInfo(ownerId);
StorageQuotaInfoDTO quotaInfo = storageQuotaService.getStorageQuotaInfo(ownerUuid);
return ResponseEntity.ok(quotaInfo);
} catch (FunctionalException e) {
return ResponseEntity.badRequest().build();
......@@ -29,11 +31,12 @@ public class StorageQuotaController {
@GetMapping(Paths.STORAGE_CHECK_AVAILABLE)
@PreAuthorize("@authorizationService.isTokenSubjectOwner(#requestAuthorization, #ownerUuid)")
public ResponseEntity<Boolean> checkAvailableStorage(
@PathVariable Long ownerId,
@PathVariable String ownerUuid,
@RequestParam Long additionalSizeBytes) {
try {
boolean hasSpace = storageQuotaService.hasAvailableStorage(ownerId, additionalSizeBytes);
boolean hasSpace = storageQuotaService.hasAvailableStorage(ownerUuid, additionalSizeBytes);
return ResponseEntity.ok(hasSpace);
} catch (FunctionalException e) {
return ResponseEntity.badRequest().build();
......@@ -41,9 +44,10 @@ public class StorageQuotaController {
}
@GetMapping(Paths.STORAGE_USAGE_PERCENTAGE)
public ResponseEntity<Double> getStorageUsagePercentage(@PathVariable Long ownerId) {
@PreAuthorize("@authorizationService.isTokenSubjectOwner(#requestAuthorization, #ownerUuid)")
public ResponseEntity<Double> getStorageUsagePercentage(@PathVariable String ownerUuid) {
try {
double percentage = storageQuotaService.getStorageUsagePercentage(ownerId);
double percentage = storageQuotaService.getStorageUsagePercentage(ownerUuid);
return ResponseEntity.ok(percentage);
} catch (FunctionalException e) {
return ResponseEntity.badRequest().build();
......@@ -51,9 +55,10 @@ public class StorageQuotaController {
}
@GetMapping(Paths.STORAGE_QUOTA_EXCEEDED)
public ResponseEntity<Boolean> isStorageQuotaExceeded(@PathVariable Long ownerId) {
@PreAuthorize("@authorizationService.isTokenSubjectOwner(#requestAuthorization, #ownerUuid)")
public ResponseEntity<Boolean> isStorageQuotaExceeded(@PathVariable String ownerUuid) {
try {
boolean exceeded = storageQuotaService.isStorageQuotaExceeded(ownerId);
boolean exceeded = storageQuotaService.isStorageQuotaExceeded(ownerUuid);
return ResponseEntity.ok(exceeded);
} catch (FunctionalException e) {
return ResponseEntity.badRequest().build();
......@@ -61,9 +66,10 @@ public class StorageQuotaController {
}
@GetMapping(Paths.STORAGE_TOTAL_USED)
public ResponseEntity<Long> getTotalUsedStorage(@PathVariable Long ownerId) {
@PreAuthorize("@authorizationService.isTokenSubjectOwner(#requestAuthorization, #ownerUuid)")
public ResponseEntity<Long> getTotalUsedStorage(@PathVariable String ownerUuid) {
try {
Long usedStorage = storageQuotaService.getTotalUsedStorage(ownerId);
Long usedStorage = storageQuotaService.getTotalUsedStorage(ownerUuid);
return ResponseEntity.ok(usedStorage);
} catch (FunctionalException e) {
return ResponseEntity.badRequest().build();
......@@ -71,9 +77,10 @@ public class StorageQuotaController {
}
@GetMapping(Paths.STORAGE_AVAILABLE)
public ResponseEntity<Long> getAvailableStorage(@PathVariable Long ownerId) {
@PreAuthorize("@authorizationService.isTokenSubjectOwner(#requestAuthorization, #ownerUuid)")
public ResponseEntity<Long> getAvailableStorage(@PathVariable String ownerUuid) {
try {
Long availableStorage = storageQuotaService.getAvailableStorage(ownerId);
Long availableStorage = storageQuotaService.getAvailableStorage(ownerUuid);
return ResponseEntity.ok(availableStorage);
} catch (FunctionalException e) {
return ResponseEntity.badRequest().build();
......
......@@ -15,6 +15,7 @@ public class StorageQuotaInfoDTO {
// Owner Information
private Long ownerId;
private String ownerUuid;
// Storage Quota Information (Constant 15 MB)
private Long storageQuotaBytes;
......
......@@ -42,4 +42,6 @@ public interface FolderRepository extends JpaRepository<Folder, Long>, JpaSpecif
List<Folder> findByOwnerId(Long ownerId);
Optional<Folder> findByOwnerIdAndName(Long targetUserId, String sharedWithMe);
List<Folder> findByownerUuid(String ownerUuid);
}
......@@ -30,24 +30,24 @@ public class StorageQuotaServiceImpl implements StorageQuotaService {
private final FolderMapper folderMapper;
@Override
@Transactional(readOnly = true)
public StorageQuotaInfoDTO getStorageQuotaInfo(Long ownerId) throws FunctionalException {
if (ownerId == null) {
public StorageQuotaInfoDTO getStorageQuotaInfo(String ownerUuid) throws FunctionalException {
if (ownerUuid == null) {
throw new FunctionalException(MessageConstants.NULL_OWNER_ID);
}
// Calculate total used storage from all folders owned by this owner
Long usedStorage = calculateTotalUsedStorage(ownerId);
Long usedStorage = calculateTotalUsedStorage(ownerUuid);
Long availableStorage = Math.max(0L, StorageQuotaConstants.STORAGE_QUOTA_BYTES - usedStorage);
double usagePercentage = StorageQuotaConstants.STORAGE_QUOTA_BYTES > 0 ? (double) usedStorage / StorageQuotaConstants.STORAGE_QUOTA_BYTES * 100 : 0.0;
boolean isQuotaExceeded = usedStorage > StorageQuotaConstants.STORAGE_QUOTA_BYTES;
// Count total folders owned by this user
List<Folder> ownedFolders = folderRepository.findByOwnerId(ownerId);
List<Folder> ownedFolders = folderRepository.findByownerUuid(ownerUuid);
int totalFolders = ownedFolders.size();
return StorageQuotaInfoDTO.builder()
.ownerId(ownerId)
.ownerUuid(ownerUuid)
.storageQuotaBytes(StorageQuotaConstants.STORAGE_QUOTA_BYTES)
.storageQuotaFormatted(StorageQuotaConstants.STORAGE_QUOTA_FORMATTED)
.usedStorageBytes(usedStorage)
......@@ -63,34 +63,34 @@ public class StorageQuotaServiceImpl implements StorageQuotaService {
@Override
@Transactional(readOnly = true)
public boolean hasAvailableStorage(Long ownerId, Long additionalSizeBytes) throws FunctionalException {
if (ownerId == null || additionalSizeBytes == null) {
public boolean hasAvailableStorage(String ownerUuid, Long additionalSizeBytes) throws FunctionalException {
if (ownerUuid == null || additionalSizeBytes == null) {
throw new FunctionalException(MessageConstants.NULL_VALUE);
}
Long currentUsedStorage = calculateTotalUsedStorage(ownerId);
Long currentUsedStorage = calculateTotalUsedStorage(ownerUuid);
return (currentUsedStorage + additionalSizeBytes) <= StorageQuotaConstants.STORAGE_QUOTA_BYTES;
}
@Override
@Transactional(readOnly = true)
public boolean isStorageQuotaExceeded(Long ownerId) throws FunctionalException {
if (ownerId == null) {
public boolean isStorageQuotaExceeded(String ownerUuid) throws FunctionalException {
if (ownerUuid == null) {
throw new FunctionalException(MessageConstants.NULL_OWNER_ID);
}
Long usedStorage = calculateTotalUsedStorage(ownerId);
Long usedStorage = calculateTotalUsedStorage(ownerUuid);
return StorageQuotaUtils.isExceeded(usedStorage);
}
@Override
@Transactional(readOnly = true)
public double getStorageUsagePercentage(Long ownerId) throws FunctionalException {
if (ownerId == null) {
public double getStorageUsagePercentage(String ownerUuid) throws FunctionalException {
if (ownerUuid == null) {
throw new FunctionalException(MessageConstants.NULL_OWNER_ID);
}
Long usedStorage = calculateTotalUsedStorage(ownerId);
Long usedStorage = calculateTotalUsedStorage(ownerUuid);
if (StorageQuotaConstants.STORAGE_QUOTA_BYTES == 0) {
return 0.0;
......@@ -101,28 +101,28 @@ public class StorageQuotaServiceImpl implements StorageQuotaService {
@Override
@Transactional(readOnly = true)
public Long getTotalUsedStorage(Long ownerId) throws FunctionalException {
if (ownerId == null) {
public Long getTotalUsedStorage(String ownerUuid) throws FunctionalException {
if (ownerUuid == null) {
throw new FunctionalException(MessageConstants.NULL_OWNER_ID);
}
return calculateTotalUsedStorage(ownerId);
return calculateTotalUsedStorage(ownerUuid);
}
@Override
@Transactional(readOnly = true)
public Long getAvailableStorage(Long ownerId) throws FunctionalException {
if (ownerId == null) {
public Long getAvailableStorage(String ownerUuid) throws FunctionalException {
if (ownerUuid == null) {
throw new FunctionalException(MessageConstants.NULL_OWNER_ID);
}
Long usedStorage = calculateTotalUsedStorage(ownerId);
Long usedStorage = calculateTotalUsedStorage(ownerUuid);
return Math.max(0L, StorageQuotaConstants.STORAGE_QUOTA_BYTES - usedStorage);
}
private Long calculateTotalUsedStorage(Long ownerId) {
List<Folder> ownedFolders = folderRepository.findByOwnerId(ownerId);
List<Document> rootDocuments = documentRepository.findByOwnerIdAndFolderIsNull(ownerId);
private Long calculateTotalUsedStorage(String ownerUuid) {
List<Folder> ownedFolders = folderRepository.findByOwnerUuid(ownerUuid);
List<Document> rootDocuments = documentRepository.findByOwnerUuidAndFolderIsNull(ownerUuid);
long foldersSize = ownedFolders.stream()
.mapToLong(folder -> FolderSizeCalculator.calculateFolderSize(folderMapper.toDto(folder)))
......
......@@ -7,20 +7,20 @@ public interface StorageQuotaService {
// storage quota for an owner
StorageQuotaInfoDTO getStorageQuotaInfo(Long ownerId) throws FunctionalException;
StorageQuotaInfoDTO getStorageQuotaInfo(String OwnerUuid) throws FunctionalException;
// Check if owner has enough storage space
boolean hasAvailableStorage(Long ownerId, Long additionalSizeBytes) throws FunctionalException;
boolean hasAvailableStorage(String ownerUuid, Long additionalSizeBytes) throws FunctionalException;
// Check if storage quota is exceeded
boolean isStorageQuotaExceeded(Long ownerId) throws FunctionalException;
boolean isStorageQuotaExceeded(String ownerUuid) throws FunctionalException;
//storage usage percentage
double getStorageUsagePercentage(Long ownerId) throws FunctionalException;
double getStorageUsagePercentage(String ownerUuid) throws FunctionalException;
//total used storage by owner
Long getTotalUsedStorage(Long ownerId) throws FunctionalException;
Long getTotalUsedStorage(String ownerUuid) throws FunctionalException;
// available storage for owner
Long getAvailableStorage(Long ownerId) throws FunctionalException;
Long getAvailableStorage(String ownerUuid) 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