public static Object getStudentActivitiesForId(int id) { String connectionString = DatabaseConnections.getConnectionString("NARAYANA_CLIENT"); List <StudentActivities> studentActivitiesList = null; using (SqlConnection connection = new SqlConnection(connectionString)) { String sqlQuery = "SELECT * FROM " + STUDENT_ACTIVITIES_TABLE + " WHERE " + STUDENT_ID + " = " + id; try { connection.Open(); using (SqlCommand command = new SqlCommand(sqlQuery, connection)) { studentActivitiesList = new List <StudentActivities>(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { StudentActivities activity = StudentActivitiesUtility.getStudentActivitiesFormReader(reader); studentActivitiesList.Add(activity); } } } } catch (InvalidOperationException e) { return(e); } catch (SqlException e) { return(e); } catch (Exception e) { return(e); } } return(studentActivitiesList); }
internal static StudentActivities getStudentActivitiesFormReader(SqlDataReader reader) { StudentActivities activity = new StudentActivities(); activity.Id = (reader[0] as int?) ?? null; activity.ActivityTypeId = (reader[1] as int?) ?? null; activity.StudentId = (reader[2] as int?) ?? null; activity.Comment = reader[3] as string; activity.Content = reader[4] as string; activity.ActionTime = reader[5] as string; activity.CreatedOn = reader[6] as string; activity.UpdatedOn = reader[7] as string; return(activity); }
public static Object insertStudentActivity(StudentActivities activity) { { String connectionString = DatabaseConnections.getConnectionString("NARAYANA_CLIENT"); // define INSERT query with parameters string query = "INSERT INTO " + STUDENT_ACTIVITIES_TABLE + " (" + ACTIVITY_TYPE_ID + "," + STUDENT_ID + "," + COMMENT + "," + CONTENT + "," + ACTION_TIME + "," + CREATED_ON + "," + UPDATED_ON + "," + IS_DONE + ")" + " VALUES (@activityTypeId, @studentId, @Comment, @Content," + " @actionTime, @createdOn, @updatedOn," + " @isDone); " + " SELECT CAST(scope_identity() AS int)"; try { //create connection and command using (SqlConnection cn = new SqlConnection(connectionString)) using (SqlCommand cmd = new SqlCommand(query, cn)) { long milliseconds = DateTimeOffset.Now.ToUnixTimeMilliseconds(); // define parameters and their values cmd.Parameters.Add("@activityTypeId", SqlDbType.NVarChar, 50).Value = activity.ActivityTypeId ?? (object)DBNull.Value; cmd.Parameters.Add("@studentId", SqlDbType.NVarChar, 50).Value = activity.StudentId ?? (object)DBNull.Value; cmd.Parameters.Add("@Comment", SqlDbType.NVarChar, 50).Value = activity.Comment ?? (object)DBNull.Value; cmd.Parameters.Add("@Content", SqlDbType.NVarChar, 50).Value = activity.Content ?? (object)DBNull.Value; cmd.Parameters.Add("@actionTime", SqlDbType.NVarChar, 50).Value = activity.ActionTime ?? (object)DBNull.Value; cmd.Parameters.Add("@createdOn", SqlDbType.NVarChar, 50).Value = milliseconds; cmd.Parameters.Add("@updatedOn", SqlDbType.NVarChar, 50).Value = milliseconds; cmd.Parameters.Add("@isDone", SqlDbType.Bit, 50).Value = activity.IsDone ?? false; int newID; cn.Open(); newID = (int)cmd.ExecuteScalar(); cn.Close(); activity.Id = newID; return(activity); } } catch (SqlException e) { return(e); } catch (InvalidOperationException e) { return(e); } catch (InvalidCastException e) { return(e); } catch (Exception e) { return(e); } } }
internal static bool checkForValidStudent(out string error, StudentActivities activity) { if (activity.ActivityTypeId == null || activity.Id < 1) { error = "Activity type is not valid"; return(false); } if (String.IsNullOrEmpty(activity.ActionTime)) { error = "Acti"; return(false); } error = "successful"; return(true); }
public ActionResult Post([FromBody] StudentActivities activity) { String error; if (StudentActivitiesUtility.checkForValidStudent(out error, activity)) { Object result = StudentActivitiesTable.insertStudentActivity(activity); StudentActivities activity1 = result as StudentActivities; if (null != activity1) { return(Ok(activity1)); } else { return(NotFound(result)); } } else { return(NotFound(new ErrorText(error))); } }
public void LogStudent(StudentActivities activity) { throw new NotImplementedException(); }
public void LogStudent(StudentActivities activity) { _repository.LogStudent(activity); }