public async Task <IActionResult> UpdateProduct(Guid productId, ProductModification productModification) { if (productId != productModification.Id) { return(BadRequest()); } var product = await _productRepository.GetProduct(productId); if (product == null) { return(NotFound()); } product.Title = productModification.Title; product.Description = productModification.Description; product.Price = productModification.Price; product.StockLevel = productModification.StockLevel; product.ThumbnailUrl = productModification.ThumbnailUrl; var category = (await _productRepository.GetProductCategories()).SingleOrDefault(pc => pc.Title == productModification.Category); if (category == null) { return(NotFound("Category not found")); } product.ProductCategory = category; _productRepository.UpdateProduct(product); await _unitOfWork.SaveChanges(); return(NoContent()); }
[HttpPut("{id}")] // 整体更新 public IActionResult Put(int id, [FromBody] ProductModification productModificationDto) { if (productModificationDto == null) { return(BadRequest()); } if (productModificationDto.Name == "你妹") { // 后期用FluentValidation实现验证逻辑 ModelState.AddModelError("Name", "你妹咯!"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var product = _productRepository.GetProduct(id); if (product == null) { return(NotFound(666)); } // 把第一个对象相应的值赋给第二个对象,此时product的state变成了modified Mapper.Map(productModificationDto, product); if (!_productRepository.Save()) { _logger.LogCritical($"【修改产品{product.Name}的时候出错】"); return(StatusCode(500, "保存产品的时候出错")); } return(NoContent()); }
public async Task <IActionResult> CreateProduct(ProductModification productModification) { var product = new Product(); product.Title = productModification.Title; product.Description = productModification.Description; product.Price = productModification.Price; product.StockLevel = productModification.StockLevel; product.ThumbnailUrl = productModification.ThumbnailUrl; var category = (await _productRepository.GetProductCategories()).SingleOrDefault(pc => pc.Title == productModification.Category); if (category == null) { return(NotFound()); } product.ProductCategory = category; _productRepository.CreateProduct(product); await _unitOfWork.SaveChanges(); return(CreatedAtAction(nameof(GetProduct), new { productId = product.Id }, product)); }
public async Task <Product> Put(int id, [FromForm] Product product) { var prm = new ProductModification(_context); var result = await prm.EditProduct(id, product); return(result); }
public IActionResult Put(int id, [FromBody] ProductModification product) { if (product == null) { return(BadRequest()); } if (product.Name == "产品") { ModelState.AddModelError("Name", "产品的名称不可以是'产品'二字"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var model = ProductService.Current.Products.SingleOrDefault(x => x.Id == id); if (model == null) { return(NotFound()); } model.Name = product.Name; model.Price = product.Price; model.Description = product.Description; return(NoContent()); }
public async Task <Product> Post([FromForm] Product product) { var prm = new ProductModification(_context); var result = await prm.CreateProduct(product); return(result); }
public IActionResult Put(int id, [FromBody] ProductModification productModificationDto) { if (productModificationDto == null) { return(BadRequest()); } if (productModificationDto.Name == "产品") { ModelState.AddModelError("Name", "产品的名称不可以是'产品'二字"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var product = _productRepository.GetProduct(id); if (product == null) { return(NotFound()); } Mapper.Map(productModificationDto, product);//这里我们使用了Mapper.Map的另一个overload的方法,它有两个参数。这个方法会把第一个对象相应的值赋给第二个对象上。这时候product的state就变成了modified了。 if (!_productRepository.Save()) { return(StatusCode(500, "保存产品的时候出错")); } return(NoContent()); }
public IActionResult Put(int id, [FromBody] ProductModification product) { if (product == null) { return(BadRequest()); } if (product.Name == "产品") { ModelState.AddModelError("Name", "产品的名称不可以是'产品'二字"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var model = _productRepository.GetProduct(id, false); if (model == null) { return(NotFound()); } Mapper.Map(product, model); if (!_productRepository.Save()) { return(StatusCode(500, "保存产品时候出错")); } return(NoContent()); }
public void Delete(int id) { var pr = new ProductRepository(_context); var product = pr.GetProductById(id); var prm = new ProductModification(_context); prm.DeleteProduct(product); }
public async Task UpdateProduct(ProductModification product) { var client = _httpClientFactory.CreateClient("sales service"); var httpContent = new StringContent(JsonSerializer.Serialize <ProductModification>(product), Encoding.UTF8, MediaTypeNames.Application.Json); var response = await client.PutAsync($"/products/{product.Id.ToString()}", httpContent); response.EnsureSuccessStatusCode(); }
public async Task <IActionResult> CreateProduct(ProductModification productModification) { Product productCreated; try { productCreated = await _salesService.CreateProduct(productModification); } catch (HttpRequestException) { return(BadRequest()); } return(CreatedAtAction(nameof(GetProduct), new { productId = productCreated.Id }, productCreated)); }
public async Task <Product> CreateProduct(ProductModification product) { var client = _httpClientFactory.CreateClient("sales service"); var httpContent = new StringContent(JsonSerializer.Serialize <ProductModification>(product), Encoding.UTF8, MediaTypeNames.Application.Json); var response = await client.PostAsync($"/products", httpContent); response.EnsureSuccessStatusCode(); var content = await response.Content.ReadAsStringAsync(); return(JsonSerializer .Deserialize <Product>(content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true })); }
public async Task <IActionResult> UpdateProduct(Guid productId, ProductModification productModification) { if (productId != productModification.Id) { return(BadRequest()); } try { await _salesService.UpdateProduct(productModification); } catch (HttpRequestException) { return(BadRequest()); } return(NoContent()); }
public IActionResult Patch(int id, [FromBody] JsonPatchDocument <ProductModification> patchDoc) { if (patchDoc == null) { _logger.LogInformation($"patchDoc参数为空"); return(BadRequest()); } var model = _productService.Get(id); if (model == null) { _logger.LogInformation($"Id为{id}的产品不存在"); return(NotFound()); } var toPatch = new ProductModification { Name = model.Name, Price = model.Price }; patchDoc.ApplyTo(toPatch, ModelState); // validation if (!ModelState.IsValid) { return(BadRequest(ModelState)); } TryValidateModel(toPatch); // validation if (!ModelState.IsValid) { return(BadRequest(ModelState)); } model.Name = toPatch.Name; model.Price = toPatch.Price; return(NoContent()); }
public IActionResult Patch(int id, [FromBody] JsonPatchDocument <ProductModification> patchDoc) { if (patchDoc == null) { return(BadRequest()); } var model = ProductService.Current.Products.SingleOrDefault(x => x.Id == id); if (model == null) { return(NotFound()); } var toPatch = new ProductModification { Name = model.Name, Description = model.Description, Price = model.Price }; patchDoc.ApplyTo(toPatch, ModelState); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (toPatch.Name == "产品") { ModelState.AddModelError("Name", "产品的名称不可以是'产品'二字"); } TryValidateModel(toPatch); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } model.Name = toPatch.Name; model.Description = toPatch.Description; model.Price = toPatch.Price; return(NoContent()); }
public IActionResult Patch(int id, [FromBody] JsonPatchDocument <ProductModification> patchDoc) { if (patchDoc == null) { return(BadRequest()); } var model = ProductService.Current.Products.First(x => x.Id == id); if (model == null) { return(NotFound()); } //这里是通过查询出来的model var toPatch = new ProductModification { Name = model.Name, Price = model.Price, Description = model.Description }; patchDoc.ApplyTo(toPatch, ModelState); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //该函数对model进行手动验证,结果反映在ModelState TryValidateModel(toPatch); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } model.Name = toPatch.Name; model.Price = toPatch.Price; model.Description = toPatch.Description; return(NoContent()); }
public IActionResult Put(int id, [FromBody] ProductModification product) { if (product == null) { _logger.LogInformation($"product参数为空"); return(BadRequest()); } if (product.Name.Equals("产品")) { ModelState.AddModelError("Name", "产品的名称不能是\"产品\"二字"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var p = _productService.Get(id); if (p == null) { _logger.LogInformation($"Id为{id}的产品不存在"); return(NotFound()); } p.Name = product.Name; p.Price = product.Price; // validation if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _productService.Update(p); return(NoContent()); }
public IActionResult Put(int id, [FromBody] ProductModification product) { if (product == null) { return(BadRequest()); } if (product.Name == "产品") { ModelState.AddModelError("Name", "产品的名称不可以是'产品'二字"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //var model = ProductService.Current.Products.SingleOrDefault(x => x.Id == id); var model = _productRepository.GetProduct(id, false); if (model == null) { return(NotFound()); } //model.Name = product.Name; //model.Price = product.Price; //model.Description = product.Description; Mapper.Map(product, model); if (!_productRepository.Save()) { return(StatusCode(500, "保存产品的时候出错")); } //return Ok(model); return(NoContent()); }
public IActionResult UpAllProduct(int id, [FromBody] ProductModification product) { if (product != null) { if (ModelState.IsValid) { var result = ProductService.Current.products.SingleOrDefault(x => x.Id == id); if (result != null) { //change in model of data result.Name = product.Name; result.Price = product.Price; result.Description = product.Description; return(NoContent()); } return(BadRequest()); } return(BadRequest(ModelState)); } return(NotFound()); }
public IActionResult UpPatchProduct(int id, [FromBody] JsonPatchDocument <ProductModification> patchDoc) { if (patchDoc != null) { var result = ProductService.Current.products.SingleOrDefault(x => x.Id == id); if (result != null) { var toPatch = new ProductModification { Name = result.Name, Price = result.Price, Description = result.Description }; patchDoc.ApplyTo(toPatch, ModelState); TryValidateModel(toPatch); if (ModelState.IsValid) { result.Name = toPatch.Name; result.Price = toPatch.Price; result.Description = toPatch.Description; return(NoContent()); } else { return(BadRequest(ModelState)); } } else { return(BadRequest()); } } return(NotFound()); }
public IActionResult Put(int id, [FromBody] ProductModification productModificationDto) { if (productModificationDto == null) { return(BadRequest()); } //Data Annotation if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //复杂验证,自定义 if (productModificationDto.Name == "name") { ModelState.AddModelError("Name", "产品名称不能是‘name’"); return(BadRequest(ModelState)); } var product = _productRepository.GetProduct(id); if (product == null) { return(NotFound()); } //把第一个对象相应的值赋给第二个对象上。这时候product的state就自动变成了modified了,所以直接sava即可。 Mapper.Map(productModificationDto, product); if (!_productRepository.Save()) { return(StatusCode(500, "保存产品的时候出错")); } //PUT建议返回NoContent() return(NoContent()); }
public void Update(ProductModification item) { throw new System.NotImplementedException(); }