Ejemplo n.º 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}"));
        }
Ejemplo n.º 2
0
        public ActionResult AddArchieveProblem(FormCollection form)
        {
            // 验证参数。
            if (string.IsNullOrEmpty(Request.QueryString["contestId"]) ||
                string.IsNullOrEmpty(form["ProblemId"]))
            {
                return(new ContentResult());
            }

            string problemId = form["ProblemId"];

            int contestId;

            if (!int.TryParse(Request.QueryString["contestId"], out contestId))
            {
                return(new ContentResult());
            }

            if (!ProblemArchieveManager.Default.IsProblemExist(problemId) ||
                !ContestManager.Default.IsContestExist(contestId))
            {
                return(new ContentResult());
            }

            using (ContestDataProvider contestData = ContestDataProvider.Create(new ContestHandle(contestId), false))
            {
                // 检查用户操作权限。
                if (!UserSession.IsAuthorized(Session) ||
                    string.Compare(contestData.Creator, UserSession.GetUsername(Session), false) != 0)
                {
                    return(new ContentResult());
                }

                // 执行操作。
                // 创建源题目的精确副本到当前比赛中。
                ProblemHandle newProblemHandle = ProblemArchieveManager.Default.CloneProblem(
                    contestData.PeekNextProblemId(), problemId);
                using (ProblemDataProvider problemData = ProblemDataProvider.Create(newProblemHandle, false))
                {
                    problemData.ContestId = contestId;
                }

                contestData.AddProblem(newProblemHandle);
            }

            return(new ContentResult());
        }