public void SaveOrUpdate(Professor professorToSave)
        {
            if (professorToSave.Id == 0)
                _entitiesDb.Professor.Add(professorToSave);

            _entitiesDb.SaveChanges();
        }
Exemplo n.º 2
0
        private void btnSalvar_Click(object sender, EventArgs e)
        {
            lblMessage.Visible = false;
            loading.Visible = true;

            Professor professor;

            try
            {
                if (_professorId == 0)
                    professor = new Professor();
                else
                    professor = _professorDatalayer.GetById(_professorId);

                // fills professor data
                professor.Name = txtName.Text;
                professor.Subject = txtSubject.Text;
                professor.NumberOfClassesWeek = Convert.ToInt32(txtWeekClassesQty.Text);
                professor.NumberOfRemainingClasses = professor.NumberOfClassesWeek;

                _professorDatalayer.RemoveAttendanceRules(professor.AttendanceRules.ToList());

                // fills attendance rules data
                foreach (var item in WeekAttendanceDict)
                {
                    var attendanceRule = new AttendanceRules();
                    attendanceRule.DayOfWeek = (int)item.Key;
                    attendanceRule.ClassNumber = item.Value;
                    attendanceRule.Professor = professor;

                    professor.AttendanceRules.Add(attendanceRule);
                }

                _professorDatalayer.SaveOrUpdate(professor);

                loading.Visible = false;
                lblMessage.Visible = true;
                this.btnExcluir.Visible = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }