示例#1
0
        public Task <int> AddAsync(TextContentTranslationAddDto createDto)
        {
            if (createDto == null)
            {
                throw new ArgumentNullException($"Text Content create dto is empty");
            }

            foreach (TextContentTranslation translation in _translations)
            {
                if (translation.Locale == createDto.Locale)
                {
                    throw new KeyAlreadyExistsException($"Text translation on {createDto.Locale.ToString()} already exists");
                }
            }

            TextContentTranslation newTranslation = new TextContentTranslation()
            {
                Id            = _translations.Count + new Random().Next(9999),
                Locale        = createDto.Locale,
                Text          = createDto.Text,
                TextContentId = createDto.TextContentId
            };

            _translations.Add(newTranslation);

            return(Task.FromResult(newTranslation.Id));
        }
        /// <summary>
        /// adds new translation to content
        /// </summary>
        /// <param name="addDto">instance of <see cref="TextContentTranslationAddDto"/></param>
        /// <returns>id of newly created translation</returns>
        public async Task <int> AddAsync(TextContentTranslationAddDto addDto)
        {
            TextContent textContent = await _context.TextContents.Where(c => c.Id == addDto.TextContentId)
                                      .Include(t => t.Translations)
                                      .FirstOrDefaultAsync();

            if (textContent == null)
            {
                throw new KeyNotFoundException($"Text Content with id:{addDto.TextContentId} could not be found");
            }

            foreach (var item in textContent.Translations)
            {
                if (item.Locale == addDto.Locale)
                {
                    throw new KeyAlreadyExistsException($"Translation with {addDto.Locale.ToString()} already exists");
                }
            }

            return(await CreateTextContentTranslation(addDto.Text, addDto.Locale, addDto.TextContentId));
        }