Commit 8380e2e59dacef7667d8af92c9a0603941c348e6

Authored by Nikita Kulshreshtha
1 parent 788cc440

added sql scripts

added logs
150-DOCUMENTATION/002-DBScripts/CreateLabExerciseTempTableType.sql 0 → 100644
  1 +CREATE TYPE LabExerciseTempTableType AS TABLE
  2 +( MaxScore int,
  3 + UserAnswers NVarchar(2000),
  4 + QuestionNo int,
  5 + CorrectAnswers NVarchar(2000),
  6 + DragItems NVarchar(2000),
  7 + Score int
  8 +)
  9 +;
0 10 \ No newline at end of file
... ...
150-DOCUMENTATION/002-DBScripts/usp_SaveLabExerciseAttempts.sql 0 → 100644
  1 +USE [AIADatabaseV5]
  2 +GO
  3 +/****** Object: StoredProcedure [dbo].[usp_SaveLabExerciseAttempts] Script Date: 2/23/2018 7:07:38 PM ******/
  4 +SET ANSI_NULLS ON
  5 +GO
  6 +SET QUOTED_IDENTIFIER ON
  7 +GO
  8 +
  9 +-- ====================================================
  10 +-- Author: Nikita Kulshreshtha
  11 +-- Create date: 21-Feb-2018
  12 +-- Description: To insert a LabExerciseData
  13 +-- ====================================================
  14 +CREATE PROCEDURE [dbo].[usp_SaveLabExerciseAttempts]
  15 + -- Add the parameters for the stored procedure here
  16 +
  17 + @UserId int,
  18 + @LabExerciseIdentifier nchar(10),
  19 + @LastQuestion int,
  20 + @TotalQuestions int,
  21 + @LabExerciseTempTbl LabExerciseTempTableType READONLY
  22 +
  23 +
  24 +AS
  25 +BEGIN
  26 + -- SET NOCOUNT ON added to prevent extra result sets from
  27 + -- interfering with SELECT statements.
  28 + SET NOCOUNT ON;
  29 +
  30 + BEGIN TRY
  31 + BEGIN TRANSACTION
  32 + DECLARE @labQuizId int;
  33 + DECLARE @count int;
  34 + DECLARE @Score int;
  35 + DECLARE @MaxScore int;
  36 + DECLARE @UserAnswers nvarchar(2000);
  37 + DECLARE @QuestionNo int;
  38 + DECLARE @CorrectAnswers nvarchar(2000);
  39 + DECLARE @DragItems nvarchar(max)
  40 + DECLARE @c int
  41 +
  42 + INSERT INTO LabExercise(UserId, LabExerciseIdentifier,IsActive, LastQuestion,TotalQuestions,SavedDate, ModifiedDate)
  43 + values(@UserId, @LabExerciseIdentifier, 1, @LastQuestion, @TotalQuestions,GETDATE(),GETDATE())
  44 + print 'inserted in 1st table'
  45 + SELECT @labQuizId = max(Id) from LabExercise where LabExerciseIdentifier = @LabExerciseIdentifier and UserId= @UserId
  46 + print @labQuizId
  47 +-- --check count before update
  48 +SELECT @count = count(*) FROM LabExercise where LabExerciseIdentifier = @LabExerciseIdentifier and UserId= @UserId and Id!=@labQuizId
  49 +print @count
  50 + if(@count>0)
  51 + BEGIN
  52 + print N'inside if'
  53 + UPDATE LabExercise set IsActive = 0 where LabExerciseIdentifier = @LabExerciseIdentifier and UserId= @UserId and Id!=@labQuizId
  54 +
  55 +----create temp table
  56 +create table #Temp
  57 +(
  58 + MaxScore int,
  59 + UserAnswers NVarchar(2000),
  60 + QuestionNo int,
  61 + CorrectAnswers NVarchar(2000),
  62 + DragItems NVarchar(2000),
  63 + Score int
  64 +)
  65 +select * from @LabExerciseTempTbl
  66 +insert into #Temp(MaxScore,UserAnswers,QuestionNo,CorrectAnswers,DragItems,Score)
  67 +select MaxScore,UserAnswers,QuestionNo,CorrectAnswers,DragItems,Score from @LabExerciseTempTbl
  68 +select @c = count(*) from #Temp
  69 +print @c
  70 +print 'inserted in temp table'
  71 + DECLARE LABEX_CURSOR CURSOR
  72 + LOCAL FORWARD_ONLY FOR
  73 + SELECT MaxScore, UserAnswers,QuestionNo, CorrectAnswers, DragItems,Score FROM #Temp
  74 + print 'cursor declared'
  75 + OPEN LABEX_CURSOR
  76 + print 'cursor open'
  77 + FETCH NEXT FROM LABEX_CURSOR INTO @MaxScore, @UserAnswers, @QuestionNo, @CorrectAnswers, @DragItems,@Score
  78 + WHILE @@FETCH_STATUS = 0
  79 + BEGIN
  80 + print 'inside cursor'
  81 + INSERT INTO LabExerciseDetails(LabQuizId,StateObject,Score,MaxScore,UserAnswers,QuestionNo,CorrectAnswers,DragItems) values(@labQuizId,null,@Score,@MaxScore,@UserAnswers,@QuestionNo,@CorrectAnswers,@DragItems)
  82 + FETCH NEXT FROM LABEX_CURSOR INTO @MaxScore, @UserAnswers, @QuestionNo, @CorrectAnswers, @DragItems,@Score
  83 + print 'end cursor'
  84 + END
  85 + CLOSE LABEX_CURSOR
  86 + DEALLOCATE LABEX_CURSOR
  87 + END
  88 + COMMIT TRANSACTION
  89 +
  90 + END TRY
  91 + BEGIN CATCH
  92 + IF @@TRANCOUNT > 0
  93 + ROLLBACK TRANSACTION
  94 + END CATCH
  95 +
  96 +END
  97 +
