using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using HLWebOKBAdminPlugin.Models; using HLWebOKBAdminPlugin.OKBAdministrationService; using System.IO; namespace HLWebOKBAdminPlugin.Controllers { /// /// Controller for Problem Submenu /// public class ProblemController : Controller { /// /// Controller for Index View /// /// public ActionResult Index() { Session["SelectedSubMenu"] = "Problem"; ProblemModel pm = new ProblemModel(); return View(pm); } /// /// Controller for Index View /// /// public ActionResult SortAsc() { Session["SelectedSubMenu"] = "Problem"; ProblemModel pm = new ProblemModel(); pm.Problems = pm.Problems.OrderBy(x => x.Name).ToList(); return View("Index", pm); } /// /// Controller for Index View /// /// public ActionResult SortDesc() { Session["SelectedSubMenu"] = "Problem"; ProblemModel pm = new ProblemModel(); //this is code for testing sortorder IOrderedEnumerable test = pm.Problems.OrderByDescending(x => x.Name); IList test2 = new List(); foreach (var item in test) { test2.Add(item); } pm.Problems = test2; // this should be the right code //pm.Problems = pm.Problems.OrderByDescending(x => x.Name).ToArray(); return View("Index", pm); } /// /// Controller for Detail View /// /// /// public ActionResult Detail(long? id) { Session["SelectedSubMenu"] = "Problem"; // We use here the ProblemMode, but I think // we can also use the Problem class. (?) ProblemModel pm = new ProblemModel(); if (id == null) pm.Problem = new Problem(); else pm.Problem = (Problem)pm.Problems.Where(x => x.Id.Equals((long)id)).FirstOrDefault(); return View(pm); } /// /// Controller for Index View /// /// public ActionResult Delete(long id) { Session["SelectedSubMenu"] = "Problem"; // We use here the ProblemMode, but I think // we can also use the Problem class. (?) ProblemModel pm = new ProblemModel(); if (id != 0) { pm.DeleteProblem(id); } return View("Index", pm); } public ActionResult UploadFile(FormCollection collection) { Session["SelectedSubMenu"] = "Problem"; long problemId = long.Parse(collection.Get("ProblemId")); ProblemModel pm = new ProblemModel(); foreach (string inputTagName in Request.Files) { HttpPostedFileBase file = Request.Files[inputTagName]; if (file.ContentLength > 0) { // maybe for storage on server //string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), Path.GetFileName(file.FileName)); if (file.InputStream.CanRead) { //file length int fileLength = file.ContentLength; //bytearray byte[] data = new byte[fileLength]; //initialize input stream Stream dataStream = file.InputStream; //read file into byte array dataStream.Read(data, 0, fileLength); //save data pm.UpdateProblemData(problemId, data); }//if } }//foreach return View("Index", pm); }//UploadFile /// /// Controller for Detail View /// /// public ActionResult SaveChanges(FormCollection collection) { long problemId = long.Parse(collection.Get("ProblemId")); String problemName = collection.Get("ProblemName"); String problemDescription = collection.Get("ProblemDescription"); String problemDataTypeName = collection.Get("ProblemDataTypeName"); long problemClassId = long.Parse(collection.Get("ProblemClassId")); long platformId = long.Parse(collection.Get("PlatformId")); // Later, we will get the runs from the session ... ProblemModel pm = new ProblemModel(); if (problemId != 0) pm.Problem = (Problem)pm.Problems.Where(x => x.Id.Equals(problemId)).FirstOrDefault(); pm.Problem.Name = problemName; pm.Problem.Description = problemDescription; pm.Problem.DataTypeName = problemDataTypeName; pm.Problem.DataTypeTypeName = ""; pm.Problem.ProblemClassId = problemClassId; pm.Problem.PlatformId = platformId; pm.SaveProblem(pm.Problem); return View("Index", pm); } } }