コード例 #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>
        public void Execute(CreateOrUpdateTranslation.Command command)
        {
            var repository = new ResourceRepository();
            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.Current.CacheManager.Remove(CacheKeyHelper.BuildKey(command.Key));
        }
コード例 #2
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));
        }
コード例 #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="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;

                // 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.Current.BaseCacheManager.StoreKnownKey(resource.ResourceKey);
            }
        }
コード例 #4
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">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);
            }
        }
コード例 #6
0
        /// <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 IEnumerable <LocalizationResource> Execute(GetAllResources.Query query)
        {
            var repository = new ResourceRepository(_configurationContext);

            return(repository.GetAll());
        }
コード例 #7
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>
        public void Execute(DeleteAllResources.Command command)
        {
            var repo = new ResourceRepository();

            repo.DeleteAllResources();
        }
コード例 #8
0
        private IEnumerable <CultureInfo> GetAvailableLanguages(bool includeInvariant)
        {
            var repo = new ResourceRepository(_configurationContext);

            return(repo.GetAvailableLanguages(includeInvariant));
        }
コード例 #9
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>
        public void Execute(DeleteAllResources.Command command)
        {
            var repo = new ResourceRepository(_configurationContext);

            repo.DeleteAllResources();
        }
コード例 #10
0
        /// <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(_configurationContext);

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