public async Task <Result <bool> > DeleteUploadAsync(string username, int id, string ipAddress, int failureCount) { var result = false; // Escape condition for recursive call if exception is thrown. if (failureCount >= Constants.OperationRetry) { return(SystemUtilityService.CreateResult(Constants.UploadDeletionFailedMessage, result, true)); } try { result = await _uploadService.DeleteUploadsAsync(new List <int>() { id }).ConfigureAwait(false); } catch (Exception ex) { // Log exception. await _loggingManager.LogAsync(DateTime.UtcNow.ToString(Constants.LoggingFormatString), Constants.DeleteUploadOperation, username, ipAddress, ex.ToString()).ConfigureAwait(false); // Recursively retry the operation until the maximum amount of retries is reached. await ContinueUploadProgressAsync(username, id, ipAddress, ++failureCount).ConfigureAwait(false); } // Log the fact that the operation was successful. await _loggingManager.LogAsync(DateTime.UtcNow.ToString(Constants.LoggingFormatString), Constants.DeleteUploadOperation, username, ipAddress).ConfigureAwait(false); return(SystemUtilityService.CreateResult(Constants.UploadDeletionSuccessMessage, result, false)); }
/// <summary> /// Delete an upload and perform business logic to check for user existence and permission. /// Retries operation when there is an exception and logs all actions. /// </summary> /// <param name="ids">Ids of uploads to delete.</param> /// <param name="performingUser">User that is trying to delete the upload.</param> /// <param name="ipAddress">Ipaddress for logging.</param> /// <param name="failureCount">Current failure count of the operation.</param> /// <param name="ex">Exception that is thrown.</param> /// <returns>bool that represents whether the operation passed.</returns> public async Task <bool> DeleteUploadsAsync(List <int> ids, string performingUser, string ipAddress, int failureCount, Exception ex) { // Escape condition for recursive call if exception is thrown. if (failureCount >= Constants.OperationRetry) { throw ex; } // Condition to return. bool deleteResult = false; try { // Check that the user exists. var userExists = await _userManagementService.CheckUserExistenceAsync(performingUser).ConfigureAwait(false); if (!userExists) { throw new ArgumentException(Constants.UsernameDNE); } // Check that all the Ids exists. if (!await _uploadService.CheckUploadsExistenceAsync(ids).ConfigureAwait(false)) { throw new ArgumentException(Constants.UploadIdsDNE); } // Get user type. var userType = await _userManagementService.GetUserTypeAsync(performingUser).ConfigureAwait(false); // Check is user is admin. if true then let him perform if (userType.Equals(Constants.AdminUserType)) { // Let execution continue. } else if (userType.Equals(Constants.CustomerUserType) || userType.Equals(Constants.StoreOwnerUserType)) { // Check if use is allowed to perform operation if (!await _uploadService.CheckUploadOwnerAsync(ids, performingUser).ConfigureAwait(false)) { throw new NotAuthorizedException(Constants.UserNotAllowed); } } else { throw new NotAuthorizedException(Constants.UserNotAllowed); } // Perform operation. deleteResult = await _uploadService.DeleteUploadsAsync(ids).ConfigureAwait(false); } catch (Exception e) { // Log everytime we catch an exception. await _loggingManager.LogAsync(DateTime.UtcNow.ToString(Constants.LoggingFormatString), Constants.DeleteUploadOperation, performingUser, ipAddress, e.ToString()).ConfigureAwait(false); // Retry operation Constant.OperationRetry amount of times when there is exception await DeleteUploadsAsync(ids, performingUser, ipAddress, ++failureCount, e).ConfigureAwait(false); } // Log the successful operations. await _loggingManager.LogAsync(DateTime.UtcNow.ToString(Constants.LoggingFormatString), Constants.DeleteUploadOperation, performingUser, ipAddress).ConfigureAwait(false); return(deleteResult); }