Пример #1
0
        public ActionResult AddProblem(ProblemDetailModel model)
        {
            // 检查参数。
            if (string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                return(Redirect("~/Contest"));
            }

            int contestId;

            if (!int.TryParse(Request.QueryString["id"], out contestId))
            {
                return(Redirect("~/Contest"));
            }

            if (!ContestManager.Default.IsContestExist(contestId))
            {
                return(Redirect("~/Contest"));
            }

            // 验证数据模型。
            if (!TryValidateModel(model))
            {
                ViewBag.TitleErrorMessage = ModelStateHelper.GetFirstError(ModelState, "Title");
                return(View(model));
            }

            ContestHandle contestHandle = new ContestHandle(contestId);

            using (ContestDataProvider contestData = ContestDataProvider.Create(contestHandle, false))
            {
                // 验证用户操作权限。
                if (!UserSession.IsAuthorized(Session) ||
                    string.Compare(contestData.Creator, UserSession.GetUsername(Session), false) != 0)
                {
                    return(Redirect("~/Error/AccessDenied"));
                }

                // 在题目库中创建题目。
                ProblemHandle problemHandle = ProblemArchieveManager.Default.CreateProblem(
                    contestData.PeekNextProblemId());

                model.ResetNullStrings();
                model.SaveTo(problemHandle);            // 为避免死锁,该句不应放置在下面的 using 块中。

                using (ProblemDataProvider problemData = ProblemDataProvider.Create(problemHandle, false))
                {
                    problemData.ContestId          = contestId;
                    problemData.AuthorizationGroup = UserGroup.Guests;
                }

                // 将创建的题目添加至当前比赛中。
                contestData.AddProblem(problemHandle);
            }

            return(Redirect($"~/Contest/Show?id={contestId}"));
        }
Пример #2
0
        private bool ValidateContestDetailModel(ContestDetailModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            bool validationPassed = TryValidateModel(model);

            if (!validationPassed)
            {
                ViewBag.TitleErrorMessage                 = ModelStateHelper.GetFirstError(ModelState, "Title");
                ViewBag.CreatorErrorMessage               = ModelStateHelper.GetFirstError(ModelState, "Creator");
                ViewBag.UsergroupNameErrorMessage         = ModelStateHelper.GetFirstError(ModelState, "UsergroupName");
                ViewBag.StartTimeStringErrorMessage       = ModelStateHelper.GetFirstError(ModelState, "StartTimeString");
                ViewBag.EndTimeStringErrorMessage         = ModelStateHelper.GetFirstError(ModelState, "EndTimeString");
                ViewBag.ParticipationModeNameErrorMessage = ModelStateHelper.GetFirstError(ModelState, "ParticipationModeName");
                ViewBag.AuthorizationModeNameErrorMessage = ModelStateHelper.GetFirstError(ModelState, "AuthorizationModeName");
            }

            if (string.Compare(model.AuthorizationModeName, "Protected", true) == 0 &&
                (string.IsNullOrEmpty(model.Password) || model.Password.Length < 6))
            {
                validationPassed             = false;
                ViewBag.PasswordErrorMessage = "Password should be at least 6 characters long.";
            }

            DateTime startTime = DateTime.Parse(model.StartTimeString);
            DateTime endTime   = DateTime.Parse(model.EndTimeString);

            if (endTime <= startTime)
            {
                validationPassed = false;
                ViewBag.EndTimeStringErrorMessage = "End time must be later than start time.";
            }

            return(validationPassed);
        }