Exemplo n.º 1
0
        /// <summary>
        /// Retrieve the recent uploads for a username and perform business logic to check existence.
        /// Retries operation when there is an exception and logs all actions.
        /// </summary>
        /// <param name="username">Username of profile score to fetch.</param>
        /// <param name="pagination">Pagination for the operation. Starts at 0.</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>List of UploadResult</returns>
        public async Task <List <UploadResult> > GetRecentUploadsAsync(string username, int pagination, string ipAddress, int failureCount, Exception ex)
        {
            // Escape condition for recursive call if exception is thrown.
            if (failureCount >= Constants.OperationRetry)
            {
                throw ex;
            }

            // List of uploads to return.
            var recentUploads = new List <UploadResult>();

            try
            {
                // Check that the user exists.
                var userExists = await _userManagementService.CheckUserExistenceAsync(username).ConfigureAwait(false);

                if (!userExists)
                {
                    throw new ArgumentException(Constants.UsernameDNE);
                }

                // Perform operation.
                recentUploads = await _uploadService.GetRecentByUploaderAsync(username, pagination).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                // Log everytime we catch an exception.
                await _loggingManager.LogAsync(DateTime.UtcNow.ToString(Constants.LoggingFormatString),
                                               Constants.GetRecentUploadsOperation, username, ipAddress, e.ToString()).ConfigureAwait(false);

                // Retry operation Constant.OperationRetry amount of times when there is exception.
                await GetRecentUploadsAsync(username, pagination, ipAddress, ++failureCount, e);
            }

            // Operation successfull, log that operation.
            await _loggingManager.LogAsync(DateTime.UtcNow.ToString(Constants.LoggingFormatString),
                                           Constants.GetRecentUploadsOperation, username, ipAddress).ConfigureAwait(false);

            return(recentUploads);
        }