// POST: api/Products public async Task<ProductModel> Post([FromBody]ProductModel product) { using (var dbModelContainer = new AAYazOkuluDBModelContainer()) { var addedProduct = dbModelContainer.ProductSet.Add(new Product { AddedDate = product.AddedDate, Description = product.Description, Name = product.Name }); await dbModelContainer.SaveChangesAsync(); return new ProductModel { AddedDate = addedProduct.AddedDate, Description = addedProduct.Description, Name = addedProduct.Name }; } }
// PUT: api/Products/5 public async Task<ProductModel> Put(int id, [FromBody]ProductModel product) { using (var dbModelContainer = new AAYazOkuluDBModelContainer()) { var productToBeUpdated = dbModelContainer.ProductSet.Find(id); if (productToBeUpdated != null) { productToBeUpdated.AddedDate = product.AddedDate; productToBeUpdated.Description = product.Description; productToBeUpdated.Name = product.Name; } await dbModelContainer.SaveChangesAsync(); return new ProductModel { AddedDate = productToBeUpdated.AddedDate, Description = productToBeUpdated.Description, Name = productToBeUpdated.Name }; } }
// DELETE: api/Products/5 public async Task Delete(int id) { using (var dbModelContainer = new AAYazOkuluDBModelContainer()) { var productToBeDeleted = await dbModelContainer.ProductSet.SingleOrDefaultAsync(t => t.Id == id); if (productToBeDeleted != null) { dbModelContainer.ProductSet.Remove(productToBeDeleted); await dbModelContainer.SaveChangesAsync(); } } }