Пример #1
0
        /// <summary>
        /// Handles the command. Actual instance of the command being executed is passed-in as argument
        /// </summary>
        /// <param name="command">Actual command instance being executed</param>
        /// <exception cref="InvalidOperationException">Resource with key `{resource.ResourceKey}` already exists</exception>
        public void Execute(CreateNewResources.Command command)
        {
            if (command.LocalizationResources == null || !command.LocalizationResources.Any())
            {
                return;
            }

            var repo = new ResourceRepository(_configurationContext);

            foreach (var resource in command.LocalizationResources)
            {
                var existingResource = repo.GetByKey(resource.ResourceKey);

                if (existingResource != null)
                {
                    throw new InvalidOperationException($"Resource with key `{resource.ResourceKey}` already exists");
                }

                resource.ModificationDate = DateTime.UtcNow;

                // if we are importing single translation and it's not invariant
                // set it also as invariant translation
                if (resource.Translations.Count == 1 && resource.Translations.InvariantTranslation() == null)
                {
                    var t = resource.Translations.First();
                    resource.Translations.Add(new LocalizationResourceTranslation {
                        Value = t.Value, Language = string.Empty
                    });
                }

                repo.InsertResource(resource);

                _configurationContext.BaseCacheManager.StoreKnownKey(resource.ResourceKey);
            }
        }
        /// <summary>
        /// Handles the command. Actual instance of the command being executed is passed-in as argument
        /// </summary>
        /// <param name="command">Actual command instance being executed</param>
        public void Execute(CreateOrUpdateTranslation.Command command)
        {
            var repository = new ResourceRepository(_configurationContext);
            var resource   = repository.GetByKey(command.Key);
            var now        = DateTime.UtcNow;

            if (resource == null)
            {
                return;
            }

            var translation = resource.Translations.FindByLanguage(command.Language);

            if (translation == null)
            {
                var newTranslation = new LocalizationResourceTranslation
                {
                    Value            = command.Translation,
                    Language         = command.Language.Name,
                    ResourceId       = resource.Id,
                    ModificationDate = now
                };

                repository.AddTranslation(resource, newTranslation);
            }
            else
            {
                translation.Value            = command.Translation;
                translation.ModificationDate = now;
                repository.UpdateTranslation(resource, translation);
            }

            resource.ModificationDate = now;
            resource.IsModified       = true;

            repository.UpdateResource(resource);

            _configurationContext.CacheManager.Remove(CacheKeyHelper.BuildKey(command.Key));
        }
Пример #3
0
        /// <summary>
        /// Handles the command. Actual instance of the command being executed is passed-in as argument
        /// </summary>
        /// <param name="command">Actual command instance being executed</param>
        /// <exception cref="ArgumentNullException">Key</exception>
        /// <exception cref="InvalidOperationException">Cannot delete resource `{command.Key}` that is synced with code</exception>
        public void Execute(DeleteResource.Command command)
        {
            if (string.IsNullOrEmpty(command.Key))
            {
                throw new ArgumentNullException(nameof(command.Key));
            }

            var repo     = new ResourceRepository();
            var resource = repo.GetByKey(command.Key);

            if (resource == null)
            {
                return;
            }
            if (resource.FromCode)
            {
                throw new InvalidOperationException($"Cannot delete resource `{command.Key}` that is synced with code");
            }

            repo.DeleteResource(resource);

            ConfigurationContext.Current.CacheManager.Remove(CacheKeyHelper.BuildKey(command.Key));
        }
        /// <summary>
        /// Handles the command. Actual instance of the command being executed is passed-in as argument
        /// </summary>
        /// <param name="command">Actual command instance being executed</param>
        /// <exception cref="InvalidOperationException">Cannot delete translation for not modified resource (key: `{command.Key}`</exception>
        public void Execute(RemoveTranslation.Command command)
        {
            var repository = new ResourceRepository();
            var resource   = repository.GetByKey(command.Key);

            if (resource == null)
            {
                return;
            }
            if (!resource.IsModified.HasValue || !resource.IsModified.Value)
            {
                throw new InvalidOperationException(
                          $"Cannot delete translation for not modified resource (key: `{command.Key}`");
            }

            var t = resource.Translations.FirstOrDefault(_ => _.Language == command.Language.Name);

            if (t != null)
            {
                repository.DeleteTranslation(resource, t);
            }

            ConfigurationContext.Current.CacheManager.Remove(CacheKeyHelper.BuildKey(command.Key));
        }
Пример #5
0
        /// <summary>
        /// Handles the command. Actual instance of the command being executed is passed-in as argument
        /// </summary>
        /// <param name="command">Actual command instance being executed</param>
        /// <exception cref="InvalidOperationException">Resource with key `{resource.ResourceKey}` already exists</exception>
        public void Execute(CreateNewResources.Command command)
        {
            if (command.LocalizationResources == null || !command.LocalizationResources.Any())
            {
                return;
            }

            var repo = new ResourceRepository();

            foreach (var resource in command.LocalizationResources)
            {
                var existingResource = repo.GetByKey(resource.ResourceKey);

                if (existingResource != null)
                {
                    throw new InvalidOperationException($"Resource with key `{resource.ResourceKey}` already exists");
                }

                resource.ModificationDate = DateTime.UtcNow;
                repo.InsertResource(resource);

                ConfigurationContext.Current.BaseCacheManager.StoreKnownKey(resource.ResourceKey);
            }
        }
        /// <summary>
        /// Place where query handling happens
        /// </summary>
        /// <param name="query">This is the query instance</param>
        /// <returns>
        /// You have to return something from the query execution. Of course you can return <c>null</c> as well if you
        /// will.
        /// </returns>
        public LocalizationResource Execute(GetResource.Query query)
        {
            var repository = new ResourceRepository();

            return(repository.GetByKey(query.ResourceKey));
        }