Ejemplo n.º 1
0
        public async Task <PhraseDto> Create(CreatePhraseDto phrase)
        {
            await DeleteRemovedPhrase(phrase.Text);

            var model = _mapper.Map <Phrase>(phrase);

            _dbContext.Add(model);
            await SaveAndCheckUnique(phrase);

            return(_mapper.Map <PhraseDto>(model));
        }
Ejemplo n.º 2
0
        async Task SaveAndCheckUnique(CreatePhraseDto phrase)
        {
            try
            {
                await _dbContext.SaveChangesAsync();
            }
            catch (DbUpdateException ex) when(ex.InnerException is SqlException sql && sql.Number == 2601)
            {
                var text = phrase.Text.Length > 10 ? phrase.Text.Substring(0, 10) + "..." : phrase.Text;

                throw new AppException($"The phrase with the text '{text}' is already exists");
            }
        }
Ejemplo n.º 3
0
 public Task <PhraseDto> CreatePhrase(CreatePhraseDto phrase) => _phrasesService.Create(phrase);