Esempio n. 1
0
        /// <summary>
        /// Deletes the specified <paramref name="redirect"/>.
        /// </summary>
        /// <param name="redirect">The redirect to be deleted.</param>
        public void DeleteRedirect(IRedirect redirect)
        {
            // Some input validation
            if (redirect == null)
            {
                throw new ArgumentNullException(nameof(redirect));
            }

            // This implementation only supports the "Redirect class"
            if (redirect is not Redirect r)
            {
                throw new ArgumentException($"Redirect type is not supported: {redirect.GetType()}", nameof(redirect));
            }

            // Create a new scope
            using IScope scope = _scopeProvider.CreateScope();

            // Remove the redirect from the database
            try {
                scope.Database.Delete(r.Dto);
            } catch (Exception ex) {
                throw new RedirectsException("Unable to delete redirect from database.", ex);
            }

            // Complete the scope
            scope.Complete();
        }
Esempio n. 2
0
        /// <summary>
        /// Saves the specified <paramref name="redirect"/>.
        /// </summary>
        /// <param name="redirect">The redirected to be saved.</param>
        /// <returns>The saved <paramref name="redirect"/>.</returns>
        public IRedirect SaveRedirect(IRedirect redirect)
        {
            // Some input validation
            if (redirect == null)
            {
                throw new ArgumentNullException(nameof(redirect));
            }

            // This implementation only supports the "Redirect class"
            if (redirect is not Redirect r)
            {
                throw new ArgumentException($"Redirect type is not supported: {redirect.GetType()}", nameof(redirect));
            }

            // Check whether another redirect matches the new URL and query string
            IRedirect existing = GetRedirectByPathAndQuery(redirect.RootKey, redirect.Url, redirect.QueryString);

            if (existing != null && existing.Id != redirect.Id)
            {
                throw new RedirectsException("A redirect with the same URL and query string already exists.");
            }

            // Update the timestamp for when the redirect was modified
            redirect.UpdateDate = DateTime.UtcNow;

            // Update the redirect in the database
            using (var scope = _scopeProvider.CreateScope()) {
                try {
                    scope.Database.Update(r.Dto);
                } catch (Exception ex) {
                    _logger.LogError(ex, "Unable to update redirect into the database.");
                    throw new RedirectsException("Unable to update redirect into the database.", ex);
                }
                scope.Complete();
            }

            return(redirect);
        }