// PUT api/ProductApi/5 public async Task<IHttpActionResult> PutProduct(int id, Product product) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } if (id != product.ProductId) { return this.BadRequest(); } this.db.Entry(product).State = EntityState.Modified; try { await this.db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!this.ProductExists(id)) { return this.NotFound(); } else { throw; } } return this.StatusCode(HttpStatusCode.NoContent); }
// PUT odata/ProductApi(5) public async Task<IHttpActionResult> Put([FromODataUri] int key, Product product) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } if (key != product.ProductId) { return this.BadRequest(); } this.db.Entry(product).State = EntityState.Modified; try { await this.db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!this.ProductExists(key)) { return this.NotFound(); } else { throw; } } return this.Updated(product); }
// POST odata/ProductApi public async Task<IHttpActionResult> Post(Product product) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } this.db.Products.Add(product); await this.db.SaveChangesAsync(); return this.Created(product); }
public async Task<IHttpActionResult> PostProduct(Product product) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } this.db.Products.Add(product); await this.db.SaveChangesAsync(); return this.CreatedAtRoute("DefaultApi", new { id = product.ProductId }, product); }
// POST odata/Product public async Task<IHttpActionResult> Post(Product product) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.Products.Add(product); await db.SaveChangesAsync(); return Created(product); }
public ActionResult Create(FormCollection fc, Product shopproduct) { if (this.ModelState.IsValid) { shopproduct.Categories = new Collection<Category>(); foreach (string field in fc) { if (field.StartsWith("category")) { var catId = int.Parse(fc[field]); var cat = this.db.Categories.Find(catId); shopproduct.Categories.Add(cat); } } this.db.Products.Add(shopproduct); this.db.SaveChanges(); return this.RedirectToAction("Index"); } this.ViewBag.categoriesSelectTemplate = new SelectList(this.db.Categories, "CategoryId", "Name"); return this.View(shopproduct); }
public void TestImportProductSimple() { var newProduct = new Product { ProductId = 0, Name = "Test Product " + Guid.NewGuid(), Description = "Test Description", Price = 1, IsFeatured = false, IsPublished = false }; var svc = new ProductImportService(); var newProductId = svc.ImportProductSimple(newProduct); var testDbContext = new GenericDBContext(); Assert.AreEqual(1, testDbContext.Products.Count(p => p.ProductId == newProductId)); testDbContext.Dispose(); }
public int ImportProductSimple(Product data) { this.db.Products.Add(data); this.db.SaveChanges(); return data.ProductId; }