Exemple #1
0
        private List <HinhAnhSanPham> layTop5SanPham(int year, int Month)//TONG SO KHACH HANG THEO THANG
        {
            List <HinhAnhSanPham> DS = new List <HinhAnhSanPham>();
            string connectionString  = Configuration["ConnectionStrings:DefaultConnection"];

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand command = new SqlCommand("LayHATop5SanPham", connection))
                {
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    if (connection.State == ConnectionState.Open)
                    {
                        connection.Close();
                    }
                    connection.Open();
                    command.Parameters.AddWithValue("@thang", Month);
                    command.Parameters.AddWithValue("@nam", year);
                    SqlDataAdapter da = new SqlDataAdapter(command);
                    DataSet        ds = new DataSet();
                    da.Fill(ds);
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        HinhAnhSanPham hinh = new HinhAnhSanPham();
                        hinh.TenSP = ds.Tables[0].Rows[i]["tenSP"].ToString();
                        hinh.Image = ds.Tables[0].Rows[i]["Hinhanh"].ToString();
                        DS.Add(hinh);
                    }
                }
            }
            return(DS);
        }
        public HttpResponseMessage Delete(HttpRequestMessage request, int id)
        {
            HinhAnhSanPham modelHinhAnhSanPham = null;

            try
            {
                var hinhAnhWithIdSanPham = _hinhAnhSanPhamService.GetAllWithIdSanPham(id);
                foreach (var item in hinhAnhWithIdSanPham)
                {
                    modelHinhAnhSanPham = _hinhAnhSanPhamService.Delete(item.IdHinhAnh);
                }
                _hinhAnhSanPhamService.SaveChange();
                if (modelHinhAnhSanPham != null)
                {
                    Message = "Xóa hình ảnh sản phẩm thành công";
                }
                else
                {
                    throw new DbEntityValidationException("Xóa hình ảnh sản phẩm không thành công");
                }
            }
            catch (DbEntityValidationException ex)
            {
                LogException(ex);
            }
            return(GetResponseMessage(IsSuccess, Message, 1, modelHinhAnhSanPham));
        }
        public HttpResponseMessage UploadImage(HttpRequestMessage request)
        {
            HinhAnhSanPham modelHinhAnhSanPham = null;

            try
            {
                var httpRequest = HttpContext.Current.Request;

                //Upload Image
                var postedFile = httpRequest.Files["fileUpload"];
                var id         = httpRequest["IdSanPham"];
                //Create custom filename
                if (postedFile != null)
                {
                    var imageName = new String(Path.GetFileNameWithoutExtension(postedFile.FileName).Take(10).ToArray()).Replace(" ", "-");
                    imageName = imageName + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(postedFile.FileName);
                    var filePath = HttpContext.Current.Server.MapPath("~/Images/" + imageName);
                    postedFile.SaveAs(filePath);

                    //Save to DB
                    var hinhAnh = new HinhAnhSanPham
                    {
                        LinkHinhAnh = imageName,
                        IdSanPham   = int.Parse(id),
                    };
                    _hinhAnhSanPhamService.Add(hinhAnh);
                    _hinhAnhSanPhamService.SaveChange();
                    modelHinhAnhSanPham = _hinhAnhSanPhamService.GetById(hinhAnh.IdHinhAnh);
                }

                if (modelHinhAnhSanPham != null)
                {
                    Message = "Upload ảnh thành công thành công";
                }
                else
                {
                    throw new DbEntityValidationException("Upload ảnh không thành công");
                }
            }
            catch (DbEntityValidationException ex)
            {
                LogException(ex);
            }
            return(GetResponseMessage(IsSuccess, Message, 1, modelHinhAnhSanPham));
        }
        public HttpResponseMessage GetById(HttpRequestMessage request, int id)
        {
            HinhAnhSanPham hinhAnhSanPhamSingler = null;

            try
            {
                hinhAnhSanPhamSingler = _hinhAnhSanPhamService.GetById(id);
                if (hinhAnhSanPhamSingler != null)
                {
                    Message = "Thông tin hình ảnh sản phẩm với id: " + id;
                }
                else
                {
                    throw new DbEntityValidationException(string.Format("Không tìm kiếm được hình ảnh sản phẩm với id {0}", id));
                }
            }
            catch (DbEntityValidationException ex)
            {
                LogException(ex);
            }
            return(GetResponseMessage(IsSuccess, Message, 1, hinhAnhSanPhamSingler));
        }
        public HttpResponseMessage Create(HttpRequestMessage request, HinhAnhSanPham hinhAnhSanPham)
        {
            HinhAnhSanPham modelHinhAnhSanPham = null;

            try
            {
                modelHinhAnhSanPham = _hinhAnhSanPhamService.Add(hinhAnhSanPham);
                if (modelHinhAnhSanPham != null)
                {
                    Message = "Tạo mới hình ảnh sản phẩm thành công";
                    _hinhAnhSanPhamService.SaveChange();
                }
                else
                {
                    throw new DbEntityValidationException("tạo mới hình ảnh sản phẩm không thành công");
                }
            }
            catch (DbEntityValidationException ex)
            {
                LogException(ex);
            }
            return(GetResponseMessage(IsSuccess, Message, 1, modelHinhAnhSanPham));
        }
        public HttpResponseMessage GetOneById(HttpRequestMessage request, int id)
        {
            IEnumerable <HinhAnhSanPham> listHinhAnhSanPham = null;
            List <HinhAnhSanPham>        hinhAnhSanPhamList = null;
            HinhAnhSanPham hinhAnhSanPhamSingler            = null;
            var            toTal = 0;

            try
            {
                listHinhAnhSanPham = _hinhAnhSanPhamService.GetAllWithIdSanPham(id);
                hinhAnhSanPhamList = listHinhAnhSanPham.ToList();

                for (int i = 0; i < hinhAnhSanPhamList.Count; i++)
                {
                    hinhAnhSanPhamSingler = hinhAnhSanPhamList[i];
                    return(GetResponseMessage(IsSuccess, Message, 1, hinhAnhSanPhamSingler));
                }
            }
            catch (DbEntityValidationException ex)
            {
                LogException(ex);
            }
            return(GetResponseMessage(IsSuccess, Message, 1, hinhAnhSanPhamSingler));
        }
        public HttpResponseMessage Update(HttpRequestMessage request, HinhAnhSanPham hinhAnhSanPham)
        {
            HinhAnhSanPham modelHinhAnhSanPham = null;

            try
            {
                _hinhAnhSanPhamService.Update(hinhAnhSanPham);
                _hinhAnhSanPhamService.SaveChange();
                modelHinhAnhSanPham = _hinhAnhSanPhamService.GetById(hinhAnhSanPham.IdHinhAnh);
                if (modelHinhAnhSanPham != null)
                {
                    Message = "Sửa thông tin hình ảnh sản phẩm thành công";
                }
                else
                {
                    throw new DbEntityValidationException("sửa thông tin hình ảnh sản phẩm không thành công");
                }
            }
            catch (DbEntityValidationException ex)
            {
                LogException(ex);
            }
            return(GetResponseMessage(IsSuccess, Message, 1, modelHinhAnhSanPham));
        }
 public void Update(HinhAnhSanPham hinhAnhSanPham)
 {
     _hinhAnhSanPhamRepository.Update(hinhAnhSanPham);
 }
 public HinhAnhSanPham Add(HinhAnhSanPham hinhAnhSanPham)
 {
     return(_hinhAnhSanPhamRepository.Add(hinhAnhSanPham));
 }
        public ActionResult DangTinSanPham(DangTinSanPhamViewModel dtspvm)
        {
            if (ModelState.IsValid)
            {
                if (db.TaiKhoans.Find((User as DangNhapPrincipal).TaiKhoan.MaTK).TongTinConLai <= 0)
                {
                    ViewBag.HetTin = 1;
                    return(View());
                }

                Tin tin = new Tin();
                tin.MaSKU       = dtspvm.MaSKU;
                tin.MaTK        = (User as DangNhapPrincipal).TaiKhoan.MaTK;
                tin.NgayDang    = dtspvm.NgayDang;
                tin.NgayKetThuc = dtspvm.NgayKetThuc;
                tin.TenSP       = dtspvm.TenSP;
                tin.Gia         = dtspvm.Gia;

                db.Tins.Add(tin);
                db.SaveChanges();

                //cập nhật số lượng tin
                TaiKhoan tk = db.TaiKhoans.Find((User as DangNhapPrincipal).TaiKhoan.MaTK);
                tk.TongTinConLai -= 1;

                db.Entry(tk).State = EntityState.Modified;
                db.SaveChanges();

                //lưu hình ảnh vào csdl
                int maTin = db.Tins.Max(ma => ma.MaTin);
                int i     = 1;
                foreach (HttpPostedFileBase hinhanh in dtspvm.HinhAnhs)
                {
                    if (hinhanh != null)
                    {
                        string tenfile   = Path.GetFileNameWithoutExtension(hinhanh.FileName);
                        string extension = Path.GetExtension(hinhanh.FileName);
                        tenfile = tenfile + DateTime.Now.ToString("yymmssfff") + extension;

                        HinhAnhSanPham hasp = new HinhAnhSanPham();
                        hasp.MaTin  = maTin;
                        hasp.MaHinh = i;
                        i++;
                        hasp.DuongDan = "~/HinhAnhSanPham/" + tenfile;

                        tenfile = Path.Combine(Server.MapPath("~/HinhAnhSanPham/"), tenfile);
                        hinhanh.SaveAs(tenfile);


                        db.HinhAnhSanPhams.Add(hasp);
                        db.SaveChanges();
                    }
                }

                ModelState.Clear();

                ViewBag.DangTinThanhCong = 1;
                return(View());
            }
            return(View());
        }