Esempio n. 1
0
        public ActionResult GetCreateBook(BookModel inputData, HttpPostedFileBase[] fileImages)
        {
            string path  = Server.MapPath("~/UploadFile/");
            string path2 = "C:\\xampp\\htdocs\\apiFahasa\\image\\";
            int    result;
            var    dao         = new BookDao();
            var    phanloaidao = new GroupPhanLoaiDao();

            if (dao.CheckBookExist(inputData.TENSACH, inputData.NHAXUATBAN))
            {
                result = -1;
            }
            else
            {
                var book    = new SANPHAM();
                var session = (UserLogin)Session[QLNhaSachFahasa.Common.CommonConstants.USER_SEESION];

                book.MASANPHAM   = dao.CreateIDAuto("SH");
                book.TENSANPHAM  = inputData.TENSACH;
                book.PHANLOAI    = inputData.MAPHANLOAISACH.Trim();
                book.TRANGTHAI   = 1;
                book.NGONNGU     = inputData.MANGONNGU;
                book.HINHTHUC    = inputData.MAHINHTHUC;
                book.TACGIA      = inputData.TACGIA;
                book.DONGIA      = inputData.GIASACH;
                book.TRONGLUONG  = inputData.TRONGLUONG;
                book.SOTRANG     = inputData.SOTRANG;
                book.KICHTHUOC   = inputData.KICHTHUOC;
                book.GHICHU      = inputData.TOMTAC;
                book.NHAXUATBAN  = inputData.NHAXUATBAN;
                book.NGAYTAO     = DateTime.Now;
                book.NGAYCAPNHAT = DateTime.Now;
                book.NGUOITAO    = session.UserID;
                book.SOLUONG     = 0;
                book.LUOTXEM     = 0;
                book.SOLUONG     = inputData.SoLuong;
                book.NHACUNGCAP  = inputData.NHACUNGCAP;
                result           = dao.InserBook(book);
                decimal gia = inputData.GIASACH;
                if (!string.IsNullOrEmpty(inputData.GIABAN.ToString()) && inputData.GIABAN != 0)
                {
                    gia = inputData.GIABAN;
                }
                var updateGiaBan = new SanPhamDao().UpdateGiaBan(book.MASANPHAM, gia, book.NGUOITAO);
                // Upload file lên server
                if (fileImages != null)
                {
                    foreach (var file in fileImages)
                    {
                        if (file != null)
                        {
                            var    images        = new HINHANH();
                            var    imageDao      = new UploadHinhAnhSanPhamDao();
                            string extensionName = System.IO.Path.GetExtension(file.FileName);
                            var    fileInfo      = new FileInfo(file.FileName);
                            string finalFileName = DateTime.Now.Ticks.ToString() + extensionName;
                            file.SaveAs(path + finalFileName);
                            file.SaveAs(path2 + finalFileName);
                            //Insert database
                            images.LINKHINHANH = finalFileName;
                            images.MASANPHAM   = book.MASANPHAM;
                            images.TENHINHANH  = file.FileName;

                            result = imageDao.Insert(images);
                        }
                    }
                }
            }
            return(Json(result, JsonRequestBehavior.AllowGet));
        }