コード例 #1
0
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            ClassModel Class = new ClassModel();

            Class.ClassDate   = datePicker.Value.ToString("yyyy-MM-dd");
            Class.StartTimeId = comboBoxStartTime.SelectedIndex + 1;
            Class.EndTimeId   = comboBoxEndTime.SelectedIndex + 1;
            Class.RoomNo      = textBoxRoomNo.Text;
            Class.SectionId   = section.Id;
            if (comboBoxClassType.SelectedIndex == 0)
            {
                Class.ClassType = ClassTypes.Lab;
            }
            else if (comboBoxClassType.SelectedIndex == 1)
            {
                Class.ClassType = ClassTypes.Theory;
            }
            else
            {
                try
                {
                    throw new Exception("Invalid class type");
                }catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            ClassController ccontroller = new ClassController();

            try
            {
                Class.IsValid();

                List <ClassModel> sameDayClasses = ccontroller.GetByDateAndFacultyId(Class.ClassDate, faculty.Id);
                //Console.WriteLine("Found " + sameDayClasses.Count + " classes");
                foreach (ClassModel model in sameDayClasses)
                {
                    //Console.WriteLine("start id: " + model.StartTimeId + " end id: " + model.EndTimeId);
                    //Console.WriteLine("this start: " + Class.StartTimeId + " this end: " + Class.EndTimeId);

                    if (Class.StartTimeId < model.EndTimeId && Class.StartTimeId >= model.StartTimeId)
                    {
                        throw new Exception("Class time clashes with another class on " + Class.ClassDate);
                    }
                    if (Class.EndTimeId <= model.EndTimeId && Class.EndTimeId > model.StartTimeId)
                    {
                        throw new Exception("Class time clashes with another class on " + Class.ClassDate);
                    }
                }
                //Console.WriteLine("Clash checking complete");
                try
                {
                    var createdClass = ccontroller.Create(Class);

                    string qrstring        = createdClass.Id.ToString() + "|" + section.SectionName.ToString() + "|" + createdClass.CreatedAt.ToString();
                    string encodedqrstring = Convert.ToBase64String(Encoding.UTF8.GetBytes(qrstring));
                    string decodedqrstring = (Encoding.UTF8.GetString(Convert.FromBase64String(encodedqrstring)));
                    //Console.WriteLine(decodedqrstring);
                    try
                    {
                        ccontroller.InsertQRCode(createdClass.Id, encodedqrstring);

                        SectionStudentController sscontroller = new SectionStudentController();
                        List <int> sectionStudentsId          = sscontroller.GetAllBySection(section.Id);

                        AttendanceController acontroller = new AttendanceController();

                        foreach (int StudentId in sectionStudentsId)
                        {
                            AttendanceModel attendance = new AttendanceModel();
                            attendance.ClassId   = createdClass.Id;
                            attendance.StudentId = StudentId;

                            try
                            {
                                acontroller.Create(attendance);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                        }
                        //Console.WriteLine("Created class id: " + createdClass.Id);
                        MessageBox.Show("Class added on " + (createdClass.ClassDate));
                        this.Hide();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }