public async Task <IHttpActionResult> HieuChinh(HangHoaInput input)
        {
            try
            {
                HangHoa entity = await db.HangHoas.FindAsync(input.ID);

                if (entity == null)
                {
                    return(BadRequest($"Hàng hóa ID ={input.ID} không tồn tại"));
                }
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                int d = await db.HangHoas.CountAsync(p => p.ID != input.ID && (p.MaHang.StartsWith(input.MaHang) || input.MaHang.StartsWith(p.MaHang)));

                if (d > 0)
                {
                    return(BadRequest($"Mã hàng hóa ='{input.MaHang}' đã có hoặc lồng nhau."));
                }

                ConvertHangHoaDTOToEntity(input, entity, false);
                await db.SaveChangesAsync();

                return(Ok());
            }
            catch (Exception ex)
            {
                return(BadRequest($"Hiệu chỉnh không thành công {ex.Message}"));
            }
        }
示例#2
0
 private void ConvertDTOToEntity(HangHoaInput input, HangHoa entity, bool them = true)
 {
     entity.MaSo           = input.MaSo;
     entity.Ten            = input.Ten;
     entity.ThongSoKyThuat = input.ThongSoKyThuat;
     entity.GiaBan         = input.GiaBan;
     if (them)
     {
         entity.NgayTao     = DateTime.Today;
         entity.NgayCapNhat = DateTime.Today;
     }
     entity.MoTa = input.MoTa;
 }
        public ActionResult AddToCart(int HangHoaID, int SoLuong = 1)
        {
            string url     = $"hang-hoa/doc-theo-id?id={HangHoaID}";
            var    gioHang = Session["GioHang"] as GioHangModel;

            if (gioHang == null)
            {
                gioHang            = new GioHangModel();
                Session["GioHang"] = gioHang;
            }
            HangHoaInput hangHoa = ApiHelper <HangHoaInput> .RunPost(url);

            var item = new GioHangItem(hangHoa, SoLuong);

            gioHang.Them(item);
            return(RedirectToAction("Index"));
        }
 private void ConvertHangHoaDTOToEntity(HangHoaInput input, HangHoa entity, bool ThemHangHoa = true)
 {
     entity.MaHang       = input.MaHang;
     entity.TenHang      = input.TenHang;
     entity.DVT          = input.DVT;
     entity.QuyCach      = input.QuyCach;
     entity.MoTa         = input.MoTa;
     entity.GiaBan       = input.GiaBan;
     entity.GiaThiTruong = input.GiaThiTruong;
     entity.LoaiID       = input.LoaiID;
     entity.XuatXu       = input.XuatXu;
     entity.TinhTrang    = input.TinhTrang;
     if (ThemHangHoa == true)
     {
         entity.NgayTao = DateTime.Now;
     }
     entity.NgaySua = DateTime.Now;
 }
示例#5
0
        public async Task <IHttpActionResult> HieuChinh(HangHoaInput input) // <-- [FromBody mac dinh]
        {
            try
            {
                // Tham chieu den thuc the thoa theo ID
                HangHoa entity = await db.HangHoas.FindAsync(input.ID);

                // Kiem tra du lieu
                if (entity == null)
                {
                    return(BadRequest($"Mat hang = {input.ID} khong ton tai"));
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                int d1 = await db.ChungLoais.CountAsync(p => p.ID != input.ID && p.MaSo == input.MaSo);

                if (d1 > 0)
                {
                    return(BadRequest($"Ma so = '{input.MaSo}' da ton tai !"));
                }
                ConvertDTOToEntity(input, entity, false);

                await db.SaveChangesAsync();

                input.ID = entity.ID;
                return(Ok("Hieu chinh thanh cong"));
                // Hoac c2 :
                //return StatusCode(HttpStatusCode.NoContent);
                //return
            }
            catch (Exception ex)
            {
                return(BadRequest($"Hieu chinh khong thanh cong. Ly do : {ex.Message}"));
            }
        }
示例#6
0
        public async Task <ActionResult> ChiTiet(int?id)
        {
            if (id == null || id < 1)
            {
                return(RedirectToAction("Index", "Home"));
            }
            try
            {
                string       url     = $"hang-hoa/doc-theo-id?id={id}";
                HangHoaInput hangHoa = await ApiHelper <HangHoaInput> .RunPostAsync(url);

                if (hangHoa == null)
                {
                    throw new Exception($"Mặt hàng ID = {id} không tồn tại");
                }
                return(View(model: hangHoa));
            }
            catch (Exception ex)
            {
                string cauBaoLoi = $"Lỗi truy cập dữ liệu.<br/>Lý do: {ex.Message}";
                return(View(viewName: "BaoLoi", model: cauBaoLoi));
            }
        }
示例#7
0
        public async Task <IHttpActionResult> Them(HangHoaInput input) // <-- [FromBody]
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                //int d1 = await db.HangHoas.CountAsync(p => p.MaSo == input.MaSo);
                int d1 = await db.HangHoas.CountAsync(p => p.MaSo.StartsWith(input.MaSo));

                if (d1 > 0)
                {
                    return(BadRequest($"Ma so = '{input.MaSo}' da ton tai"));
                }
                bool ktFK = await db.ChungLoais.AnyAsync(p => p.ID == input.ChungLoaiID);

                if (!ktFK)
                {
                    return(BadRequest($"Chung loai ID  = '{input.ChungLoaiID}' khong to tai"));
                }
                // Khoi tao mot hang hoa moi  (entity type = kieu du lieu giao tiep voi nguon du lieu)
                var entity = new HangHoa();
                ConvertDTOToEntity(input, entity, true);
                // Them vao dataset va luu bao DB
                db.HangHoas.Add(entity);
                await db.SaveChangesAsync();

                input.ID = entity.ID;

                return(Ok(input));
            }
            catch (Exception ex)
            {
                return(BadRequest($"Them khong thanh cong. Ly do : {ex.Message}"));
            }
        }
        public async Task <IHttpActionResult> ThemHangHoa(HangHoaInput input)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                //Tránh Lồng mã
                int d1 = await db.HangHoas.CountAsync(p => p.MaHang.StartsWith(input.MaHang) || input.MaHang.StartsWith(p.MaHang));

                if (d1 > 0)
                {
                    return(BadRequest($"Mã số ='{input.MaHang}' bị trùng hoặc lồng nhau."));
                }
                bool ktFK = await db.Loais.AnyAsync(p => p.ID == input.LoaiID);

                if (!ktFK)
                {
                    return(BadRequest($"Loại ID='{input.LoaiID}' không tồn tại."));
                }
                //Khỏi tạo 1 hanghoa mới (entity type - kiểu dữ liệu giao tiếp với nguồn dữ liệu)
                var entity = new HangHoa();
                //Gán giá trị
                ConvertHangHoaDTOToEntity(input, entity, true);
                //Thêm vao DbSet và lưu vào database
                db.HangHoas.Add(entity);
                await db.SaveChangesAsync();

                return(Ok());
            }
            catch (Exception ex)
            {
                return(BadRequest($"Thêm Không thành công. {ex.Message}"));
            }
        }
 public Task <HangHoaInput> Create(HangHoaInput input)
 {
     throw new NotImplementedException();
 }