LabExerciseController.cs 5.96 KB
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;
namespace AIAHTML5.API.Controllers
{
    public class LabExerciseController : ApiController
    {
        // GET api/authenticate
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/authenticate/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/authenticate
        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.UserAnswer = 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.CorrectAnswer = 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);

                        }
                    }
                }
                return new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent("insertyed") };

            }
            catch (SqlException e)
            {

                //logger.Fatal("SqlException occured for loginId =" + credentials["username"].ToString() + " and password= " + credentials["password"].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.SendEmail(credentials, supportMailList, "", mailSubject, mailBody);

                return new HttpResponseMessage { StatusCode = HttpStatusCode.InternalServerError, Content = new StringContent(AIAConstants.SQL_CONNECTION_ERROR) };
            }
            catch (Exception e)
            {

                //logger.Fatal("Exception occured for loginId =" + credentials["username"].ToString() + " and password= " + credentials["password"].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.SendEmail(credentials, supportMailList, "", mailSubject, mailBody);

                return new HttpResponseMessage { StatusCode = HttpStatusCode.InternalServerError, Content = new StringContent(AIAConstants.EXCEPTION_OCCURED) };

            }

            

        }

     

      



        // PUT api/authenticate/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/authenticate/5
        public void Delete(int id)
        {
        }
    }
}