/// <summary> /// Replaces the tag or if it doesn't exist, creates it. /// </summary> /// <param name="request">The request.</param> /// <returns> /// <c>PutTagResponse</c> with a tag id. /// </returns> public PutTagResponse Put(PutTagRequest request) { var tagName = request.Data.Name.Trim(); var tagsByIdFuture = repository.AsQueryable <Module.Root.Models.Tag>().Where(tag1 => tag1.Id == request.Id).ToFuture(); var tagsByIdNameFuture = repository.AsQueryable <Module.Root.Models.Tag>().Where(tag1 => tag1.Name == tagName).ToFuture(); var tagById = tagsByIdFuture.FirstOrDefault(); var tagByName = tagsByIdNameFuture.FirstOrDefault(); // Validate. if (tagById != null && tagByName != null && tagById.Id != tagByName.Id) { var logMessage = string.Format("Failed to rename tag. Tag with the same name already exists. Name: '{0}'.", tagName); throw new CmsApiValidationException(logMessage); } if (tagById == null && tagByName != null) { var logMessage = string.Format("Failed to create a tag. Tag with the same name already exists. Name: '{0}'.", tagName); throw new CmsApiValidationException(logMessage); } // Create or update. var createTag = tagById == null; if (createTag) { tagById = new Module.Root.Models.Tag { Id = request.Id.GetValueOrDefault() }; } else if (request.Data.Version > 0) { tagById.Version = request.Data.Version; } tagById.Name = tagName; repository.Save(tagById); unitOfWork.Commit(); // Fire events. if (createTag) { Events.RootEvents.Instance.OnTagCreated(tagById); } else { Events.RootEvents.Instance.OnTagUpdated(tagById); } return(new PutTagResponse { Data = tagById.Id, }); }
public async Task <ActionResult> CreateTag([FromBody] PutTagRequest request) { var tag = new Tag { Name = request.Name }; await _dbContext.Tags.AddAsync(tag); await _dbContext.SaveChangesAsync(); return(Ok(tag)); }
/// <summary> /// Replaces the tag or if it doesn't exist, creates it. /// </summary> /// <param name="request">The request.</param> /// <returns> /// <c>PutTagResponse</c> with a tag id. /// </returns> public PutTagResponse Put(PutTagRequest request) { var tagName = request.Data.Name.Trim(); var tagsByIdFuture = repository.AsQueryable<Module.Root.Models.Tag>().Where(tag1 => tag1.Id == request.Id).ToFuture(); var tagsByIdNameFuture = repository.AsQueryable<Module.Root.Models.Tag>().Where(tag1 => tag1.Name == tagName).ToFuture(); var tagById = tagsByIdFuture.FirstOrDefault(); var tagByName = tagsByIdNameFuture.FirstOrDefault(); // Validate. if (tagById != null && tagByName != null && tagById.Id != tagByName.Id) { var logMessage = string.Format("Failed to rename tag. Tag with the same name already exists. Name: '{0}'.", tagName); throw new CmsApiValidationException(logMessage); } if (tagById == null && tagByName != null) { var logMessage = string.Format("Failed to create a tag. Tag with the same name already exists. Name: '{0}'.", tagName); throw new CmsApiValidationException(logMessage); } // Create or update. var createTag = tagById == null; if (createTag) { tagById = new Module.Root.Models.Tag { Id = request.Id.GetValueOrDefault() }; } else if (request.Data.Version > 0) { tagById.Version = request.Data.Version; } tagById.Name = tagName; repository.Save(tagById); unitOfWork.Commit(); // Fire events. if (createTag) { Events.RootEvents.Instance.OnTagCreated(tagById); } else { Events.RootEvents.Instance.OnTagUpdated(tagById); } return new PutTagResponse { Data = tagById.Id, }; }