LabExerciseController.cs
9.03 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
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 string Get(int id)
{
return "value";
}
// 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) };
}
}
// PUT api/LabExercise/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/LabExercise/5
public void Delete(int id)
{
}
}
}