public void Map(DriversMedical poco)
 {
     poco.DriverMedicalID = this.DriverMedicalID;
     poco.DriverID = this.DriverID;
     poco.MedTypeID = this.MedTypeID;
     poco.ExaminationDate = this.ExaminationDate;
     poco.ValidityDate = this.ValidityDate;
 }
 public DriverMedicalModel(DriversMedical poco)
     : this()
 {
     this.DriverMedicalID = poco.DriverMedicalID;
     this.DriverID = poco.DriverID;
     this.MedTypeID = poco.MedTypeID;
     this.ExaminationDate = poco.ExaminationDate;
     this.ValidityDate = poco.ValidityDate;
     this.IsChanged = false;
 }
        private static void InsertMedical(DSModel db, KeyBinder key, DriverMedicalModel model)
        {
            DriversMedical poco = new DriversMedical();
            model.Map(poco);
            db.Add(poco);

            key.AddKey(poco, model, model.GetName(p => p.DriverMedicalID));
            db.FlushChanges();
            SaveReminders(db, key, model, poco);
        }
        private static void SaveReminders(DSModel db, KeyBinder key, DriverMedicalModel model, DriversMedical poco)
        {
            string sqlDelete = @"DELETE FROM drivers_medicals_reminders WHERE DriverMedicalID = @DriverMedicalID AND DriverMedicalReminderID NOT IN (@DriverMedicalReminderID);";
            string ids = "0";
            if (model.Reminders.Count > 0)
                ids = string.Join<uint>(",", model.Reminders.Select(r => r.DriverMedicalReminderID));

            sqlDelete = sqlDelete.Replace("@DriverMedicalReminderID", ids);

            db.ExecuteNonQuery(sqlDelete, new MySqlParameter("DriverMedicalID", poco.DriverMedicalID));
            string sqlInsert = @"
                INSERT INTO drivers_medicals_reminders
                  (DriverMedicalID, ReminderID, ReminderType, ShouldRemind) VALUES (@DriverMedicalID, @ReminderID, @ReminderType, @ShouldRemind);
                SELECT LAST_INSERT_ID();";
            string sqlUpdate = @"
                UPDATE drivers_medicals_reminders
                SET
                  DriverMedicalID = @DriverMedicalID, 
                  ReminderID = @ReminderID, 
                  ReminderType = @ReminderType, 
                  ShouldRemind = @ShouldRemind
                WHERE
                  DriverMedicalReminderID = @DriverMedicalReminderID;";

            foreach (var rem in model.Reminders)
            {
                if (rem.DriverMedicalReminderID == 0)
                {
                    rem.DriverMedicalID = poco.DriverMedicalID;
                    UtilityModel<uint> temp = new UtilityModel<uint>();
                    temp.Value = db.ExecuteQuery<uint>(sqlInsert,
                        new MySqlParameter("DriverMedicalID", rem.DriverMedicalID),
                        new MySqlParameter("ReminderID", rem.ReminderID),
                        new MySqlParameter("ReminderType", rem.ReminderType),
                        new MySqlParameter("ShouldRemind", rem.ShouldRemind))
                        .First();
                    key.AddKey(temp, rem, "Value", rem.GetName(p => p.DriverMedicalReminderID));
                }
                else
                {
                    rem.DriverMedicalID = poco.DriverMedicalID;
                    db.ExecuteNonQuery(sqlUpdate,
                        new MySqlParameter("DriverMedicalReminderID", rem.DriverMedicalReminderID),
                        new MySqlParameter("DriverMedicalID", rem.DriverMedicalID),
                        new MySqlParameter("ReminderID", rem.ReminderID),
                        new MySqlParameter("ReminderType", rem.ReminderType),
                        new MySqlParameter("ShouldRemind", rem.ShouldRemind));
                }
            }
        }