Ejemplo n.º 1
0
        public async Task <Dictionary <string, string> > CreateResourcesAsync(Dictionary <string, string> resources)
        {
            if (resources == null)
            {
                throw new ArgumentNullException(nameof(resources));
            }

            if (!resources.ContainsKey("key") || string.IsNullOrWhiteSpace(resources["key"]))
            {
                return new Dictionary <string, string> {
                           ["error"] = "key is incorrect"
                }
            }
            ;

            var key           = resources["key"];
            var resourcesList = new List <Resource>();
            var isExist       = await _resourceStore.CheckResourcesExistingByKeyAsync(key);

            if (isExist)
            {
                return new Dictionary <string, string> {
                           ["error"] = $"Key {key} already exists"
                }
            }
            ;

            foreach (var k in resources.Keys.Where(k => k != "key"))
            {
                var culture = await _cultureStore.GetCultureByLanguageAsync(k);

                if (culture == null)
                {
                    return new Dictionary <string, string> {
                               ["error"] = $"Culture {k} doesn't exist"
                    }
                }
                ;

                var resource = new Resource
                {
                    Key       = key,
                    Value     = resources[k],
                    CultureId = culture.Id
                };
                resourcesList.Add(resource);
            }
            await _resourceStore.CreateResourcesAsync(resourcesList);

            return(resources);
        }