Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebApplication/MVC2/HLWebOKBAdminPlugin/Controllers/ProblemController.cs @ 11589

Last change on this file since 11589 was 6317, checked in by jwolfing, 13 years ago

#1433 code formatted

File size: 4.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Web;
5using System.Web.Mvc;
6using HLWebOKBAdminPlugin.Models;
7using HLWebOKBAdminPlugin.OKBAdministrationService;
8using System.IO;
9
10namespace HLWebOKBAdminPlugin.Controllers {
11  /// <summary>
12  /// Controller for Problem Submenu
13  /// </summary>
14  public class ProblemController : Controller {
15    /// <summary>
16    /// Controller for Index View
17    /// </summary>
18    /// <returns></returns>
19    public ActionResult Index() {
20      Session["SelectedSubMenu"] = "Problem";
21      ProblemModel pm = new ProblemModel();
22      return View(pm);
23    }
24
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    }
35
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();
43
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;
51
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    }
72
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    }
87
88    public ActionResult UploadFile(FormCollection collection) {
89      Session["SelectedSubMenu"] = "Problem";
90
91      long problemId = long.Parse(collection.Get("ProblemId"));
92
93      ProblemModel pm = new ProblemModel();
94
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;
103
104            //bytearray
105            byte[] data = new byte[fileLength];
106
107            //initialize input stream
108            Stream dataStream = file.InputStream;
109
110            //read file into byte array
111            dataStream.Read(data, 0, fileLength);
112
113            //save data
114            pm.UpdateProblemData(problemId, data);
115          }//if
116        }
117      }//foreach
118
119      return View("Index", pm);
120    }//UploadFile
121
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"));
133
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();
138
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;
145
146      pm.SaveProblem(pm.Problem);
147
148      return View("Index", pm);
149    }
150  }
151}
Note: See TracBrowser for help on using the repository browser.