Ejemplo n.º 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>
        /// deletes translation async
        /// </summary>
        /// <param name="id">translation id</param>
        public async Task DeleteByIdAsync(int id)
        {
            TextContentTranslation textContentTranslation = await _context.TextContentTranslations.FindAsync(id);

            if (textContentTranslation != null)
            {
                _context.TextContentTranslations.Remove(textContentTranslation);
                await _context.SaveChangesAsync();
            }
            else
            {
                throw new KeyAlreadyExistsException($"Text Content translation with id:{id} could not be found");
            }
        }
        /// <summary>
        /// private method for creating text content translation
        /// </summary>
        /// <param name="text">translation text</param>
        /// <param name="locale">translation language</param>
        /// <param name="textContentId">text content id</param>
        /// <returns>id of newly created translation</returns>
        private async Task <int> CreateTextContentTranslation(string text, Locale locale, int textContentId)
        {
            TextContentTranslation translation = new TextContentTranslation
            {
                Text          = text,
                Locale        = locale,
                TextContentId = textContentId
            };

            await _context.TextContentTranslations.AddAsync(translation);

            await _context.SaveChangesAsync();

            return(translation.Id);
        }
        /// <summary>
        /// updates text content translation
        /// </summary>
        /// <param name="updateDto">instance of <see cref="TextContentTranslationUpdateDto"/></param>
        /// <returns>instance of <see cref="TextContentTranslationDto"/></returns>
        public async Task <TextContentTranslationDto> UpdateAsync(TextContentTranslationUpdateDto updateDto)
        {
            TextContentTranslation contentTranslation = await _context.TextContentTranslations.FindAsync(updateDto.Id);

            if (contentTranslation == null)
            {
                throw new KeyNotFoundException($"Text Content translation with id:{updateDto.Id} could not be found");
            }

            contentTranslation.Text = updateDto.Text;
            _context.TextContentTranslations.Update(contentTranslation);
            await _context.SaveChangesAsync();

            return(new TextContentTranslationDto()
            {
                Id = contentTranslation.Id,
                Locale = contentTranslation.Locale,
                Text = contentTranslation.Text
            });
        }