public ActionResult Create(Product mbook, HttpPostedFileBase file)
        {
            ViewBag.listCate = db.Categorys.Where(m => m.status != 0 && m.ID > 2).ToList();
            if (ModelState.IsValid)
            {
                string slug = Mystring.ToSlug(mbook.name.ToString());
                if (db.Categorys.Where(m => m.slug == slug).Count() > 0)
                {
                    Message.set_flash("Sản phẩm đã tồn tại trong bảng Category", "danger");
                    return(View(mbook));
                }

                if (db.Products.Where(m => m.slug == slug).Count() > 0)
                {
                    Message.set_flash(" Sản phẩm đã tồn tại trong bảng book", "danger");
                    return(View(mbook));
                }
                // lấy tên loại sản phẩm
                var    namecateDb = db.Categorys.Where(m => m.ID == mbook.catid).First();
                string namecate   = Mystring.ToStringNospace(namecateDb.name);
                // lấy tên ảnh
                file = Request.Files["img"];
                string filename = file.FileName.ToString();
                //lấy đuôi ảnh
                string ExtensionFile = Mystring.GetFileExtension(filename);
                // lấy tên sản phẩm làm slug

                //lấy tên mới của ảnh slug + [đuôi ảnh lấy đc]
                string namefilenew = namecate + "/" + slug + "." + ExtensionFile;
                //lưu ảnh vào đường đẫn
                var path = Path.Combine(Server.MapPath("~/public/images/product"), namefilenew);
                //nếu thư mục k tồn tại thì tạo thư mục
                var folder = Server.MapPath("~/public/images/product/" + namecate);
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }
                file.SaveAs(path);
                mbook.img  = namefilenew;
                mbook.slug = slug;

                mbook.created_at = DateTime.Now;
                mbook.created_by = int.Parse(Session["Admin_id"].ToString());
                db.Products.Add(mbook);
                db.SaveChanges();
                //create Link
                link tt_link = new link();
                tt_link.slug     = slug;
                tt_link.tableId  = 1;
                tt_link.type     = "ProductDetail";
                tt_link.parentId = mbook.ID;
                db.Link.Add(tt_link);
                db.SaveChanges();

                Message.set_flash("Thêm thành công", "success");
                return(RedirectToAction("index"));
            }
            Message.set_flash("Thêm Thất Bại", "danger");
            return(View(mbook));
        }
 public ActionResult Create(Mpost mpost)
 {
     if (ModelState.IsValid)
     {
         HttpPostedFileBase file;
         var    namecateDb = db.topics.Where(m => m.ID == mpost.topid).First();
         string slug       = Mystring.ToSlug(mpost.title.ToString());
         string namecate   = Mystring.ToStringNospace(namecateDb.name);
         file = Request.Files["img"];
         string filename      = file.FileName.ToString();
         string ExtensionFile = Mystring.GetFileExtension(filename);
         string namefilenew   = namecate + "/" + slug + "." + ExtensionFile;
         var    path          = Path.Combine(Server.MapPath("~/public/images/post/"), namefilenew);
         var    folder        = Server.MapPath("~/public/images/" + namecate);
         if (!Directory.Exists(folder))
         {
             Directory.CreateDirectory(folder);
         }
         file.SaveAs(path);
         mpost.img        = namefilenew;
         mpost.slug       = slug;
         mpost.type       = "Post";
         mpost.created_at = DateTime.Now;
         mpost.updated_at = DateTime.Now;
         mpost.created_by = int.Parse(Session["Admin_id"].ToString());
         mpost.updated_by = int.Parse(Session["Admin_id"].ToString());
         db.posts.Add(mpost);
         db.SaveChanges();
         Message.set_flash("Thêm thành công", "success");
         return(RedirectToAction("Index"));
     }
     ViewBag.listTopic = db.topics.Where(m => m.status != 0).ToList();
     Message.set_flash("Thêm Thất Bại", "danger");
     return(View(mpost));
 }
 public ActionResult Edit(Mproduct mproduct, HttpPostedFileBase file)
 {
     if (ModelState.IsValid)
     {
         string slug = Mystring.ToSlug(mproduct.name.ToString());
         file = Request.Files["img"];
         string filename = file.FileName.ToString();
         if (filename.Equals("") == false)
         {
             var    namecateDb    = db.Categorys.Where(m => m.ID == mproduct.catid).First();
             string namecate      = Mystring.ToStringNospace(namecateDb.name);
             string ExtensionFile = Mystring.GetFileExtension(filename);
             string namefilenew   = namecate + "/" + slug + "." + ExtensionFile;
             var    path          = Path.Combine(Server.MapPath("~/public/images"), namefilenew);
             var    folder        = Server.MapPath("~/public/images/" + namecate);
             if (!Directory.Exists(folder))
             {
                 Directory.CreateDirectory(folder);
             }
             file.SaveAs(path);
             mproduct.img = namefilenew;
         }
         mproduct.slug            = slug;
         mproduct.updated_at      = DateTime.Now;
         mproduct.updated_by      = int.Parse(Session["Admin_id"].ToString());
         db.Entry(mproduct).State = EntityState.Modified;
         db.SaveChanges();
         ViewBag.listCate = db.Categorys.Where(m => m.status != 0 && m.ID > 2).ToList();
         Message.set_flash("Sửa thành công", "success");
         return(RedirectToAction("Index"));
     }
     Message.set_flash("Sửa thất bại", "danger");
     ViewBag.listCate = db.Categorys.Where(m => m.status != 0 && m.ID > 2).ToList();
     return(View(mproduct));
 }
