/// <summary> /// Creates a new <see cref="Tag"/>. /// </summary> /// <param name="tag">The tag with data to be created.</param> /// <exception cref="MokException">If tag is empty or title exists already.</exception> /// <returns>Created tag.</returns> public async Task <Tag> CreateAsync(Tag tag) { if (tag == null || tag.Title.IsNullOrEmpty()) { throw new MokException($"Invalid tag to create."); } // prep title tag.Title = PrepareTitle(tag.Title); // make sure it is unique var allTags = await GetAllAsync(); if (allTags.Any(t => t.Title.Equals(tag.Title, StringComparison.CurrentCultureIgnoreCase))) { throw new MokException($"'{tag.Title}' already exists."); } // prep slug, description and count tag.Slug = BlogUtil.SlugifyTaxonomy(tag.Title, SLUG_MAXLEN, allTags.Select(c => c.Slug)); // slug is based on title tag.Description = Util.CleanHtml(tag.Description); tag.Count = tag.Count; // create tag = await tagRepository.CreateAsync(tag); // remove cache await cache.RemoveAsync(BlogCache.KEY_ALL_TAGS); await cache.RemoveAsync(BlogCache.KEY_POSTS_INDEX); logger.LogDebug("Created {@Tag}", tag); return(tag); }
public async Task <IActionResult> CreateTagAsync([FromBody] Client.TagCreationInfo creationInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (creationInfo == null) { var error = Responses.BodyIsMissing(nameof(creationInfo)); return(BadRequest(error)); } var modelCreationInfo = Converter.TagCreationInfoConverter.Convert(creationInfo); Model.Tag modelTag; try { modelTag = await repository.CreateAsync(modelCreationInfo, cancellationToken).ConfigureAwait(false); } catch (TagDuplicationException ex) { var error = Responses.DuplicationError(ex.Message, Target); return(BadRequest(error)); } var clientTag = Converter.TagConverter.Convert(modelTag); return(CreatedAtRoute("GetTagRoute", new { id = clientTag.Id }, clientTag)); }
public override async Task <int> CreateAsync(Test test) { int id = await Query.InsertReturningIdAsync <int>(new { test.Name, test.Description }); test.Id = id; foreach (var tag in test.Tags) { tag.TestId = id; } foreach (var question in test.Questions) { question.TestId = id; } await _tagRepository.CreateAsync(test.Tags); await _questionRepository.CreateAsync(test.Questions); return(id); }
public async Task <List <Tag> > GetTagsAndCreateIfNotExistAsync(string[] tagNames) { var result = new List <Tag>(); tagNames = tagNames.Select(t => t.ToLower()).Distinct().ToArray(); var existingTags = await _tagRepository.GetExistingTagsAsync(tagNames); result.AddRange(existingTags); var notExistingTags = tagNames.Except(existingTags.Select(t => t.Name)); for (int i = 0; i < notExistingTags.Count(); i++) { var tag = new Tag() { Name = notExistingTags.ElementAt(i), }; result.Add(tag); await _tagRepository.CreateAsync(tag); } await _tagRepository.SaveChangesAsync(); return(result); }
public async Task <IActionResult> Create(CreateViewModel model) { if (ModelState.IsValid) { if (_tagRepository.NameExists(model.Tag.Name)) { var message = $"Ya existe una etiqueta con el nombre {model.Tag.Name}"; ModelState.AddModelError("Tag.Name", message); return(View(model)); } var result = await _uploadImageService.UploadAndResizeAsync( model.Image, _uploadTo, _maxWidth, _maxHeight ); if (result.Success) { var filename = result.ReturnMessages["filename"]; model.Tag.Image = Path.Combine(_uploadTo, filename); await _tagRepository.CreateAsync(model.Tag); this.AddMessage("success", "Etiqueta creada con éxito"); return(RedirectToAction(nameof(List))); } foreach (var error in result.Errors) { ModelState.AddModelError("", error); } } return(View(model)); }
public async Task <long> CreateAsync(TagDto tag, CancellationToken cancellation = default) { var entity = Mapper.Map <TagDto, Tag>(tag); var result = await _tagStorage.CreateAsync(entity, cancellation); return(result.Id); }
public async Task CreateAsync(TagDto item) { var result = _mapper.Map <Tag>(item); await _tagRepository.CreateAsync(result); await _historyService.CreateAsync(UserAction.Add, "Created tag " + result.Name); }
public async Task <Tag> Create([FromBody] Tag model) { model.CreatedBy = _httpContextAccessor.GetContactId(); _logger.LogInformation($"create.tag ${model.Name}"); var data = await _repository.CreateAsync(model); _logger.LogInformation($"created.tag ${data.Id}"); return(data); }
/// <inheritdoc /> public async Task <long> CreateAsync(TagEdit tag) { var entity = _mapper.Map <Tag>(tag); var id = await _repo.CreateAsync(entity); await _repo.SaveAsync(); return(id); }
public async Task <IEnumerable <TagDto> > CreateIfNotExistAsync(IEnumerable <TagDto> tags) { IEnumerable <TagDto> tagsForSafe = await GetFilteredTags(tags.ToList()); var tag = _mapper.Map <IEnumerable <TagDto>, IEnumerable <Tags> >(tagsForSafe); tag = await _tagRepository.CreateAsync(tag); return(_mapper.Map <IEnumerable <Tags>, IEnumerable <TagDto> >(tag)); }
// -------------------------------------------------------------------- Tags /// <summary> /// Creates a <see cref="Tag"/> and invalidates cache for all tags. /// </summary> /// <param name="tag"></param> /// <returns></returns> public async Task <Tag> CreateTagAsync(Tag tag) { tag = await PrepTaxonomyAsync(tag, ECreateOrUpdate.Create) as Tag; tag = await _tagRepo.CreateAsync(tag); await _cache.RemoveAsync(CACHE_KEY_ALL_TAGS); return(tag); }
public async Task <IActionResult> Post([FromBody] TagDto item) { try { return(Ok(await _repository.CreateAsync(item))); } catch (Exception ex) { return(StatusCode(500, ex)); } }
/// <inheritdoc /> public async Task <TagDto> AddTagAsync(long id, TagEdit tag) { var tagEntity = await _tagRepository.GetByName(tag.Name); if (tagEntity == null) { tagEntity = _mapper.Map <Tag>(tag); await _tagRepository.CreateAsync(tagEntity); } var appliedTagEntity = new AppliedTag() { RecipeId = id, Tag = tagEntity }; await _appliedTagRepository.CreateAsync(appliedTagEntity); await _repo.SaveAsync(); var dto = _mapper.Map <TagDto>(appliedTagEntity); return(dto); }
public async Task <TagDTO> CreateAsync(TagDTO item) { try { if (item.Name != null || item.Description != null) { var tag = _mapper.Map <TagDTO, Tag>(item); await _tagRepository.CreateAsync(tag); return(item); } else { new ArgumentNullException("Данные не заполнены"); return(null); } } catch (ArgumentNullException) { return(null); } }
public async Task <JsonResult> Create(Tag tag) { var tags = await _repository.GetAllAsync(); var exist = tags as IList <Tag> ?? tags.ToList(); if (exist.Contains(tag)) { ModelState.AddModelError("key", "Tag is exiting."); return(Json(RedirectToAction("Index"))); } if (string.IsNullOrWhiteSpace(tag.Name)) { ModelState.AddModelError("key", "New tag value cannot be empty."); return(Json(RedirectToAction("Index"))); } await _repository.CreateAsync(tag); return(Json(tag)); }