public bool Handle(CreateTopicCommand request)
        {
            var model = request.Adapt <Model.Topic>();
            int id    = FindCountInDB("select count(*) from Topics where Courses_course_id=" + model.CourseId.ToString()) + 1;

            using (MySqlConnection conn = _context.GetConnection())
            {
                conn.Open();
                string query = string.Format("insert into Topics(topic_id, topic_name, count_of_questions, Courses_course_id) " +
                                             "values('{0}', '{1}', '{2}', '{3}')", id, model.Name, model.CountOfQuestions, model.CourseId);
                MySqlCommand cmd = new MySqlCommand(query, conn);
                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    return(false);
                }
                finally
                {
                    conn.CloseAsync();
                }
            }

            return(true);
        }
Example #2
0
        public bool Handle(int topicId, CreateTopicCommand request)
        {
            var model = request.Adapt <Model.Topic>();

            using (MySqlConnection conn = _context.GetConnection())
            {
                conn.Open();
                string query = string.Format("update Topics set topic_name='{1}', count_of_questions={2}," +
                                             "Courses_course_id={3} where topic_id={0}",
                                             topicId,
                                             model.Name,
                                             model.CountOfQuestions,
                                             model.CourseId);

                MySqlCommand cmd = new MySqlCommand(query, conn);
                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch
                {
                    return(false);
                }
                finally
                {
                    conn.CloseAsync();
                }
            }

            return(true);
        }
Example #3
0
        public bool Handle(int topicId, int courseId)
        {
            using (MySqlConnection conn = _context.GetConnection())
            {
                conn.Open();
                string       query = string.Format("delete from Topics where (topic_id, Courses_course_id) = ({0}, {1})", topicId, courseId);
                MySqlCommand cmd   = new MySqlCommand(query, conn);
                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch
                {
                    return(false);
                }
                finally
                {
                    conn.CloseAsync();
                }
            }

            return(true);
        }
        public List <Model.Topic> Handle(int courseId)
        {
            List <Model.Topic> list = new List <Model.Topic>();

            using (MySqlConnection conn = _context.GetConnection())
            {
                conn.Open();
                string       query = "select * from Topics where Courses_course_id = " + courseId.ToString();
                MySqlCommand cmd   = new MySqlCommand(query, conn);

                try
                {
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            list.Add(new Model.Topic()
                            {
                                Id               = Convert.ToInt32(reader["topic_id"]),
                                Name             = reader["topic_name"].ToString(),
                                CountOfQuestions = Convert.ToInt32(reader["count_of_questions"]),
                                CourseId         = Convert.ToInt32(reader["Courses_course_id"])
                            });
                        }
                    }
                }
                catch
                {
                    return(null);
                }
                finally
                {
                    conn.CloseAsync();
                }
            }
            return(list);
        }