Esempio n. 1
0
        /// <summary>
        /// Adds or updates the given model in the database
        /// depending on its state.
        /// </summary>
        /// <param name="model">The model</param>
        public async Task SaveAsync(Alias model)
        {
            // Ensure id
            if (model.Id == Guid.Empty)
            {
                model.Id = Guid.NewGuid();
            }

            // Validate model
            var context = new ValidationContext(model);

            Validator.ValidateObject(model, context, true);

            // Fix urls
            if (!model.AliasUrl.StartsWith("/"))
            {
                model.AliasUrl = "/" + model.AliasUrl;
            }
            if (!model.RedirectUrl.StartsWith("/") && !model.RedirectUrl.StartsWith("http://") && !model.RedirectUrl.StartsWith("https://"))
            {
                model.RedirectUrl = "/" + model.RedirectUrl;
            }

            // Ensure url uniqueness
            var alias = await _repo.GetByAliasUrl(model.AliasUrl, model.SiteId).ConfigureAwait(false);

            if (alias != null && alias.Id != model.Id)
            {
                throw new ValidationException($"The AliasUrl field must be unique");
            }

            // Call hooks & save
            App.Hooks.OnBeforeSave(model);
            await _repo.Save(model).ConfigureAwait(false);

            App.Hooks.OnAfterSave(model);

            // Remove from cache
            RemoveFromCache(model);
        }