private void SaveProblem(viewModels.EditProblemViewModel Model, int ProblemID, DateTime LastModifiedTime)
        {
            switch ((ProblemTypes)Model.ProblemTypesListID)
            {
            case ProblemTypes.Standart:
                SaveProblemStandart(Model, ProblemID, LastModifiedTime);
                break;

            case ProblemTypes.Open:
                SaveProblemOpen(Model, ProblemID, LastModifiedTime);
                break;
            }
        }
        private ActionResult UpdateProblem(viewModels.EditProblemViewModel Model, int ProblemID = -1)
        {
            try
            {
                Problem problem = repository.Problems.FirstOrDefault(p => p.ProblemID == ProblemID);

                if (problem == null)
                {
                    logger.Warn("Problem with id = " + ProblemID + " not found");
                    throw new HttpException(404, "Problem not found");
                }

                problem.Name             = Model.Name;
                problem.TimeLimit        = Model.TimeLimit;
                problem.MemoryLimit      = Model.MemoryLimit;
                problem.LastModifiedTime = DateTime.Now.Truncate(TimeSpan.FromSeconds(1));
                problem.CheckPending     = Model.CheckPending;
                //problem.Type = (ProblemTypes)Model.ProblemTypesListID;
                if (Model.ProblemTagsListIDs != null)
                {
                    foreach (var tagId in Model.ProblemTagsListIDs)
                    {
                        var tag = repository.ProblemTags.FirstOrDefault(t => t.ProblemTagID == tagId);
                        if (tag != null)
                        {
                            problem.Tags.Add(tag);
                        }
                    }
                }

                repository.AddProblem(problem);

                SaveProblem(Model, ProblemID, problem.LastModifiedTime);

                logger.Info("User " + WebSecurity.GetUserId(User.Identity.Name) +
                            " \"" + User.Identity.Name + "\" update problem \"" + ProblemID + "\"");

                TestersSingleton.Instance.SendProblem(ProblemID);

                TempData["SuccessMessage"] = "Задача успешно обновлена!";
            }
            catch (Exception ex)
            {
                TempData["ErrorMessage"] = "Произошла ошибка при обновлении задачи.";
                logger.Error("Error occured on user " + WebSecurity.GetUserId(User.Identity.Name) +
                             " \"" + User.Identity.Name + "\" updating problem \"" + ProblemID + "\": ", ex);
            }

            return(RedirectToAction("Update", new { ProblemID = ProblemID }));
        }
        public ActionResult Update(viewModels.EditProblemViewModel Model, int ProblemID = -1, string Update = null, string Delete = null, string Cancel = null)
        {
            if (ProblemID == -1)
            {
                logger.Warn("Problem with id = " + ProblemID + " not found");
                throw new HttpException(404, "Problem not found");
            }

            if (Delete != null)
            {
                return(DeleteProblem(ProblemID));
            }
            if (Cancel != null)
            {
                return(CancelProblem());
            }
            if (Update != null)
            {
                return(UpdateProblem(Model, ProblemID));
            }

            return(CancelProblem());
        }
        private void SaveProblemOpen(viewModels.EditProblemViewModel Model, int ProblemID, DateTime LastModifiedTime)
        {
            // Write model data to App_Data

            string dirName = ProblemID.ToString();

            if (!Directory.Exists(LocalPath.AbsoluteProblemsDirectory + dirName))
            {
                Directory.CreateDirectory(LocalPath.AbsoluteProblemsDirectory + dirName);
            }

            if (System.IO.File.Exists(LocalPath.AbsoluteProblemsDirectory + dirName + "/" + dirName + ".zip"))
            {
                System.IO.File.Delete(LocalPath.AbsoluteProblemsDirectory + dirName + "/" + dirName + ".zip");
            }

            // Write problem legend
            ProblemLegend.Write(LocalPath.AbsoluteProblemsDirectory + dirName,
                                Model.Name, Model.TimeLimit, Model.MemoryLimit, LastModifiedTime, ProblemTypes.Open,
                                Model.Description, Model.InputFormat, Model.OutputFormat);

            SaveChecker(Model.CheckerListID, Model.Checker, dirName);

            if (Model.OpenProblemResultDropDownID == "other")
            {
                if (System.IO.File.Exists(LocalPath.AbsoluteProblemsDirectory + dirName + "OpenProblemResult"))
                {
                    System.IO.File.Delete(LocalPath.AbsoluteProblemsDirectory + dirName + "OpenProblemResult");
                }
                SaveOpenProblemResult(Model.OpenProblemResult, dirName);
            }

            // Save attachment
            List <string> attach = new List <string>();

            attach.AddRange(ParseImagesPath(Model.Description));
            attach.AddRange(ParseImagesPath(Model.InputFormat));
            attach.AddRange(ParseImagesPath(Model.OutputFormat));
            SaveImages(attach, LocalPath.AbsoluteProblemsDirectory + dirName);

            // Create zip for tester
            ZipFile.CreateFromDirectory(
                LocalPath.AbsoluteProblemsDirectory + dirName,
                LocalPath.AbsoluteProblemsDirectory + dirName + ".zip");

            // Delete description, inputFormat and outputFormat from archive
            using (ZipArchive archive = ZipFile.Open(
                       LocalPath.AbsoluteProblemsDirectory + dirName + ".zip", ZipArchiveMode.Update))
            {
                List <ZipArchiveEntry> forDelete = new List <ZipArchiveEntry>();

                foreach (ZipArchiveEntry entry in archive.Entries)
                {
                    if (entry.Name == "Description" ||
                        entry.Name == "InputFormat" ||
                        entry.Name == "OutputFormat")
                    {
                        forDelete.Add(entry);
                    }
                }

                forDelete.Each(e => archive.Entries.First(en => en == e).Delete());
            }

            // Move tester zip
            if (System.IO.File.Exists(LocalPath.AbsoluteProblemsDirectory + dirName + ".zip"))
            {
                System.IO.File.Move(LocalPath.AbsoluteProblemsDirectory + dirName + ".zip",
                                    LocalPath.AbsoluteProblemsDirectory + dirName + "/" + dirName + ".zip");
            }
        }
        private void SaveProblemStandart(viewModels.EditProblemViewModel Model, int ProblemID, DateTime LastModifiedTime)
        {
            // Write model data to App_Data

            string dirName = ProblemID.ToString();

            if (!Directory.Exists(LocalPath.AbsoluteProblemsDirectory + dirName))
            {
                throw new DirectoryNotFoundException("Problem directory not found");
            }

            if (System.IO.File.Exists(LocalPath.AbsoluteProblemsDirectory + dirName + "/" + dirName + ".zip"))
            {
                System.IO.File.Delete(LocalPath.AbsoluteProblemsDirectory + dirName + "/" + dirName + ".zip");
            }

            // Write problem legend
            ProblemLegend.Write(LocalPath.AbsoluteProblemsDirectory + dirName,
                                Model.Name, Model.TimeLimit, Model.MemoryLimit, LastModifiedTime, ProblemTypes.Standart,
                                Model.Description, Model.InputFormat, Model.OutputFormat);

            SaveChecker(Model.CheckerListID, Model.Checker, dirName);

            if (Model.TestsDropDownID == "other")
            {
                if (Directory.Exists(LocalPath.AbsoluteProblemsDirectory + dirName + "/Tests"))
                {
                    Directory.Delete(LocalPath.AbsoluteProblemsDirectory + dirName + "/Tests", true);
                }
                SaveTests(Model.Tests, dirName);
            }

            // Save attachment
            List <string> attach = new List <string>();

            attach.AddRange(ParseImagesPath(Model.Description));
            attach.AddRange(ParseImagesPath(Model.InputFormat));
            attach.AddRange(ParseImagesPath(Model.OutputFormat));
            SaveImages(attach, LocalPath.AbsoluteProblemsDirectory + dirName);

            // Create zip for tester
            if (System.IO.File.Exists(LocalPath.AbsoluteProblemsDirectory + dirName + ".zip"))
            {
                System.IO.File.Delete(LocalPath.AbsoluteProblemsDirectory + dirName + ".zip");
            }
            ZipFile.CreateFromDirectory(
                LocalPath.AbsoluteProblemsDirectory + dirName,
                LocalPath.AbsoluteProblemsDirectory + dirName + ".zip");

            // Delete description, inputFormat and outputFormat from archive
            using (ZipArchive archive = ZipFile.Open(
                       LocalPath.AbsoluteProblemsDirectory + dirName + ".zip", ZipArchiveMode.Update))
            {
                List <ZipArchiveEntry> entriesForDelete = new List <ZipArchiveEntry>();
                foreach (ZipArchiveEntry entry in archive.Entries)
                {
                    if (entry.Name == "Description" ||
                        entry.Name == "InputFormat" ||
                        entry.Name == "OutputFormat")
                    {
                        entriesForDelete.Add(entry);
                    }
                }

                entriesForDelete.Each(e => e.Delete());
            }

            // Move tester zip
            if (System.IO.File.Exists(LocalPath.AbsoluteProblemsDirectory + dirName + ".zip"))
            {
                System.IO.File.Move(LocalPath.AbsoluteProblemsDirectory + dirName + ".zip",
                                    LocalPath.AbsoluteProblemsDirectory + dirName + "/" + dirName + ".zip");
            }

            if (Model.SamplesDropDownID == "other")
            {
                if (Directory.Exists(LocalPath.AbsoluteProblemsDirectory + dirName + "/Samples"))
                {
                    Directory.Delete(LocalPath.AbsoluteProblemsDirectory + dirName + "/Samples", true);
                }
                SaveSamples(Model.Samples, dirName);
            }
        }