public HttpResponseMessage PutSanPham(string id, [FromBody] SanPham SanPham) { try { using (MyPhamEntities db = new MyPhamEntities()) { var s = db.SanPhams.SingleOrDefault(x => x.ID_SP == id); if (s != null) { s.Ten_SP = SanPham.Ten_SP; s.ID_Loai = SanPham.ID_Loai; s.HinhAnh = SanPham.HinhAnh; s.Mota = SanPham.Mota; s.So_Luong = SanPham.So_Luong; s.NoiDung = SanPham.NoiDung; s.ThuongHieu = SanPham.ThuongHieu; db.SaveChanges(); } return(Request.CreateResponse(HttpStatusCode.OK, s)); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message)); } }
// Hiện thị all loại sản phẩm public HttpResponseMessage GetLoaiSPs() { using (MyPhamEntities db = new MyPhamEntities()) { var result = db.LoaiSPs; return(Request.CreateResponse(HttpStatusCode.OK, result.ToList())); } }
// Get theo mã loại public LoaiSP GetLoaiSP(string id) { using (MyPhamEntities db = new MyPhamEntities()) { LoaiSP s = db.LoaiSPs.SingleOrDefault(x => x.ID_Loai == id); if (s != null) { return(s); } else { return(null); } } }
// GET: api/SanPham/5 public SanPham GetSanPham(string id) { using (MyPhamEntities db = new MyPhamEntities()) { SanPham s = db.SanPhams.SingleOrDefault(x => x.ID_SP == id); if (s != null) { return(s); } else { return(null); } } }
public HttpResponseMessage PostLoaiSP([FromBody] LoaiSP LoaiSP) { try { using (MyPhamEntities db = new MyPhamEntities()) { db.LoaiSPs.Add(LoaiSP); db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.Created)); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message)); } }
public HttpResponseMessage DeleteLoaiSP(string id) { try { using (MyPhamEntities db = new MyPhamEntities()) { LoaiSP s = db.LoaiSPs.SingleOrDefault(x => x.ID_Loai == id); db.LoaiSPs.Remove(s); db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, s)); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message)); } }
//Hiển thị sản phẩm theo mã loại public IHttpActionResult GetSP_Loai(string id) { using (MyPhamEntities db = new MyPhamEntities()) { List <SanPham> dshh = db.SanPhams.ToList(); List <SanPham> dshhout = new List <SanPham>(); foreach (SanPham hh in dshh) { if (hh.ID_Loai.Equals(id)) { dshhout.Add(hh); } } if (dshhout == null) { return(NotFound()); } return(Ok(dshhout)); } }
//Get các SanPham sắp hết ( SoLuongCon <10) public IHttpActionResult GetSP_SapHet() { using (MyPhamEntities db = new MyPhamEntities()) { List <SanPham> dshh = db.SanPhams.ToList(); List <SanPham> dshhout = new List <SanPham>(); foreach (SanPham hh in dshh) { if (hh.So_Luong < 10) { dshhout.Add(hh); } } if (dshhout == null) { return(NotFound()); } return(Ok(dshhout)); } }
//Get các LoaiSP theo TenLoai public IHttpActionResult GetLoaiSP_TenLoai(string tenloai) { using (MyPhamEntities db = new MyPhamEntities()) { List <LoaiSP> dshh = db.LoaiSPs.ToList(); List <LoaiSP> dshhout = new List <LoaiSP>(); LoaiSP lh = db.LoaiSPs.FirstOrDefault(e => e.Ten_Loai == tenloai); foreach (LoaiSP hh in dshh) { if (hh.ID_Loai.Equals(lh.ID_Loai)) { dshhout.Add(hh); } } if (dshhout == null) { return(NotFound()); } return(Ok(dshhout)); } }
public HttpResponseMessage PutLoaiSP(string id, [FromBody] LoaiSP LoaiSP) { try { using (MyPhamEntities db = new MyPhamEntities()) { var s = db.LoaiSPs.SingleOrDefault(x => x.ID_Loai == id); if (s != null) { s.Ten_Loai = LoaiSP.Ten_Loai; s.ID_Cha = LoaiSP.ID_Cha; s.MoTa = LoaiSP.MoTa; db.SaveChanges(); } return(Request.CreateResponse(HttpStatusCode.OK, s)); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message)); } }