Esempio n. 1
0
        public static Exam GetExamById(DBConnection connection, int examId)
        {
            SqlCommand command = connection.GetCommand(ExamTable.SQL_GET_EXAM_BY_ID);

            command.AddParameter("@examId", SqlDbType.Int, examId);

            try
            {
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    Exam exam = null;

                    while (reader.Read())
                    {
                        if (exam == null)
                        {
                            exam = new Exam(examId,
                                            (int)reader.GetColumnValue <decimal>("ownerId"),
                                            reader.GetColumnValue <string>("name"),
                                            reader.GetColumnValue <int>("timelimit"),
                                            reader.GetColumnValue <int>("minimum_points"),
                                            reader.GetColumnValue <int>("maximum_attempts"),
                                            reader.GetColumnValue <DateTime>("start_date"),
                                            reader.GetColumnValue <DateTime>("end_date"),
                                            reader.GetColumnValue <DateTime>("created_at")
                                            );
                        }

                        object questionId = reader.GetValue(reader.GetOrdinal("questionId"));

                        if (!DBNull.Value.Equals(questionId))
                        {
                            ExamQuestion examQuestion = new ExamQuestion(
                                (int)reader.GetColumnValue <decimal>("questionId"),
                                examId,
                                reader.GetColumnValue <int>("index"),
                                reader.GetColumnValue <decimal>("points")
                                );

                            exam.AddQuestion(examQuestion);
                        }
                    }

                    if (exam == null)
                    {
                        throw new DatabaseException("Couldn't find exam");
                    }

                    return(exam);
                }
            }
            catch (Exception e)
            {
                throw new DatabaseException(e.Message);
            }
        }
Esempio n. 2
0
 public void AddQuestion(ExamQuestion examQuestion)
 {
     this.Questions.Add(examQuestion);
 }