LabExerciseController.cs
13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using log4net;
using AIAHTML5.API.Constants;
using AIAHTML5.API.Models;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using System.Reflection;
namespace AIAHTML5.API.Controllers
{
public class LabExerciseController : ApiController
{
// GET api/LabExercise
//public IEnumerable<string> Get()
//{
// return new string[] { "value1", "value2" };
//}
// GET api/LabExercise/5
public HttpResponseMessage Get([FromUri] int userId, string labExerciseIdentifier)
{
ILog logger = log4net.LogManager.GetLogger((System.Reflection.MethodBase.GetCurrentMethod().DeclaringType));
logger.Debug("inside Get LabExerciseController");
try
{
DBModel db = new DBModel();
LabExercise labEx = db.GetLabExercise(userId, labExerciseIdentifier);
if (labEx!=null)
return new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonConvert.SerializeObject(labEx)) };
else
return new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(AIAConstants.SAVED_LAB_EXERCISE_NOT_FOUND) };
}
catch (SqlException ex)
{
logger.Fatal("SqlException occured for UserId =" + userId.ToString() + " and LabExerciseIdentifier= " + labExerciseIdentifier + "Exception= " + ex.Message + ", STACKTRACE: " + ex.StackTrace);
ArrayList supportMailList = UserUtility.GetSupportMailList();
string mailSubject = AIAConstants.SQL_CONNECTION_ERROR_MAIL_SUBJECT;
string mailBody = "MESSAGE: " + ex.Message + ", STACKTRACE: " + ex.StackTrace;
UserUtility.SendEmailForException(userId, supportMailList, "", mailSubject, mailBody);
return new HttpResponseMessage { StatusCode = HttpStatusCode.InternalServerError, Content = new StringContent(AIAConstants.SQL_CONNECTION_ERROR) };
}
catch (Exception e)
{
logger.Fatal("SqlException occured for UserId =" + userId.ToString() + " and LabExerciseIdentifier= " + labExerciseIdentifier + "Exception= " + e.Message + ", STACKTRACE: " + e.StackTrace);
ArrayList supportMailList = UserUtility.GetSupportMailList();
string mailSubject = AIAConstants.SQL_CONNECTION_ERROR_MAIL_SUBJECT;
string mailBody = "MESSAGE: " + e.Message + ", STACKTRACE: " + e.StackTrace;
UserUtility.SendEmailForException(userId, supportMailList, "", mailSubject, mailBody);
return new HttpResponseMessage { StatusCode = HttpStatusCode.InternalServerError, Content = new StringContent(AIAConstants.SQL_CONNECTION_ERROR) };
}
}
// POST api/LabExercise
public HttpResponseMessage Post([FromBody]JArray labExerciseUserAttemptData)
{
ILog logger = log4net.LogManager.GetLogger((System.Reflection.MethodBase.GetCurrentMethod().DeclaringType));
logger.Debug("inside insertLabExerciseAttempt Post LabExerciseController");
try
{
LabExercise le = new LabExercise();
le.labExercise = new List<LabEcerciseDetails>();
foreach (var item in labExerciseUserAttemptData.Children())
{
var itemProperties = item.Children<JProperty>();
//you could do a foreach or a linq here depending on what you need to do exactly with the value
var userId = itemProperties.FirstOrDefault(x => x.Name == "userId");
le.userId = Convert.ToInt32(userId.Value); ////This is a JValue type
var labExerciseIdentifier = itemProperties.FirstOrDefault(x => x.Name == "labExerciseIdentifier");
le.labExerciseIdentifier = labExerciseIdentifier.Value.ToString(); ////This is a JValue type
var lastQuestion = itemProperties.FirstOrDefault(x => x.Name == "LastQuestion");
le.lastQuestion = Convert.ToInt32(lastQuestion.Value); ////This is a JValue type
var totalQuestions = itemProperties.FirstOrDefault(x => x.Name == "TotalQuestions");
le.totalQuestions = Convert.ToInt32(totalQuestions.Value); ////This is a JValue type
var LabExerciseUserData = itemProperties.FirstOrDefault(x => x.Name == "LabExerciseUserData");
foreach (var labQuiz in LabExerciseUserData.Children())
{
var labQuizProperties = labQuiz.Children();
foreach (var lb in labQuizProperties)
{
var itemProperties1 = lb.Children<JProperty>();
LabEcerciseDetails labExDetails = new LabEcerciseDetails();
var MaxScore = itemProperties1.FirstOrDefault(x => x.Name == "MaxScore");
labExDetails.MaxScore = Convert.ToInt32(MaxScore.Value);
var UserAnswer = itemProperties1.FirstOrDefault(x => x.Name == "UserAnswer");
labExDetails.UserAnswers = UserAnswer.Value.ToString();
var QuestionNo = itemProperties1.FirstOrDefault(x => x.Name == "QuestionNo");
labExDetails.QuestionNo = Convert.ToInt32(QuestionNo.Value);
var CorrectAnswer = itemProperties1.FirstOrDefault(x => x.Name == "CorrectAnswer");
labExDetails.CorrectAnswers = CorrectAnswer.Value.ToString();
var DragItems = itemProperties1.FirstOrDefault(x => x.Name == "DragItems");
labExDetails.DragItems = DragItems.Value.ToString();
var Score = itemProperties1.FirstOrDefault(x => x.Name == "Score");
labExDetails.Score = Convert.ToInt32(Score.Value);
le.labExercise.Add(labExDetails);
}
}
}
DataTable labExerciseDetailsDataTable = new UserUtility().ToDataTable(le.labExercise);
int result = le.insertLabExerciseAttempt(le, labExerciseDetailsDataTable);
if (result == -1)
{
return new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(AIAConstants.LAB_EXERCISE_SAVE_SUCCESS) };
}
else
{
return new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(AIAConstants.LAB_EXERCISE_SAVE_FAILURE) };
}
}
catch (SqlException e)
{
int UserId = 0;
string LabExerciseIdentifier = "";
int TotalQuestions = 0;
int LastQuestion = 0;
foreach (var item in labExerciseUserAttemptData.Children())
{
var itemProperties = item.Children<JProperty>();
//you could do a foreach or a linq here depending on what you need to do exactly with the value
var userId = itemProperties.FirstOrDefault(x => x.Name == "userId");
UserId = Convert.ToInt32(userId.Value); ////This is a JValue type
var labExerciseIdentifier = itemProperties.FirstOrDefault(x => x.Name == "labExerciseIdentifier");
LabExerciseIdentifier = labExerciseIdentifier.Value.ToString(); ////This is a JValue type
var lastQuestion = itemProperties.FirstOrDefault(x => x.Name == "LastQuestion");
LastQuestion = Convert.ToInt32(lastQuestion.Value); ////This is a JValue type
var totalQuestions = itemProperties.FirstOrDefault(x => x.Name == "TotalQuestions");
TotalQuestions = Convert.ToInt32(totalQuestions.Value); ////This is a JValue type
}
logger.Fatal("SqlException occured for UserId =" + UserId.ToString() + " and LabExerciseIdentifier= " + LabExerciseIdentifier+"and LastQuestion= "+LastQuestion.ToString()+ "Exception= " + e.Message + ", STACKTRACE: " + e.StackTrace);
ArrayList supportMailList = UserUtility.GetSupportMailList();
string mailSubject = AIAConstants.SQL_CONNECTION_ERROR_MAIL_SUBJECT;
string mailBody = "MESSAGE: " + e.Message + ", STACKTRACE: " + e.StackTrace;
UserUtility.SendEmailForException(UserId, supportMailList, "", mailSubject, mailBody);
return new HttpResponseMessage { StatusCode = HttpStatusCode.InternalServerError, Content = new StringContent(AIAConstants.SQL_CONNECTION_ERROR) };
}
catch (Exception e)
{
int UserId = 0;
string LabExerciseIdentifier = "";
int TotalQuestions = 0;
int LastQuestion = 0;
foreach (var item in labExerciseUserAttemptData.Children())
{
var itemProperties = item.Children<JProperty>();
//you could do a foreach or a linq here depending on what you need to do exactly with the value
var userId = itemProperties.FirstOrDefault(x => x.Name == "userId");
UserId = Convert.ToInt32(userId.Value); ////This is a JValue type
var labExerciseIdentifier = itemProperties.FirstOrDefault(x => x.Name == "labExerciseIdentifier");
LabExerciseIdentifier = labExerciseIdentifier.Value.ToString(); ////This is a JValue type
var lastQuestion = itemProperties.FirstOrDefault(x => x.Name == "LastQuestion");
LastQuestion = Convert.ToInt32(lastQuestion.Value); ////This is a JValue type
var totalQuestions = itemProperties.FirstOrDefault(x => x.Name == "TotalQuestions");
TotalQuestions = Convert.ToInt32(totalQuestions.Value); ////This is a JValue type
}
logger.Fatal("Exception occured for UserId =" + UserId.ToString() + " and LabExerciseIdentifier= " + LabExerciseIdentifier + "and LastQuestion= " + LastQuestion.ToString() + "Exception= " + e.Message + ", STACKTRACE: " + e.StackTrace);
ArrayList supportMailList = UserUtility.GetSupportMailList();
string mailSubject = AIAConstants.EXCEPTION_IN_AIAHTML5_MAIL_SUBJECT;
string mailBody = "MESSAGE: " + e.Message + ", STACKTRACE: " + e.StackTrace;
UserUtility.SendEmailForException(UserId, supportMailList, "", mailSubject, mailBody);
return new HttpResponseMessage { StatusCode = HttpStatusCode.InternalServerError, Content = new StringContent(AIAConstants.EXCEPTION_OCCURED) };
}
}
[HttpPost]
public HttpResponseMessage Hi(int userId)
{
return Request.CreateResponse(HttpStatusCode.OK, userId.ToString());
}
[Route("GetUserProfile/{userId}")]
[HttpGet]
public IHttpActionResult GetUserProfile(int userId)
{
//dbContext.Configuration.ProxyCreationEnabled = false;
try
{
return Ok("Hello");
}
catch (Exception ex)
{
throw ex;
}
//return ToJson(dbContext.AIAUsers.Where(u => u.Id == userId).AsEnumerable());
}
[Route("api/LabExercise/SendEmail")]
[HttpPost]
public HttpResponseMessage SendEmail([FromBody]JObject User)
{
ArrayList aar = new ArrayList();
if (User["EmailTo"].ToString().ToLower().Contains(';'))
{
string[] words = User["EmailTo"].ToString().Split(';');
foreach (string word in words)
{
Console.WriteLine(word);
aar.Add(word);
}
}
else
{
aar.Add(User["EmailTo"].ToString());
}
if (Convert.ToBoolean(User["ReportCheck"]) == true)
{
aar.Add(User["EmailAdd"].ToString());
}
AIAHTML5.API.Models.UserUtility.SendEmail(User, aar, User["EmailAdd"].ToString(), User["reportTitle"].ToString(), User["reportImage"].ToString());
return Request.CreateResponse(HttpStatusCode.OK);
}
[Route("api/LabExercise/GetParameter")]
[HttpGet]
public IEnumerable<string> GetParameter()
{
return new string[] { "val1", "val2" };
}
// PUT api/LabExercise/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/LabExercise/5
public void Delete(int id)
{
}
}
}