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 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(); foreach (var item in labExerciseUserAttemptData.Children()) { var itemProperties = item.Children(); //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(); 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(); //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(); //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(), true); // return Request.CreateResponse(HttpStatusCode.OK); bool isEMailSent = AIAHTML5.API.Models.UserUtility.SendEmail(User, aar, User["EmailAdd"].ToString(), User["reportTitle"].ToString(), User["reportImage"].ToString(), true); // return Request.CreateResponse(HttpStatusCode.OK,); return new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(isEMailSent.ToString()) }; } [Route("api/LabExercise/GetParameter")] [HttpGet] public IEnumerable 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) { } } }