diff --git a/150-DOCUMENTATION/002-DBScripts/CreateLabExerciseTempTableType.sql b/150-DOCUMENTATION/002-DBScripts/CreateLabExerciseTempTableType.sql new file mode 100644 index 0000000..7017505 --- /dev/null +++ b/150-DOCUMENTATION/002-DBScripts/CreateLabExerciseTempTableType.sql @@ -0,0 +1,9 @@ +CREATE TYPE LabExerciseTempTableType AS TABLE +( MaxScore int, + UserAnswers NVarchar(2000), + QuestionNo int, + CorrectAnswers NVarchar(2000), + DragItems NVarchar(2000), + Score int +) +; \ No newline at end of file diff --git a/150-DOCUMENTATION/002-DBScripts/usp_SaveLabExerciseAttempts.sql b/150-DOCUMENTATION/002-DBScripts/usp_SaveLabExerciseAttempts.sql new file mode 100644 index 0000000..275ccde --- /dev/null +++ b/150-DOCUMENTATION/002-DBScripts/usp_SaveLabExerciseAttempts.sql @@ -0,0 +1,97 @@ +USE [AIADatabaseV5] +GO +/****** Object: StoredProcedure [dbo].[usp_SaveLabExerciseAttempts] Script Date: 2/23/2018 7:07:38 PM ******/ +SET ANSI_NULLS ON +GO +SET QUOTED_IDENTIFIER ON +GO + +-- ==================================================== +-- Author: Nikita Kulshreshtha +-- Create date: 21-Feb-2018 +-- Description: To insert a LabExerciseData +-- ==================================================== +CREATE PROCEDURE [dbo].[usp_SaveLabExerciseAttempts] + -- Add the parameters for the stored procedure here + + @UserId int, + @LabExerciseIdentifier nchar(10), + @LastQuestion int, + @TotalQuestions int, + @LabExerciseTempTbl LabExerciseTempTableType READONLY + + +AS +BEGIN + -- SET NOCOUNT ON added to prevent extra result sets from + -- interfering with SELECT statements. + SET NOCOUNT ON; + + BEGIN TRY + BEGIN TRANSACTION + DECLARE @labQuizId int; + DECLARE @count int; + DECLARE @Score int; + DECLARE @MaxScore int; + DECLARE @UserAnswers nvarchar(2000); + DECLARE @QuestionNo int; + DECLARE @CorrectAnswers nvarchar(2000); + DECLARE @DragItems nvarchar(max) + DECLARE @c int + + INSERT INTO LabExercise(UserId, LabExerciseIdentifier,IsActive, LastQuestion,TotalQuestions,SavedDate, ModifiedDate) + values(@UserId, @LabExerciseIdentifier, 1, @LastQuestion, @TotalQuestions,GETDATE(),GETDATE()) + print 'inserted in 1st table' + SELECT @labQuizId = max(Id) from LabExercise where LabExerciseIdentifier = @LabExerciseIdentifier and UserId= @UserId + print @labQuizId +-- --check count before update +SELECT @count = count(*) FROM LabExercise where LabExerciseIdentifier = @LabExerciseIdentifier and UserId= @UserId and Id!=@labQuizId +print @count + if(@count>0) + BEGIN + print N'inside if' + UPDATE LabExercise set IsActive = 0 where LabExerciseIdentifier = @LabExerciseIdentifier and UserId= @UserId and Id!=@labQuizId + +----create temp table +create table #Temp +( + MaxScore int, + UserAnswers NVarchar(2000), + QuestionNo int, + CorrectAnswers NVarchar(2000), + DragItems NVarchar(2000), + Score int +) +select * from @LabExerciseTempTbl +insert into #Temp(MaxScore,UserAnswers,QuestionNo,CorrectAnswers,DragItems,Score) +select MaxScore,UserAnswers,QuestionNo,CorrectAnswers,DragItems,Score from @LabExerciseTempTbl +select @c = count(*) from #Temp +print @c +print 'inserted in temp table' + DECLARE LABEX_CURSOR CURSOR + LOCAL FORWARD_ONLY FOR + SELECT MaxScore, UserAnswers,QuestionNo, CorrectAnswers, DragItems,Score FROM #Temp + print 'cursor declared' + OPEN LABEX_CURSOR + print 'cursor open' + FETCH NEXT FROM LABEX_CURSOR INTO @MaxScore, @UserAnswers, @QuestionNo, @CorrectAnswers, @DragItems,@Score + WHILE @@FETCH_STATUS = 0 + BEGIN + print 'inside cursor' + INSERT INTO LabExerciseDetails(LabQuizId,StateObject,Score,MaxScore,UserAnswers,QuestionNo,CorrectAnswers,DragItems) values(@labQuizId,null,@Score,@MaxScore,@UserAnswers,@QuestionNo,@CorrectAnswers,@DragItems) + FETCH NEXT FROM LABEX_CURSOR INTO @MaxScore, @UserAnswers, @QuestionNo, @CorrectAnswers, @DragItems,@Score + print 'end cursor' + END + CLOSE LABEX_CURSOR + DEALLOCATE LABEX_CURSOR + END + COMMIT TRANSACTION + + END TRY + BEGIN CATCH + IF @@TRANCOUNT > 0 + ROLLBACK TRANSACTION + END CATCH + +END + diff --git a/400-SOURCECODE/AIAHTML5.API/Models/DBModel.cs b/400-SOURCECODE/AIAHTML5.API/Models/DBModel.cs index 6226910..7c48a86 100644 --- a/400-SOURCECODE/AIAHTML5.API/Models/DBModel.cs +++ b/400-SOURCECODE/AIAHTML5.API/Models/DBModel.cs @@ -902,62 +902,77 @@ namespace AIAHTML5.API.Models { - logger.Debug(" Inside GetUserDetailsByEmailId for userId = " + userId + "and labIdentifier= " + labIdentifier); + logger.Debug(" Inside GetLabExercise for userId = " + userId + "and labIdentifier= " + labIdentifier); + SqlConnection conn = null; + try + { + LabExercise le = new LabExercise(); + le.labExercise = new List(); + DBModel objModel = new DBModel(); - LabExercise le = new LabExercise(); - le.labExercise = new List(); - DBModel objModel = new DBModel(); + conn = new SqlConnection(dbConnectionString); + SqlCommand cmd = new SqlCommand(); + SqlDataAdapter adapter; + SqlParameter param; + DataSet ds = new DataSet(); - SqlConnection conn = new SqlConnection(dbConnectionString); - SqlCommand cmd = new SqlCommand(); - SqlDataAdapter adapter; - SqlParameter param; - DataSet ds = new DataSet(); + cmd.Connection = conn; + cmd.CommandText = DBConstants.GET_LAB_EXERCISE; + cmd.CommandType = CommandType.StoredProcedure; - cmd.Connection = conn; - cmd.CommandText = DBConstants.GET_LAB_EXERCISE; - cmd.CommandType = CommandType.StoredProcedure; - - //cmd.Parameters.AddWithValue("@UserId", userId); - //cmd.Parameters.AddWithValue("@Identifier", labIdentifier); - cmd.Parameters.Add("@UserId", SqlDbType.Int).Value = userId; - cmd.Parameters.Add("@Identifier", SqlDbType.NVarChar).Value = labIdentifier; + //cmd.Parameters.AddWithValue("@UserId", userId); + //cmd.Parameters.AddWithValue("@Identifier", labIdentifier); + cmd.Parameters.Add("@UserId", SqlDbType.Int).Value = userId; + cmd.Parameters.Add("@Identifier", SqlDbType.NVarChar).Value = labIdentifier; - adapter = new SqlDataAdapter(cmd); - adapter.Fill(ds); + adapter = new SqlDataAdapter(cmd); + adapter.Fill(ds); + logger.Debug("dataset is filled"); - if (ds != null && ds.Tables.Count > 0) - { - DataTable dt = ds.Tables[0]; - - if (dt.Rows.Count > 0) + if (ds != null && ds.Tables.Count > 0) { - foreach (DataRow dr in dt.Rows) + logger.Debug("ds.Tables.Count= " + ds.Tables.Count); + DataTable dt = ds.Tables[0]; + + if (dt.Rows.Count > 0) { - le.userId = Convert.ToInt32(dr["UserId"]); - le.labExerciseIdentifier = dr["labExerciseIdentifier"].ToString(); - - le.lastQuestion = Convert.ToInt32(dr["LastQuestion"]); - le.totalQuestions = Convert.ToInt32(dr["TotalQuestions"]); - - LabEcerciseDetails led = new LabEcerciseDetails(); - // led.StateObject = dr["StateObject"].ToString(); - led.UserAnswers = dr["UserAnswers"].ToString(); - led.Score = Convert.ToInt32(dr["Score"]); - led.MaxScore = Convert.ToInt32(dr["MaxScore"]); - led.QuestionNo = Convert.ToInt32(dr["QuestionNo"]); - led.CorrectAnswers = dr["CorrectAnswers"].ToString(); - led.DragItems = dr["DragItems"].ToString(); - le.labExercise.Add(led); + logger.Debug("dt.Rows.Count= " + dt.Rows.Count); + foreach (DataRow dr in dt.Rows) + { + le.userId = Convert.ToInt32(dr["UserId"]); + le.labExerciseIdentifier = dr["labExerciseIdentifier"].ToString(); + + le.lastQuestion = Convert.ToInt32(dr["LastQuestion"]); + le.totalQuestions = Convert.ToInt32(dr["TotalQuestions"]); + + LabEcerciseDetails led = new LabEcerciseDetails(); + // led.StateObject = dr["StateObject"].ToString(); + led.UserAnswers = dr["UserAnswers"].ToString(); + led.Score = Convert.ToInt32(dr["Score"]); + led.MaxScore = Convert.ToInt32(dr["MaxScore"]); + led.QuestionNo = Convert.ToInt32(dr["QuestionNo"]); + led.CorrectAnswers = dr["CorrectAnswers"].ToString(); + led.DragItems = dr["DragItems"].ToString(); + le.labExercise.Add(led); + } } } - } - return le; + return le; + } - + catch (SqlException ex) + { + logger.Fatal("Exception in GetLabExercise for userId= " + userId + ", labExerciseIdentifier= " + labIdentifier); + throw; + } + finally + { + conn.Dispose(); + } + } //internal int InsertLabExerciseDetails(int userId, string labExerciseIdentifier, int lastQusetion, int totalQuestions, List<> labExDetails )