Exemple #1
0
        public ActionResult DangKyHocPhan(List <string> DanhSachHocPhan, List <string> MonHoc)
        {
            if (!isUserNameExist())
            {
                return(RedirectToAction("Index"));
            }
            //Create client connection to our MongoDB database
            var client = new MongoClient(new configWEB().connectionstring);

            //Create a session object that is used when leveraging transactions


            MonHocModel  monHocModel  = new MonHocModel();
            HocPhanModel hocPhanModel = new HocPhanModel();
            AccountModel accountModel = new AccountModel();
            Account      account      = new AccountModel().find_username(Session[currentAccount].ToString());

            List <string> HocPhanDaDangKy_OLD = account.HocPhanDaDangKy;
            List <bool>   trungID             = new List <bool>();

            for (int i = 0; i < HocPhanDaDangKy_OLD.Count(); i++)
            {
                if (HocPhanDaDangKy_OLD[i] == DanhSachHocPhan[i])
                {
                    trungID.Add(true);
                }
                else
                {
                    trungID.Add(false);
                }
            }
            string monHoc_Success = "";
            string monHoc_ThatBai = "";
            bool   isThatBai      = false;

            if (account != null)
            {
                using (var session = client.StartSession())
                {
                    account.Status = true;
                    accountModel.update(account);
                    session.StartTransaction(new TransactionOptions(
                                                 readConcern: ReadConcern.Snapshot,
                                                 writeConcern: WriteConcern.WMajority));
                    try
                    {
                        account.Status          = false;
                        account.HocPhanDaDangKy = DanhSachHocPhan;
                        accountModel.updateHocPhanDaDangKy(account, session);
                        for (int i = 0; i < DanhSachHocPhan.Count(); i++)
                        {
                            if (DanhSachHocPhan[i] != "")
                            {
                                while (monHocModel.getHocphan(MonHoc[i], account.HocPhanDaDangKy[i]).Status == true)
                                {
                                    if (monHocModel.getHocphan(MonHoc[i], account.HocPhanDaDangKy[i]).Status == false)
                                    {
                                        break;
                                    }
                                }
                                monHocModel.lockHocPhan(DanhSachHocPhan[i]);

                                if (monHocModel.ConLai(account.HocPhanDaDangKy[i],
                                                       monHocModel.getHocphan(MonHoc[i],
                                                                              account.HocPhanDaDangKy[i]).SiSo) > 0 || trungID[i] == true)
                                {
                                    monHoc_Success = monHoc_Success + ", " + monHocModel.find(MonHoc[i]).TenMonHoc + " ";
                                }
                                else
                                {
                                    monHoc_ThatBai = monHoc_ThatBai + ", " + monHocModel.find(MonHoc[i]).TenMonHoc + " ";
                                    isThatBai      = true;
                                }
                            }
                        }



                        Thread.Sleep(3000);



                        for (int i = 0; i < DanhSachHocPhan.Count(); i++)
                        {
                            if (DanhSachHocPhan[i] != "")
                            {
                                monHocModel.unlockHocPhan(DanhSachHocPhan[i]);
                            }
                        }

                        if (isThatBai)
                        {
                            ThongBao_Error("học phần môn " + monHoc_ThatBai + " đã hết chỗ , vui lòng chọn học phần khác");
                            session.AbortTransactionAsync();
                            ViewBag.listMonHoc  = monHocModel.findAll();
                            ViewBag.accountInfo = accountModel.find_username(Session[currentAccount].ToString());

                            account.Status          = false;
                            account.HocPhanDaDangKy = HocPhanDaDangKy_OLD;
                            accountModel.update(account);
                            return(View(HocPhanDaDangKy_OLD));
                        }
                        else
                        {
                            ThongBao_Success("Đăng ký học phần " + monHoc_Success + "  thành công!");
                            session.CommitTransaction();
                            ViewBag.listMonHoc  = monHocModel.findAll();
                            ViewBag.accountInfo = accountModel.find_username(Session[currentAccount].ToString());
                            return(View(DanhSachHocPhan));
                        }
                    }
                    catch (Exception e)
                    {
                        account.Status          = false;
                        account.HocPhanDaDangKy = HocPhanDaDangKy_OLD;
                        accountModel.update(account);

                        ThongBao_Error("Error writing to MongoDB: " + e.Message);
                        session.AbortTransaction();
                        ViewBag.listMonHoc  = monHocModel.findAll();
                        ViewBag.accountInfo = accountModel.find_username(Session[currentAccount].ToString());
                        return(View(HocPhanDaDangKy_OLD));
                    }
                }
            }



            ViewBag.listMonHoc  = monHocModel.findAll();
            ViewBag.accountInfo = accountModel.find_username(Session[currentAccount].ToString());
            return(View(DanhSachHocPhan));
        }
 public ActionResult Delete(string id)
 {
     if (id == null)
     {
         return(RedirectToAction("Index"));
     }
     else if (model.find(id) == null)
     {
         return(RedirectToAction("Index"));
     }
     model.delete(id);
     return(RedirectToAction("Index"));
 }