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)); }
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"); } }
public Task <PhraseDto> CreatePhrase(CreatePhraseDto phrase) => _phrasesService.Create(phrase);