/// <summary> /// 根据id更新产品 /// </summary> /// <param name="id">产品id</param> /// <param name="product"></param> public HttpResponseMessage PutProduct(int id, ProductModel product) { product.Id = id; _productService.Update(product.ToEntity()); return new HttpResponseMessage(HttpStatusCode.OK); }
private async void PostProduct(object sender, RoutedEventArgs e) { BtnPostProduct.IsEnabled = false; try { var product = new ProductModel() { Category = this.TextCategory.Text, Name = this.TextName.Text, Price = decimal.Parse(this.TextPrice.Text) }; var response = await client.PostAsJsonAsync("api/Products", product); response.EnsureSuccessStatusCode(); _products.Add(product); } catch (HttpRequestException ex) { MessageBox.Show(ex.Message); } catch (FormatException ex) { MessageBox.Show("Price must be a number"); } finally { BtnPostProduct.IsEnabled = true; } }
/// <summary> /// 新增 /// </summary> /// <param name="model"></param> /// <returns></returns> public HttpResponseMessage PostProduct(ProductModel model) { if (model == null || !ModelState.IsValid) { throw new HttpResponseException(HttpStatusCode.BadRequest); } var product = _productService.Add(model.ToEntity()).ToModel(); var result = Request.CreateResponse(HttpStatusCode.Created, product); string uri = Url.Link("DefaultApi", new { id = product.Id }); result.Headers.Location = new Uri(uri); return result; }