[6130] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Web;
|
---|
| 5 | using System.Web.Mvc;
|
---|
| 6 | using HLWebOKBAdminPlugin.Models;
|
---|
| 7 | using HLWebOKBAdminPlugin.OKBAdministrationService;
|
---|
[6246] | 8 | using System.IO;
|
---|
[6130] | 9 |
|
---|
[6317] | 10 | namespace HLWebOKBAdminPlugin.Controllers {
|
---|
| 11 | /// <summary>
|
---|
| 12 | /// Controller for Problem Submenu
|
---|
| 13 | /// </summary>
|
---|
| 14 | public class ProblemController : Controller {
|
---|
[6130] | 15 | /// <summary>
|
---|
[6317] | 16 | /// Controller for Index View
|
---|
[6130] | 17 | /// </summary>
|
---|
[6317] | 18 | /// <returns></returns>
|
---|
| 19 | public ActionResult Index() {
|
---|
| 20 | Session["SelectedSubMenu"] = "Problem";
|
---|
| 21 | ProblemModel pm = new ProblemModel();
|
---|
| 22 | return View(pm);
|
---|
| 23 | }
|
---|
[6130] | 24 |
|
---|
[6317] | 25 | /// <summary>
|
---|
| 26 | /// Controller for Index View
|
---|
| 27 | /// </summary>
|
---|
| 28 | /// <returns></returns>
|
---|
| 29 | public ActionResult SortAsc() {
|
---|
| 30 | Session["SelectedSubMenu"] = "Problem";
|
---|
| 31 | ProblemModel pm = new ProblemModel();
|
---|
| 32 | pm.Problems = pm.Problems.OrderBy(x => x.Name).ToList<Problem>();
|
---|
| 33 | return View("Index", pm);
|
---|
| 34 | }
|
---|
[6153] | 35 |
|
---|
[6317] | 36 | /// <summary>
|
---|
| 37 | /// Controller for Index View
|
---|
| 38 | /// </summary>
|
---|
| 39 | /// <returns></returns>
|
---|
| 40 | public ActionResult SortDesc() {
|
---|
| 41 | Session["SelectedSubMenu"] = "Problem";
|
---|
| 42 | ProblemModel pm = new ProblemModel();
|
---|
[6213] | 43 |
|
---|
[6317] | 44 | //this is code for testing sortorder
|
---|
| 45 | IOrderedEnumerable<Problem> test = pm.Problems.OrderByDescending(x => x.Name);
|
---|
| 46 | IList<Problem> test2 = new List<Problem>();
|
---|
| 47 | foreach (var item in test) {
|
---|
| 48 | test2.Add(item);
|
---|
| 49 | }
|
---|
| 50 | pm.Problems = test2;
|
---|
[6213] | 51 |
|
---|
[6317] | 52 | // this should be the right code
|
---|
| 53 | //pm.Problems = pm.Problems.OrderByDescending(x => x.Name).ToArray<Problem>();
|
---|
| 54 | return View("Index", pm);
|
---|
| 55 | }
|
---|
| 56 | /// <summary>
|
---|
| 57 | /// Controller for Detail View
|
---|
| 58 | /// </summary>
|
---|
| 59 | /// <param name="p"></param>
|
---|
| 60 | /// <returns></returns>
|
---|
| 61 | public ActionResult Detail(long? id) {
|
---|
| 62 | Session["SelectedSubMenu"] = "Problem";
|
---|
| 63 | // We use here the ProblemMode, but I think
|
---|
| 64 | // we can also use the Problem class. (?)
|
---|
| 65 | ProblemModel pm = new ProblemModel();
|
---|
| 66 | if (id == null)
|
---|
| 67 | pm.Problem = new Problem();
|
---|
| 68 | else
|
---|
| 69 | pm.Problem = (Problem)pm.Problems.Where(x => x.Id.Equals((long)id)).FirstOrDefault();
|
---|
| 70 | return View(pm);
|
---|
| 71 | }
|
---|
[6130] | 72 |
|
---|
[6317] | 73 | /// <summary>
|
---|
| 74 | /// Controller for Index View
|
---|
| 75 | /// </summary>
|
---|
| 76 | /// <returns></returns>
|
---|
| 77 | public ActionResult Delete(long id) {
|
---|
| 78 | Session["SelectedSubMenu"] = "Problem";
|
---|
| 79 | // We use here the ProblemMode, but I think
|
---|
| 80 | // we can also use the Problem class. (?)
|
---|
| 81 | ProblemModel pm = new ProblemModel();
|
---|
| 82 | if (id != 0) {
|
---|
| 83 | pm.DeleteProblem(id);
|
---|
| 84 | }
|
---|
| 85 | return View("Index", pm);
|
---|
| 86 | }
|
---|
[6153] | 87 |
|
---|
[6317] | 88 | public ActionResult UploadFile(FormCollection collection) {
|
---|
| 89 | Session["SelectedSubMenu"] = "Problem";
|
---|
[6246] | 90 |
|
---|
[6317] | 91 | long problemId = long.Parse(collection.Get("ProblemId"));
|
---|
[6246] | 92 |
|
---|
[6317] | 93 | ProblemModel pm = new ProblemModel();
|
---|
[6246] | 94 |
|
---|
[6317] | 95 | foreach (string inputTagName in Request.Files) {
|
---|
| 96 | HttpPostedFileBase file = Request.Files[inputTagName];
|
---|
| 97 | if (file.ContentLength > 0) {
|
---|
| 98 | // maybe for storage on server
|
---|
| 99 | //string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), Path.GetFileName(file.FileName));
|
---|
| 100 | if (file.InputStream.CanRead) {
|
---|
| 101 | //file length
|
---|
| 102 | int fileLength = file.ContentLength;
|
---|
[6246] | 103 |
|
---|
[6317] | 104 | //bytearray
|
---|
| 105 | byte[] data = new byte[fileLength];
|
---|
[6246] | 106 |
|
---|
[6317] | 107 | //initialize input stream
|
---|
| 108 | Stream dataStream = file.InputStream;
|
---|
[6246] | 109 |
|
---|
[6317] | 110 | //read file into byte array
|
---|
| 111 | dataStream.Read(data, 0, fileLength);
|
---|
[6246] | 112 |
|
---|
[6317] | 113 | //save data
|
---|
| 114 | pm.UpdateProblemData(problemId, data);
|
---|
| 115 | }//if
|
---|
| 116 | }
|
---|
| 117 | }//foreach
|
---|
[6246] | 118 |
|
---|
[6317] | 119 | return View("Index", pm);
|
---|
| 120 | }//UploadFile
|
---|
[6142] | 121 |
|
---|
[6317] | 122 | /// <summary>
|
---|
| 123 | /// Controller for Detail View
|
---|
| 124 | /// </summary>
|
---|
| 125 | /// <returns></returns>
|
---|
| 126 | public ActionResult SaveChanges(FormCollection collection) {
|
---|
| 127 | long problemId = long.Parse(collection.Get("ProblemId"));
|
---|
| 128 | String problemName = collection.Get("ProblemName");
|
---|
| 129 | String problemDescription = collection.Get("ProblemDescription");
|
---|
| 130 | String problemDataTypeName = collection.Get("ProblemDataTypeName");
|
---|
| 131 | long problemClassId = long.Parse(collection.Get("ProblemClassId"));
|
---|
| 132 | long platformId = long.Parse(collection.Get("PlatformId"));
|
---|
[6142] | 133 |
|
---|
[6317] | 134 | // Later, we will get the runs from the session ...
|
---|
| 135 | ProblemModel pm = new ProblemModel();
|
---|
| 136 | if (problemId != 0)
|
---|
| 137 | pm.Problem = (Problem)pm.Problems.Where(x => x.Id.Equals(problemId)).FirstOrDefault();
|
---|
[6142] | 138 |
|
---|
[6317] | 139 | pm.Problem.Name = problemName;
|
---|
| 140 | pm.Problem.Description = problemDescription;
|
---|
| 141 | pm.Problem.DataTypeName = problemDataTypeName;
|
---|
| 142 | pm.Problem.DataTypeTypeName = "";
|
---|
| 143 | pm.Problem.ProblemClassId = problemClassId;
|
---|
| 144 | pm.Problem.PlatformId = platformId;
|
---|
[6142] | 145 |
|
---|
[6317] | 146 | pm.SaveProblem(pm.Problem);
|
---|
| 147 |
|
---|
| 148 | return View("Index", pm);
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
[6130] | 151 | }
|
---|