public async Task <IActionResult> PostCategory([FromBody] CategoryRequest item) { var category = item.ToDataModel(); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _context.Categories.Add(category); try { await _context.SaveChangesAsync(); } catch (DbUpdateException ex) { if (CategoryExists(category.Id)) { return(new StatusCodeResult(StatusCodes.Status409Conflict)); } else { throw; } } return(CreatedAtAction("GetCategory", new { id = category.Id }, category.ToApiModel())); }
public async Task <IActionResult> PutCategory([FromRoute] int id, [FromBody] CategoryRequest item) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != item.Id) { return(BadRequest()); } var category = await _context.Categories.FirstOrDefaultAsync(m => m.Id == id); if (category == null) { return(NotFound()); } var edited = item.ToDataModel(); try { _context.Entry(edited).State = EntityState.Modified; await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CategoryExists(id)) { return(NotFound()); } else { throw; } } return(Ok(category.ToApiModel())); }