private async Task ValidateTag(NewTagDto newTagDto) { if (newTagDto == null) { throw new ArgumentNullException(nameof(newTagDto)); } else if (await _repository.TagExists(newTagDto.Title)) { throw new ValidationException("Tag already exists"); } else if (newTagDto.Title.Length < 3) { throw new ValidationException("Tag is too short"); } else if (newTagDto.Title.Length > 10) { throw new ValidationException("Tag is too long"); } var regexForTagValidation = new Regex("^[a-zA-Z0-9#-]*$"); if (!regexForTagValidation.IsMatch(newTagDto.Title)) { throw new ValidationException("Tag has invalid characters"); } }
public async Task <IActionResult> UpdateTag(Guid id, TagToUpdateDto tag) { var tagEntity = await _tagsRepo.GetTagAsync(id); if (tagEntity == null) { return(NotFound()); } _mapper.Map(tag, tagEntity); _tagsRepo.UpdateTag(tagEntity); // It might be more proper to handle this error in the repository try { await _tagsRepo.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) when(!_tagsRepo.TagExists(id)) { return(NotFound()); } return(NoContent()); }
public bool TagExists(string tagName) { return(_tagsRepository.TagExists(tagName)); }