... ...
400-SOURCECODE/AIAHTML5.API/Models/DBModel.cs
... ... @@ -902,62 +902,77 @@ namespace AIAHTML5.API.Models
902 902 {
903 903  
904 904  
905   - logger.Debug(" Inside GetUserDetailsByEmailId for userId = " + userId + "and labIdentifier= " + labIdentifier);
  905 + logger.Debug(" Inside GetLabExercise for userId = " + userId + "and labIdentifier= " + labIdentifier);
  906 + SqlConnection conn = null;
  907 + try
  908 + {
  909 + LabExercise le = new LabExercise();
  910 + le.labExercise = new List<LabEcerciseDetails>();
  911 + DBModel objModel = new DBModel();
906 912  
907   - LabExercise le = new LabExercise();
908   - le.labExercise = new List<LabEcerciseDetails>();
909   - DBModel objModel = new DBModel();
  913 + conn = new SqlConnection(dbConnectionString);
  914 + SqlCommand cmd = new SqlCommand();
  915 + SqlDataAdapter adapter;
  916 + SqlParameter param;
  917 + DataSet ds = new DataSet();
910 918  
911   - SqlConnection conn = new SqlConnection(dbConnectionString);
912   - SqlCommand cmd = new SqlCommand();
913   - SqlDataAdapter adapter;
914   - SqlParameter param;
915   - DataSet ds = new DataSet();
  919 + cmd.Connection = conn;
  920 + cmd.CommandText = DBConstants.GET_LAB_EXERCISE;
  921 + cmd.CommandType = CommandType.StoredProcedure;
916 922  
917   - cmd.Connection = conn;
918   - cmd.CommandText = DBConstants.GET_LAB_EXERCISE;
919   - cmd.CommandType = CommandType.StoredProcedure;
920   -
921   - //cmd.Parameters.AddWithValue("@UserId", userId);
922   - //cmd.Parameters.AddWithValue("@Identifier", labIdentifier);
923   - cmd.Parameters.Add("@UserId", SqlDbType.Int).Value = userId;
924   - cmd.Parameters.Add("@Identifier", SqlDbType.NVarChar).Value = labIdentifier;
  923 + //cmd.Parameters.AddWithValue("@UserId", userId);
  924 + //cmd.Parameters.AddWithValue("@Identifier", labIdentifier);
  925 + cmd.Parameters.Add("@UserId", SqlDbType.Int).Value = userId;
  926 + cmd.Parameters.Add("@Identifier", SqlDbType.NVarChar).Value = labIdentifier;
925 927  
926 928  
927   - adapter = new SqlDataAdapter(cmd);
928   - adapter.Fill(ds);
  929 + adapter = new SqlDataAdapter(cmd);
  930 + adapter.Fill(ds);
929 931  
  932 + logger.Debug("dataset is filled");
930 933  
931   - if (ds != null && ds.Tables.Count > 0)
932   - {
933   - DataTable dt = ds.Tables[0];
934   -
935   - if (dt.Rows.Count > 0)
  934 + if (ds != null && ds.Tables.Count > 0)
936 935 {
937   - foreach (DataRow dr in dt.Rows)
  936 + logger.Debug("ds.Tables.Count= " + ds.Tables.Count);
  937 + DataTable dt = ds.Tables[0];
  938 +
  939 + if (dt.Rows.Count > 0)
938 940 {
939   - le.userId = Convert.ToInt32(dr["UserId"]);
940   - le.labExerciseIdentifier = dr["labExerciseIdentifier"].ToString();
941   -
942   - le.lastQuestion = Convert.ToInt32(dr["LastQuestion"]);
943   - le.totalQuestions = Convert.ToInt32(dr["TotalQuestions"]);
944   -
945   - LabEcerciseDetails led = new LabEcerciseDetails();
946   - // led.StateObject = dr["StateObject"].ToString();
947   - led.UserAnswers = dr["UserAnswers"].ToString();
948   - led.Score = Convert.ToInt32(dr["Score"]);
949   - led.MaxScore = Convert.ToInt32(dr["MaxScore"]);
950   - led.QuestionNo = Convert.ToInt32(dr["QuestionNo"]);
951   - led.CorrectAnswers = dr["CorrectAnswers"].ToString();
952   - led.DragItems = dr["DragItems"].ToString();
953   - le.labExercise.Add(led);
  941 + logger.Debug("dt.Rows.Count= " + dt.Rows.Count);
  942 + foreach (DataRow dr in dt.Rows)
  943 + {
  944 + le.userId = Convert.ToInt32(dr["UserId"]);
  945 + le.labExerciseIdentifier = dr["labExerciseIdentifier"].ToString();
  946 +
  947 + le.lastQuestion = Convert.ToInt32(dr["LastQuestion"]);
  948 + le.totalQuestions = Convert.ToInt32(dr["TotalQuestions"]);
  949 +
  950 + LabEcerciseDetails led = new LabEcerciseDetails();
  951 + // led.StateObject = dr["StateObject"].ToString();
  952 + led.UserAnswers = dr["UserAnswers"].ToString();
  953 + led.Score = Convert.ToInt32(dr["Score"]);
  954 + led.MaxScore = Convert.ToInt32(dr["MaxScore"]);
  955 + led.QuestionNo = Convert.ToInt32(dr["QuestionNo"]);
  956 + led.CorrectAnswers = dr["CorrectAnswers"].ToString();
  957 + led.DragItems = dr["DragItems"].ToString();
  958 + le.labExercise.Add(led);
  959 + }
954 960 }
955 961 }
956   - }
957 962  
958   - return le;
  963 + return le;
  964 + }
959 965  
960   -
  966 + catch (SqlException ex)
  967 + {
  968 + logger.Fatal("Exception in GetLabExercise for userId= " + userId + ", labExerciseIdentifier= " + labIdentifier);
  969 + throw;
  970 + }
  971 + finally
  972 + {
  973 + conn.Dispose();
  974 + }
  975 +
961 976  
962 977 }
963 978 //internal int InsertLabExerciseDetails(int userId, string labExerciseIdentifier, int lastQusetion, int totalQuestions, List<> labExDetails )
... ...