public SaveProductRes SaveProduct(SaveProductReq saveProductReq) { var result = new SaveProductRes() { Result = 0, Message = $"Đã xảy ra lỗi, vui lòng thử lại sau." }; try { DynamicParameters parameters = new DynamicParameters(); parameters.Add(@"ID", saveProductReq.ID); parameters.Add(@"ProductName", saveProductReq.ProductName); parameters.Add(@"BrandID", saveProductReq.BrandID); parameters.Add(@"CategoryID", saveProductReq.CategoryID); parameters.Add(@"Price", saveProductReq.Price); var response = SqlMapper.ExecuteScalar <int>(con, "proc_SaveProduct", param: parameters, commandType: CommandType.StoredProcedure); result.Result = response; result.Message = saveProductReq.ID != 0 ? $"Sản phẩm đã được tạo thành công." : $"Sản phẩm đã được cập nhật thành công"; return(result); } catch (Exception ex) { return(result); } }
public async Task <SaveProductRes> Save(SaveProductReq request) { var result = new SaveProductRes() { ProductId = 0, Message = "Something went wrong, please contact administrator." }; try { DynamicParameters parameters = new DynamicParameters(); parameters.Add("@ProductId", request.ProductId); parameters.Add("@ProductName", request.ProductName); parameters.Add("@Price", request.Price); parameters.Add("@AvatarPath", request.AvatarPath); parameters.Add("@CategoryId", request.CategoryId); parameters.Add("@AquariumSizeId", request.AquariumSizeId); parameters.Add("@Status", request.Status); result = await SqlMapper.QueryFirstOrDefaultAsync <SaveProductRes>(cnn : connection, sql : "sp_SaveProduct", param : parameters, commandType : CommandType.StoredProcedure); return(result); } catch (Exception) { return(result); } }
public IActionResult SaveProduct([FromBody] SaveProduct model) { var saveProductResult = new SaveProductRes() { Message = "Lỗi, vui lòng thử lại.", Result = 0 }; try { if (model != null) { if (model.ID == 0) { var url = $"{Common.Common.ApiUrl}/Product/SaveProduct"; HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.ContentType = "application/json"; httpWebRequest.Method = "POST"; using (var streamWrite = new StreamWriter(httpWebRequest.GetRequestStream())) { var json = JsonConvert.SerializeObject(model); streamWrite.Write(json); } var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var resKetQua = streamReader.ReadToEnd(); saveProductResult = JsonConvert.DeserializeObject <SaveProductRes>(resKetQua); } if (saveProductResult.Result > 0) { return(new JsonResult(new { status = 1, message = saveProductResult.Message })); } } else //update student by StudentId { var product = new SaveProduct(); var url = $"{Common.Common.ApiUrl}/Product/GetById/{product.ID}"; HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.Method = "GET"; var response = httpWebRequest.GetResponse(); { string responseData; Stream responseStream = response.GetResponseStream(); try { StreamReader streamReader = new StreamReader(responseStream); try { responseData = streamReader.ReadToEnd(); } finally { ((IDisposable)streamReader).Dispose(); } } finally { ((IDisposable)responseStream).Dispose(); } product = JsonConvert.DeserializeObject <SaveProduct>(responseData); } product = model; url = $"{Common.Common.ApiUrl}/Product/SaveProduct"; httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.ContentType = "application/json"; httpWebRequest.Method = "POST"; using (var streamWrite = new StreamWriter(httpWebRequest.GetRequestStream())) { var json = JsonConvert.SerializeObject(model); streamWrite.Write(json); } var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var resKetQua = streamReader.ReadToEnd(); saveProductResult = JsonConvert.DeserializeObject <SaveProductRes>(resKetQua); } if (saveProductResult.Result > 0) { return(new JsonResult(new { status = 1, message = saveProductResult.Message })); } } } } catch (Exception ex) { } return(new JsonResult(new { status = 0, message = saveProductResult.Message })); }