public ActionResult Edit(int id, DetailedProblemViewModel problem)
        {
            // TODO: Add validation with ModelState.IsValid
            if (problem != null && ModelState.IsValid)
            {
                var existingProblem = this.Data.Problems.All()
                .FirstOrDefault(x => x.Id == id);

                existingProblem.Name = problem.Name;
                existingProblem.MaximumPoints = problem.MaximumPoints;
                existingProblem.TimeLimit = problem.TimeLimit;
                existingProblem.MemoryLimit = problem.MemoryLimit;
                existingProblem.SourceCodeSizeLimit = problem.SourceCodeSizeLimit;
                existingProblem.Checker = this.Data.Checkers.All().FirstOrDefault(x => x.Name == problem.Checker);
                existingProblem.OrderBy = problem.OrderBy;

                this.Data.SaveChanges();

                this.TempData["InfoMessage"] = "Задачата беше променена успешно";
                return this.RedirectToAction("Contest", new { id = existingProblem.ContestId });
            }

            problem.AvailableCheckers = this.Data.Checkers.All().Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name });
            return this.View(problem);
        }
        public ActionResult Create(int id, HttpPostedFileBase testArchive, DetailedProblemViewModel problem)
        {
            if (problem.Resources != null && problem.Resources.Count() > 0)
            {
                var validResources = problem.Resources
                .All(res => !string.IsNullOrEmpty(res.Name) &&
                    ((res.Type == ProblemResourceType.AuthorsSolution && res.File != null && res.File.ContentLength > 0) ||
                    (res.Type == ProblemResourceType.ProblemDescription && res.File != null && res.File.ContentLength > 0) ||
                    (res.Type == ProblemResourceType.Video && !string.IsNullOrEmpty(res.Link))));

                if (!validResources)
                {
                    ModelState.AddModelError("Resources", "Ресурсите трябва да бъдат попълнени изцяло!");
                }
            }

            if (problem != null && ModelState.IsValid)
            {
                var newProblem = new Problem
                {
                    Name = problem.Name,
                    ContestId = id,
                    MaximumPoints = problem.MaximumPoints,
                    MemoryLimit = problem.MemoryLimit,
                    TimeLimit = problem.TimeLimit,
                    SourceCodeSizeLimit = problem.SourceCodeSizeLimit,
                    OrderBy = problem.OrderBy,
                    Checker = this.Data.Checkers.All().Where(x => x.Name == problem.Checker).FirstOrDefault()
                };

                if (problem.Resources != null && problem.Resources.Count() > 0)
                {
                    this.AddResourcesToProblem(newProblem, problem.Resources);
                }

                if (testArchive != null && testArchive.ContentLength != 0)
                {
                    try
                    {
                        this.AddTestsToProblem(newProblem, testArchive);
                    }
                    catch (Exception ex)
                    {
                        TempData.Add("DangerMessage", ex.Message);
                        problem.AvailableCheckers = this.Data.Checkers.All().Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name });
                        return this.View(problem);
                    }
                }

                this.Data.Problems.Add(newProblem);
                this.Data.SaveChanges();

                TempData.Add("InfoMessage", "Задачата беше добавена успешно");
                return this.RedirectToAction("Contest", new { id = id });
            }

            problem.AvailableCheckers = this.Data.Checkers.All().Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name });

            return this.View(problem);
        }
        public ActionResult Create(int? id)
        {
            if (id == null)
            {
                this.TempData["DangerMessage"] = "Невалидно състезание";
                return this.RedirectToAction("Index");
            }

            var contest = this.Data.Contests.All().FirstOrDefault(x => x.Id == id);

            if (contest == null)
            {
                this.TempData["DangerMessage"] = "Невалидно състезание";
                return this.RedirectToAction("Index");
            }

            var checkers = this.Data.Checkers.All()
                .Select(x => x.Name);

            var lastOrderBy = -1;
            var lastProblem = this.Data.Problems.All().Where(x => x.ContestId == id);

            if (lastProblem.Count() > 0)
            {
                lastOrderBy = lastProblem.Max(x => x.OrderBy);
            }

            var problem = new DetailedProblemViewModel
            {
                Name = "Име",
                MaximumPoints = 100,
                TimeLimit = 1000,
                MemoryLimit = 16777216,
                AvailableCheckers = this.Data.Checkers.All().Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name }),
                OrderBy = lastOrderBy + 1,
                ContestId = contest.Id,
                ContestName = contest.Name,
            };

            return this.View(problem);
        }
        public ActionResult Edit(int id, DetailedProblemViewModel problem)
        {
            if (problem != null && this.ModelState.IsValid)
            {
                var existingProblem = this.Data.Problems.All()
                .FirstOrDefault(x => x.Id == id);

                existingProblem.Name = problem.Name;
                existingProblem.MaximumPoints = problem.MaximumPoints;
                existingProblem.TimeLimit = problem.TimeLimit;
                existingProblem.MemoryLimit = problem.MemoryLimit;
                existingProblem.SourceCodeSizeLimit = problem.SourceCodeSizeLimit;
                existingProblem.ShowResults = problem.ShowResults;
                existingProblem.ShowDetailedFeedback = problem.ShowDetailedFeedback;
                existingProblem.Checker = this.Data.Checkers.All().FirstOrDefault(x => x.Name == problem.Checker);
                existingProblem.OrderBy = problem.OrderBy;

                this.Data.SaveChanges();

                this.TempData.AddInfoMessage(GlobalResource.Problem_edited);
                return this.RedirectToAction("Contest", new { id = existingProblem.ContestId });
            }

            problem.AvailableCheckers = this.Data.Checkers.All().Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name });
            return this.View(problem);
        }
        public ActionResult Create(int id, HttpPostedFileBase testArchive, DetailedProblemViewModel problem)
        {
            if (problem.Resources != null && problem.Resources.Any())
            {
                var validResources = problem.Resources
                .All(res => !string.IsNullOrEmpty(res.Name) &&
                    ((res.Type == ProblemResourceType.AuthorsSolution && res.File != null && res.File.ContentLength > 0) ||
                    (res.Type == ProblemResourceType.ProblemDescription && res.File != null && res.File.ContentLength > 0) ||
                    (res.Type == ProblemResourceType.Video && !string.IsNullOrEmpty(res.Link))));

                if (!validResources)
                {
                    this.ModelState.AddModelError("Resources", GlobalResource.Resources_not_complete);
                }
            }

            if (this.ModelState.IsValid)
            {
                var newProblem = new Problem
                {
                    Name = problem.Name,
                    ContestId = id,
                    MaximumPoints = problem.MaximumPoints,
                    MemoryLimit = problem.MemoryLimit,
                    TimeLimit = problem.TimeLimit,
                    SourceCodeSizeLimit = problem.SourceCodeSizeLimit,
                    ShowResults = problem.ShowResults,
                    ShowDetailedFeedback = problem.ShowDetailedFeedback,
                    OrderBy = problem.OrderBy,
                    Checker = this.Data.Checkers.All().FirstOrDefault(x => x.Name == problem.Checker)
                };

                if (problem.Resources != null && problem.Resources.Any())
                {
                    this.AddResourcesToProblem(newProblem, problem.Resources);
                }

                if (testArchive != null && testArchive.ContentLength != 0)
                {
                    try
                    {
                        this.AddTestsToProblem(newProblem, testArchive);
                    }
                    catch (Exception ex)
                    {
                        // TempData is not working with return this.View
                        var systemMessages = new SystemMessageCollection
                                {
                                    new SystemMessage
                                    {
                                        Content = ex.Message,
                                        Type = SystemMessageType.Error,
                                        Importance = 0
                                    }
                                };
                        this.ViewBag.SystemMessages = systemMessages;
                        problem.AvailableCheckers = this.Data.Checkers.All().Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name });
                        return this.View(problem);
                    }
                }

                this.Data.Problems.Add(newProblem);
                this.Data.SaveChanges();

                this.TempData.AddInfoMessage(GlobalResource.Problem_added);
                return this.RedirectToAction("Contest", new { id });
            }

            problem.AvailableCheckers = this.Data.Checkers.All().Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name });

            return this.View(problem);
        }
        public ActionResult Create(int? id)
        {
            if (id == null)
            {
                this.TempData.AddDangerMessage(GlobalResource.Invalid_contest);
                return this.RedirectToAction(GlobalConstants.Index);
            }

            var contest = this.Data.Contests.All().FirstOrDefault(x => x.Id == id);

            if (contest == null)
            {
                this.TempData.AddDangerMessage(GlobalResource.Invalid_contest);
                return this.RedirectToAction(GlobalConstants.Index);
            }

            var checkers = this.Data.Checkers.All()
                .Select(x => x.Name);

            var lastOrderBy = -1;
            var lastProblem = this.Data.Problems.All().Where(x => x.ContestId == id);

            if (lastProblem.Any())
            {
                lastOrderBy = lastProblem.Max(x => x.OrderBy);
            }

            var problem = new DetailedProblemViewModel
            {
                Name = "Име",
                MaximumPoints = 100,
                TimeLimit = 100,
                MemoryLimit = 16777216,
                AvailableCheckers = this.Data.Checkers.All().Select(checker => new SelectListItem { Text = checker.Name, Value = checker.Name }),
                OrderBy = lastOrderBy + 1,
                ContestId = contest.Id,
                ContestName = contest.Name,
                ShowResults = true,
                ShowDetailedFeedback = false,
            };

            return this.View(problem);
        }