public JsonResult LoadListXeNew()
        {
            List <Xe> listXe = new List <Xe>();

            try
            {
                using (DoAnWebEntities daw = new DoAnWebEntities())
                {
                    daw.Configuration.ProxyCreationEnabled = false;
                    listXe = daw.Xes.Where(x => x.IsDeleted == false && x.TinhTrang == "NEW").OrderByDescending(x => x.NgayNhap).ToList();
                    if (listXe.Count > 9)
                    {
                        listXe = listXe.Take(9).ToList();
                    }
                    else
                    {
                        var listXeThem = daw.Xes.Where(x => x.TinhTrang != "NEW" && x.IsDeleted == false).OrderByDescending(x => x.NgayNhap).ToList();
                        listXeThem = listXeThem.Take(9 - listXe.Count).ToList();
                        if (listXeThem.Count > 0)
                        {
                            foreach (var item in listXeThem)
                            {
                                listXe.Add(item);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return(new JsonResult {
                Data = listXe, JsonRequestBehavior = JsonRequestBehavior.AllowGet
            });
        }
        public ActionResult Single(string data)
        {
            string url = Request.Url.AbsoluteUri;

            string[] ArrayUrl = url.Split('/');
            int      count    = ArrayUrl.Length;

            if (data == null)
            {
                data = ArrayUrl[count - 1];
            }
            Xe xe = new Xe();

            try
            {
                using (DoAnWebEntities daw = new DoAnWebEntities())
                {
                    Guid idXe = new Guid(data);
                    xe = daw.Xes.Where(x => x.IsDeleted == false && x.IdXe == idXe).FirstOrDefault();
                    if (xe != null)
                    {
                        UpdateSoLuongXe(idXe);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            //return new JsonResult { Data = xe, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
            ViewBag.Xe = xe;
            return(View());
        }
        public JsonResult ThemXe(Xe xe, string listhinh)
        {
            string message = "";

            if (ModelState.IsValid)
            {
                using (DoAnWebEntities daw = new DoAnWebEntities())
                {
                    try
                    {
                        Xe dt = daw.Xes.Where(x => (x.SoKhung == xe.SoKhung || x.SoMay == xe.SoMay) && x.IsDeleted == false).FirstOrDefault();
                        if (dt != null)
                        {
                            message = "Số khung hoặc số máy này đã có trong dữ liệu";
                        }
                        else
                        {
                            xe.CreatedDate = DateTime.Now;
                            xe.CreatedBy   = Guid.Parse("2DAD25D4-A75F-405C-B405-468D4F8E1D3A");
                            xe.IdXe        = Guid.NewGuid();
                            xe.SoLuotXem   = 0;
                            xe.TinhTrang   = "NEW";
                            xe.IsDeleted   = false;
                            daw.Xes.Add(xe);
                            daw.SaveChanges();
                            message = "Bạn đã thêm xe thành công.";
                            if (message == "Bạn đã thêm xe thành công.")
                            {
                                UpdateSoLuongLoaiXe(xe.MaLoaiXe.Value);
                                //gửi email đến người dùng là có xe mới nhập về
                                GuiEmail(xe.SoKhung);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        message = "Đã xảy ra lỗi khi thêm thêm xe mới.";
                    }
                }
            }
            else
            {
                message = "Đã xảy ra lỗi khi thêm thêm xe mới.";
            }
            return(new JsonResult {
                Data = message, JsonRequestBehavior = JsonRequestBehavior.AllowGet
            });
        }
Exemple #4
0
        public void MoiNhat()
        {
            DoAnWeb.Controllers.HomeController ctrl = new HomeController();
            List <Xe>       listXe = new List <Xe>();
            DoAnWebEntities daw    = new DoAnWebEntities();

            daw.Configuration.ProxyCreationEnabled = false;
            var result = ctrl.LoadListXeNew();
            int count  = 0;

            foreach (var item in result.Data.ToString())
            {
                count++;
            }
            Assert.AreNotEqual(9, count);
        }
 private void UpdateSoLuongXe(Guid IdXe)
 {
     try
     {
         using (DoAnWebEntities daw = new DoAnWebEntities())
         {
             Xe xe = daw.Xes.Where(x => x.IdXe == IdXe && x.IsDeleted == false).FirstOrDefault();
             if (xe != null)
             {
                 xe.SoLuotXem = xe.SoLuotXem + 1;
                 daw.Xes.Attach(xe);
                 daw.Entry(xe).State = EntityState.Modified;
                 daw.SaveChanges();
             }
         }
     }
     catch (Exception ex)
     {
         throw;
     }
 }