public async Task <IHttpActionResult> Post(CategoryViewModel model) { try { var result = await _categoryRespository.GetCatgoryByMonikerAsync(model.Moniker); if (result != null) { ModelState.AddModelError("Category", $"Category Moniker '{model.Moniker}' value already in use"); } if (model.Items != null) { foreach (InventoryItemViewModel item in model.Items) { if (await _categoryRespository.CheckItemMonikerExist(item.Moniker)) { ModelState.AddModelError("Item", $"Item moniker '{item.Moniker}' already in use."); } } } if (ModelState.IsValid) { var category = _mapper.Map <Category>(model); _categoryRespository.AddCategory(category); if (await _categoryRespository.SaveContextChangesAsync()) { var newCategory = _mapper.Map <CategoryViewModel>(category); return(CreatedAtRoute("GetCategory", new { moniker = newCategory.Moniker, IncludeItems = category.Items.Count > 0 ? true : false }, newCategory)); } } } catch (Exception ex) { return(InternalServerError(ex)); } return(BadRequest(ModelState)); }
public void AddCategory(Category category) { categoryRespository.AddCategory(category); }