Ejemplo n.º 1
0
 public async Task <IHttpActionResult> Post(ItemModel item)
 {
     try
     {
         if (ModelState.IsValid)
         {
             var itemToUpdate = _repo.GetItemsByIdAsync(item.ItemId).Result;
             if (itemToUpdate != null)
             {
                 itemToUpdate.Description    = item.Description;
                 itemToUpdate.ItemCategoryId = item.ItemCategoryId;
                 itemToUpdate.Price          = item.Price;
                 if (_repo.SaveChanges())
                 {
                     return(Ok(itemToUpdate));
                 }
             }
             else
             {
                 var mapped = _mapper.Map <Item>(item);
                 _repo.AddItem(mapped);
                 if (await _repo.SaveChangesAsync())
                 {
                     return(Created("GetItems", mapped));
                 }
             }
         }
     }
     catch (Exception ex)
     {
         return(BadRequest(ex.Message));
     }
     return(BadRequest());
 }