Exemplo n.º 1
0
        public ActionResult Term()
        {
            checkLogin();

            TERM previousT = TermService.GetPreviousTerm();
            TERM currentT  = TermService.GetCurrentTerm();
            TERM nextT     = TermService.GetNextTerm();

            Session["previousT"] = previousT;
            Session["currentT"]  = currentT;
            Session["nextT"]     = nextT;


            return(View(new TermViewModel(previousT, currentT, nextT)));
        }
Exemplo n.º 2
0
        public ActionResult TermPost(TERM next)
        {
            checkLogin();

            TERM previousT = Session["previousT"] != null? (TERM)Session["previousT"]: TermService.GetPreviousTerm();
            TERM currentT  = Session["currentT"] != null ? (TERM)Session["currentT"] : TermService.GetCurrentTerm();
            TERM nextT     = Session["nextT"] != null ? (TERM)Session["nextT"] : TermService.GetNextTerm();

            TermViewModel viewModel = new TermViewModel(previousT, currentT, nextT);

            TempData["showForm"]  = false;
            TempData["resultMsg"] = "";

            // fields checking
            if (!ModelState.IsValid)
            {
                TempData["showForm"] = true;
                return(View("Term", viewModel)); // redirect to form with data
            }
            // logic checking
            bool pass = true;

            if (TermService.GetTermByName(next.TERM_NAME).ID != 0 && TermService.GetTermByName(next.TERM_NAME).ID != next.ID)
            {
                TempData[Constant.msg_error] = "term name already in use";
                pass = false;
            }

            if (pass && next.TERM_START >= next.TERM_END)
            {
                TempData[Constant.msg_error] = "start date > end date";
                pass = false;
            }

            if (pass && next.TERM_START < DateTime.Now.Date)
            {
                TempData[Constant.msg_error] = "start date < today";
                pass = false;
            }

            if (pass && (currentT != null) && currentT.TERM_END >= next.TERM_START)
            {
                TempData[Constant.msg_error] = "current term end date > next term start date";
                pass = false;
            }

            if (!pass)
            {
                TempData["showForm"] = true;
                return(View("Term", viewModel));
            }// redirect to form with data

            next.ACTIVE = true;
            bool isSuccess = false;

            if (next.ID == Constant.DEF_INT)
            {
                isSuccess = DBS.addRecord(next);
            }
            else
            {
                isSuccess = DBS.updateRecord(next);
            }

            // some unkown error happened
            if (!isSuccess)
            {
                TempData[Constant.msg_error] = "Ops! something gone wrong.";
                TempData["showForm"]         = true;
                return(View("Term", viewModel)); // redirect to form with data
            }

            TempData[Constant.msg_success] = ((next.ID == Constant.DEF_INT) ? "New term has been created" : "Next term has been edited");

            //regen lists
            previousT = TermService.GetPreviousTerm();
            currentT  = TermService.GetCurrentTerm();
            nextT     = TermService.GetNextTerm();

            return(View(new TermViewModel(previousT, currentT, nextT))); // success
        }