public async Task <IActionResult> PostSpendingItem([FromBody] SpendingItem spendingItem) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //Do sanity check before posting if (SpendingItemHelper.ValidCategory(spendingItem.Category) && spendingItem.Heading != null && spendingItem.Cost != 0 && spendingItem.Currency != null) { _context.SpendingItem.Add(spendingItem); await _context.SaveChangesAsync(); return(CreatedAtAction("GetSpendingItem", new { id = spendingItem.ID }, spendingItem)); } else { return(UnprocessableEntity(ModelState)); } }
public async Task <IActionResult> PutSpendingItem([FromRoute] int id, [FromBody] SpendingItem spendingItem) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != spendingItem.ID) { return(BadRequest()); } if (!SpendingItemHelper.ValidCategory(spendingItem.Category) || spendingItem.Heading == null || spendingItem.Cost == 0 || spendingItem.Currency == null) { return(UnprocessableEntity()); } _context.Entry(spendingItem).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SpendingItemExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }