public async Task<IHttpActionResult> PutCategory(int id, Category category) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != category.Id) { return BadRequest(); } db.Entry(category).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CategoryExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public async Task<IHttpActionResult> PostCategory(Category category) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.Categories.Add(category); await db.SaveChangesAsync(); var categoryDTO = new CategoryDTO { Id = category.Id, CategoryName = category.CategoryName, ParentCategoryId = category.ParentCategoryId, ParentCategoryName = (from cat in db.Categories where cat.Id == category.ParentCategoryId select cat.CategoryName).FirstOrDefault(), }; return CreatedAtRoute("DefaultApi", new { id = category.Id }, categoryDTO); }