Esempio n. 4
0
        public ActionResult Edit(Post post, HttpPostedFileBase file)
        {
            file = Request.Files["img"];
            string filename = file.FileName.ToString();

            if (filename.Equals("") == false)
            {
                string getJsonRepons = SendRequest.sendRequestGET(ApiUrl.urlFindTopicById + post.Topid, null);
                Topic  topic         = JsonConvert.DeserializeObject <Topic>(getJsonRepons);
                string slug          = Mystring.ToSlug(post.Title.ToString()) + DateTime.Now.ToString("-mmss");
                string namecate      = Mystring.ToStringNospace(topic.Name);
                string ExtensionFile = Mystring.GetFileExtension(filename);
                string namefilenew   = namecate + "/" + slug + "." + ExtensionFile;
                var    path          = Path.Combine(Server.MapPath("~/public/images/post"), namefilenew);
                var    folder        = Server.MapPath("~/public/images/post/" + namecate);
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }
                file.SaveAs(path);
                post.Img = namefilenew;
            }
            post.Updated_at = DateTime.Now;
            JObject PostJson = new JObject
            {
                { "Id", post.ID },
                { "Topid", post.Topid },
                { "Title", post.Title },
                { "Slug", "" },
                { "Detail", post.Detail },
                { "Img", post.Img },
                { "Type", post.Type },
                { "Metakey", post.Metakey },
                { "Metadesc", post.Metadesc },
                { "Created_at", post.Created_at.ToString("yyyy-MM-ddTHH:mm:ss") },
                { "Created_by", post.Created_by },
                { "Updated_at", post.Updated_at.ToString("yyyy-MM-ddTHH:mm:ss") },
                { "Updated_by", post.Updated_by },
                { "Status", post.Status },
            };
            string EditResult   = SendRequest.sendRequestPOSTwithJsonContent(ApiUrl.urlEditPost, PostJson.ToString());
            var    objectResult = JsonConvert.DeserializeObject <ObjectResult <Post> >(EditResult);

            if (objectResult.code == 200)
            {
                Message.set_flash(objectResult.message.Message, "success");
                return(RedirectToAction("index"));
            }
            else
            {
                Message.set_flash(objectResult.message.Message, "danger");
            }
            string getJsonAllTopicRepons = SendRequest.sendRequestGET(ApiUrl.urlGetAllTopic, null);

            ViewBag.listtopic = JsonConvert.DeserializeObject <List <Topic> >(getJsonAllTopicRepons);
            return(View(""));
        }
        public ActionResult Edit(Product mbook, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                string slug = Mystring.ToSlug(mbook.name.ToString());
                file = Request.Files["img"];
                string filename = file.FileName.ToString();
                if (filename.Equals("") == false)
                {
                    var    namecateDb    = db.Categorys.Where(m => m.ID == mbook.catid).First();
                    string namecate      = Mystring.ToStringNospace(namecateDb.name);
                    string ExtensionFile = Mystring.GetFileExtension(filename);
                    string namefilenew   = namecate + "/" + slug + "." + ExtensionFile;
                    var    path          = Path.Combine(Server.MapPath("~/public/images/product"), namefilenew);
                    var    folder        = Server.MapPath("~/public/images/product/" + namecate);
                    if (!Directory.Exists(folder))
                    {
                        Directory.CreateDirectory(folder);
                    }
                    file.SaveAs(path);
                    mbook.img = namefilenew;
                }
                mbook.slug = slug;
                try
                {
                    var  thisLink = db.Link.Where(m => m.tableId == 1 && m.parentId == mbook.ID).First();
                    link tt_link  = db.Link.Find(thisLink.ID);
                    tt_link.slug            = slug;
                    tt_link.tableId         = 1;
                    tt_link.parentId        = mbook.ID;
                    db.Entry(tt_link).State = EntityState.Modified;
                }
                catch (Exception)
                {
                    //no runing
                }

                db.Entry(mbook).State = EntityState.Modified;
                db.SaveChanges();
                ViewBag.listCate = db.Categorys.Where(m => m.status != 0 && m.ID > 2).ToList();
                Message.set_flash("Sửa thành công", "success");
                return(RedirectToAction("Index"));
            }
            Message.set_flash("Sửa thất bại", "danger");
            ViewBag.listCate = db.Categorys.Where(m => m.status != 0 && m.ID > 2).ToList();
            return(View(mbook));
        }
        public ActionResult Index(Morder order)
        {
            Random rand          = new Random((int)DateTime.Now.Ticks);
            int    numIterations = 0;

            numIterations = rand.Next(1, 100000);
            DateTime time           = DateTime.Now;
            string   orderCode      = Mystring.ToStringNospace(numIterations + "" + time);
            string   sumOrder       = Request["sumOrder"];
            string   payment_method = Request["option_payment"];

            // Neu Ship COde
            if (payment_method.Equals("COD"))
            {
                // cap nhat thong tin sau khi dat hang thanh cong

                saveOrder(order, "COD", 2, orderCode);
                var cart = Session[SessionCart];
                var list = new List <Cart_item>();
                ViewBag.cart           = (List <Cart_item>)cart;
                Session["SessionCart"] = null;
                var listProductOrder = db.Orders.Where(m => m.ID == order.ID).FirstOrDefault();
                return(View("oderComplete", listProductOrder));
            }
            //Neu Thanh toan MOMO
            else if (payment_method.Equals("MOMO"))
            {
                //request params need to request to MoMo system
                string endpoint    = momoInfo.endpoint;
                string partnerCode = momoInfo.partnerCode;
                string accessKey   = momoInfo.accessKey;
                string serectkey   = momoInfo.serectkey;
                string orderInfo   = momoInfo.orderInfo;
                string returnUrl   = momoInfo.returnUrl;
                string notifyurl   = momoInfo.notifyurl;

                string amount    = sumOrder;
                string orderid   = Guid.NewGuid().ToString();
                string requestId = Guid.NewGuid().ToString();
                string extraData = "";

                //Before sign HMAC SHA256 signature
                string rawHash = "partnerCode=" +
                                 partnerCode + "&accessKey=" +
                                 accessKey + "&requestId=" +
                                 requestId + "&amount=" +
                                 amount + "&orderId=" +
                                 orderid + "&orderInfo=" +
                                 orderInfo + "&returnUrl=" +
                                 returnUrl + "&notifyUrl=" +
                                 notifyurl + "&extraData=" +
                                 extraData;

                log.Debug("rawHash = " + rawHash);

                MoMoSecurity crypto = new MoMoSecurity();
                //sign signature SHA256
                string signature = crypto.signSHA256(rawHash, serectkey);
                log.Debug("Signature = " + signature);

                //build body json request
                JObject message = new JObject
                {
                    { "partnerCode", partnerCode },
                    { "accessKey", accessKey },
                    { "requestId", requestId },
                    { "amount", amount },
                    { "orderId", orderid },
                    { "orderInfo", orderInfo },
                    { "returnUrl", returnUrl },
                    { "notifyUrl", notifyurl },
                    { "extraData", extraData },
                    { "requestType", "captureMoMoWallet" },
                    { "signature", signature }
                };
                log.Debug("Json request to MoMo: " + message.ToString());
                string  responseFromMomo = PaymentRequest.sendPaymentRequest(endpoint, message.ToString());
                JObject jmessage         = JObject.Parse(responseFromMomo);

                saveOrder(order, "Cổng thanh toán MOMO", 2, orderid);
                return(Redirect(jmessage.GetValue("payUrl").ToString()));
            }
            //Neu Thanh toan Ngan Luong
            else if (payment_method.Equals("NL"))
            {
                string      str_bankcode = Request["bankcode"];
                RequestInfo info         = new RequestInfo();
                info.Merchant_id       = nganluongInfo.Merchant_id;
                info.Merchant_password = nganluongInfo.Merchant_password;
                info.Receiver_email    = nganluongInfo.Receiver_email;
                info.cur_code          = "vnd";
                info.bank_code         = str_bankcode;
                info.Order_code        = orderCode;
                info.Total_amount      = sumOrder;
                info.fee_shipping      = "0";
                info.Discount_amount   = "0";
                info.order_description = "Thanh toán ngân lượng cho đơn hàng";
                info.return_url        = nganluongInfo.return_url;
                info.cancel_url        = nganluongInfo.cancel_url;
                info.Buyer_fullname    = order.deliveryname;
                info.Buyer_email       = order.deliveryemail;
                info.Buyer_mobile      = order.deliveryphone;
                APICheckoutV3 objNLChecout = new APICheckoutV3();
                ResponseInfo  result       = objNLChecout.GetUrlCheckout(info, payment_method);
                // neu khong gap loi gi
                if (result.Error_code == "00")
                {
                    saveOrder(order, "Cổng thanh toán Ngân Lượng", 2, orderCode);
                    // chuyen sang trang ngan luong
                    return(Redirect(result.Checkout_url));
                }
                else
                {
                    ViewBag.errorPaymentOnline = result.Description;
                    return(View("payment"));
                }
            }
            //Neu Thanh Toán ATM online
            else if (payment_method.Equals("ATM_ONLINE"))
            {
                string      str_bankcode = Request["bankcode"];
                RequestInfo info         = new RequestInfo();
                info.Merchant_id       = nganluongInfo.Merchant_id;
                info.Merchant_password = nganluongInfo.Merchant_password;
                info.Receiver_email    = nganluongInfo.Receiver_email;
                info.cur_code          = "vnd";
                info.bank_code         = str_bankcode;
                info.Order_code        = orderCode;
                info.Total_amount      = sumOrder;
                info.fee_shipping      = "0";
                info.Discount_amount   = "0";
                info.order_description = "Thanh toán ngân lượng cho đơn hàng";
                info.return_url        = nganluongInfo.return_url;
                info.cancel_url        = nganluongInfo.cancel_url;
                info.Buyer_fullname    = order.deliveryname;
                info.Buyer_email       = order.deliveryemail;
                info.Buyer_mobile      = order.deliveryphone;
                APICheckoutV3 objNLChecout = new APICheckoutV3();
                ResponseInfo  result       = objNLChecout.GetUrlCheckout(info, payment_method);
                // neu khong gap loi gi
                if (result.Error_code == "00")
                {
                    saveOrder(order, "ATM Online qua ngân lượng", 2, orderCode);
                    return(Redirect(result.Checkout_url));
                }
                else
                {
                    ViewBag.errorPaymentOnline = result.Description;
                    return(View("payment"));
                }
            }
            return(View("payment"));
        }