public async Task <IActionResult> Add([FromBody] SanPhamDto sanPhamDto) { int idSeller = User.GetIdSeller(); if (await sanPhamService.Add(idSeller, mapper.Map <SanPham>(sanPhamDto))) { return(Ok()); } return(BadRequest()); }
public IActionResult Them(SanPhamViewModel model, IFormFile AnhDaiDien, IFormCollection fc, IFormFile productImages1, IFormFile productImages2, IFormFile productImages3 ) { if (ModelState.IsValid) { model.AnhDaiDien = FileHelper.UploadedFile(AnhDaiDien, "ProductAvatar"); model.SoLuotXemSp = 0; model.Ishot = Convert.ToBoolean(fc["isHot"].ToString().Split(',')[0]); model.Isnew = Convert.ToBoolean(fc["isNew"].ToString().Split(',')[0]); _spService.Add(model); AnhSanPhamViewModel pic = new AnhSanPhamViewModel() { MaSp = model.MaSp, Anh1 = productImages1 != null?FileHelper.UploadedFile(productImages1, "ProductImages") : null, Anh2 = productImages2 != null?FileHelper.UploadedFile(productImages2, "ProductImages") : null, Anh3 = productImages3 != null?FileHelper.UploadedFile(productImages3, "ProductImages") : null, }; _aspService.Add(pic); ThongSoKiThuatViewModel tskt; List <ThongSoViewModel> listTS = _tsService.GetThongSo(model.LoaiSp).ToList(); for (int i = 0; i < listTS.Count(); i++) { tskt = new ThongSoKiThuatViewModel() { MaSp = model.MaSp, ThongSo = listTS[i].MaThongSo, GiaTri = fc[listTS[i].MaThongSo], }; //context.ThongSoKiThuat.AddAsync(tskt); //context.SaveChanges(); _tsktService.AddTSKT(tskt); tskt = null; } _spService.SaveChange(); return(RedirectToAction("QuanLy", "Admin", new { id = model.LoaiSp }).WithSuccess("Thành công", "Sản phẩm đã được thêm. ID:" + model.MaSp)); } else { ModelState.AddModelError("", "aaa"); ViewData["MaLoai"] = model.LoaiSp; return(View(model)); } }
public HttpResponseMessage Create(HttpRequestMessage request, SAN_PHAM sanPhamModel) { return(CreateHttpResponse(request, () => { HttpResponseMessage response = null; if (ModelState.IsValid) { request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } else { var thuongHieu = _sanPhamService.Add(sanPhamModel); _sanPhamService.SaveChange(); response = request.CreateResponse(HttpStatusCode.Created, thuongHieu); } return response; })); }
public HttpResponseMessage Create(HttpRequestMessage request, SanPham sanPham) { SanPham modelSanPham = null; try { sanPham.NgayNhap = DateTime.Now; modelSanPham = _sanPhamService.Add(sanPham); if (modelSanPham != null) { Message = "Tạo mới sản phẩm thành công"; _sanPhamService.SaveChange(); } else { throw new DbEntityValidationException("tạo sản phẩm không thành công"); } } catch (DbEntityValidationException ex) { LogException(ex); } return(GetResponseMessage(IsSuccess, Message, 1, modelSanPham)); }