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