public ActionResult <GetFullCategoryResponse> Poststring(PutCategoryRequest categoryRequest) { var entity = mapper.Map <CategoryEntity>(categoryRequest); context.Categories.Add(entity); context.SaveChanges(); var category = mapper.Map <GetFullCategoryResponse>(entity); return(CreatedAtAction(nameof(GetCategoryById), new { id = entity.Id }, category)); }
public async Task <ActionResult> PutCategory([FromBody] PutCategoryRequest request) { var newCategory = new Category { Name = request.Name, Abbreviation = request.Abbreviation }; await _documentlibContext.AddAsync(newCategory); await _documentlibContext.SaveChangesAsync(); return(Ok(newCategory)); }
/// <summary> /// Puts the specified request. /// </summary> /// <param name="request">The request.</param> /// <returns><c>PutCategoryResponse</c> with created or updated item id.</returns> public PutCategoryResponse Put(PutCategoryRequest request) { var categories = repository.AsQueryable <Module.Root.Models.Category>() .Where(l => l.Id == request.Id || l.Name == request.Data.Name) .ToList(); var categoryToSave = categories.FirstOrDefault(l => l.Id == request.Id); var createCategory = categoryToSave == null; if (createCategory) { categoryToSave = new Module.Root.Models.Category { Id = request.Id.GetValueOrDefault() }; } else if (request.Data.Version > 0) { categoryToSave.Version = request.Data.Version; } if (categories.Any(l => l.Id != request.Id && l.Name == request.Data.Name)) { var message = string.Format("Category with name '{0}' already exists.", request.Data.Name); throw new CmsApiValidationException(message); } unitOfWork.BeginTransaction(); categoryToSave.Name = request.Data.Name; repository.Save(categoryToSave); unitOfWork.Commit(); // Fire events. if (createCategory) { Events.RootEvents.Instance.OnCategoryCreated(categoryToSave); } else { Events.RootEvents.Instance.OnCategoryUpdated(categoryToSave); } return(new PutCategoryResponse { Data = categoryToSave.Id }); }
/// <summary> /// Puts the specified request. /// </summary> /// <param name="request">The request.</param> /// <returns><c>PutCategoryResponse</c> with created or updated item id.</returns> public PutCategoryResponse Put(PutCategoryRequest request) { var categories = repository.AsQueryable<Module.Root.Models.Category>() .Where(l => l.Id == request.Id || l.Name == request.Data.Name) .ToList(); var categoryToSave = categories.FirstOrDefault(l => l.Id == request.Id); var createCategory = categoryToSave == null; if (createCategory) { categoryToSave = new Module.Root.Models.Category { Id = request.Id.GetValueOrDefault() }; } else if (request.Data.Version > 0) { categoryToSave.Version = request.Data.Version; } if (categories.Any(l => l.Id != request.Id && l.Name == request.Data.Name)) { var message = string.Format("Category with name '{0}' already exists.", request.Data.Name); throw new CmsApiValidationException(message); } unitOfWork.BeginTransaction(); categoryToSave.Name = request.Data.Name; repository.Save(categoryToSave); unitOfWork.Commit(); // Fire events. if (createCategory) { Events.RootEvents.Instance.OnCategoryCreated(categoryToSave); } else { Events.RootEvents.Instance.OnCategoryUpdated(categoryToSave); } return new PutCategoryResponse { Data = categoryToSave.Id }; }