private string GetInsertIntoCommandText <T>(T entity) { string commandText = null; if (typeof(T).Equals(typeof(Make))) { Make make = entity as Make; commandText = $"INSERT INTO Make(Name, VpicID) VALUES ('{make.Name}', {make.VpicID})"; } else if (typeof(T).Equals(typeof(Model))) { Model model = entity as Model; commandText = $"INSERT INTO Model(ID, Name, TrimName, MakeID, VpicID) VALUES ({model.ID}, '{model.Name}', '{model.TrimName}', {model.MakeID}, {model.VpicID})"; } else if (typeof(T).Equals(typeof(ScheduleRequest))) { ScheduleRequest sr = entity as ScheduleRequest; commandText = $"INSERT INTO schedulerequest(IP, VINID, Mileage, time)VALUES('{sr.IP}',{sr.VINID},{sr.Mileage},'{sr.Time}')"; } else if (typeof(T).Equals(typeof(Spare))) { Spare spare = entity as Spare; commandText = $"INSERT INTO spare(name, costOrigin, costReplacement, replacementProduction, originDuration, replacementDuration, serviceId, applicationId) VALUES('{spare.Name}',{spare.CostOrigin},{spare.CostReplacement},'{spare.ReplacementProduction}',{spare.OriginDuration},{spare.ReplacementDuration},{spare.ServiceId},{spare.ApplicationId})"; } else if (typeof(T).Equals(typeof(Application))) { Application app = entity as Application; commandText = $"INSERT INTO application(date, phone, email)VALUES('{app.Date}','{app.Phone}','{app.Email}')"; } return(commandText); }
private T CreateEntity <T>(MySqlDataReader reader, bool isLazy = false) where T : class { T entity = null; if (typeof(T).Equals(typeof(User))) { int id = (int)reader["id"]; string name = (string)reader["name"]; string phone = (string)reader["phone"]; CarDbContext db = new CarDbContext(connectionString); entity = new User { Id = id, Name = name, Phone = phone } as T; } else if (typeof(T).Equals(typeof(Make))) { int id = (int)reader["ID"]; string name = (string)reader["Name"]; int VpicID = (int)reader["VpicID"]; CarDbContext db = new CarDbContext(connectionString); entity = new Make { ID = id, Name = name, VpicID = VpicID } as T; } else if (typeof(T).Equals(typeof(Model))) { int id = (int)reader["ID"]; string name = (string)reader["Name"]; string trimName = (string)reader["TrimName"]; int MakeID = (int)reader["MakeID"]; int VpicID = (int)reader["VpicID"]; CarDbContext db = new CarDbContext(connectionString); Make make = db.Get <Make>(true).FirstOrDefault(m => m.ID == MakeID); entity = new Model { ID = id, Name = name, TrimName = trimName, MakeID = MakeID, VpicID = VpicID, Make = make } as T; } else if (typeof(T).Equals(typeof(ScheduleRequest))) { int id = (int)reader["ID"]; string IP = (string)reader["IP"]; int VINID = (int)reader["VINID"]; int Mileage = (int)reader["Mileage"]; string time = (string)reader["time"]; CarDbContext db = new CarDbContext(connectionString); entity = new ScheduleRequest { ID = id, IP = IP, VINID = VINID, Mileage = Mileage, Time = time } as T; } else if (typeof(T).Equals(typeof(Vin))) { int id = (int)reader["ID"]; string VIN = (string)reader["VIN"]; int ModelYearId = (int)reader["ModelYearId"]; CarDbContext db = new CarDbContext(connectionString); ModelYear modelYear = db.Get <ModelYear>(true).FirstOrDefault(m => m.ID == ModelYearId); entity = new Vin { ID = id, VIN = VIN, ModelYearId = ModelYearId, ModelYear = modelYear } as T; } else if (typeof(T).Equals(typeof(ModelYear))) { int ID = (int)reader["ID"]; int Year = (int)reader["Year"]; object CarDetailsJSON = reader["CarDetailsJSON"]; object ScheduleJSON = reader["ScheduleJSON"]; int ModelID = (int)reader["ModelID"]; int VpicID = (int)reader["VpicID"]; CarDbContext db = new CarDbContext(connectionString); Model model = db.Get <Model>(true).FirstOrDefault(m => m.ID == ModelID); entity = new ModelYear { ID = ID, Year = Year, CarDetailsJSON = CarDetailsJSON, ScheduleJSON = ScheduleJSON, ModelID = ModelID, VpicID = VpicID, Model = model } as T; } else if (typeof(T).Equals(typeof(Service))) { int id = (int)reader["id"]; string name = (string)reader["name"]; int modelId = (int)reader["modelId"]; CarDbContext db = new CarDbContext(connectionString); Model model = db.Get <Model>(true).FirstOrDefault(m => m.ID == modelId); entity = new Service { Id = id, Name = name, ModelId = modelId, Model = model } as T; } else if (typeof(T).Equals(typeof(Application))) { int id = (int)reader["id"]; string Date = (string)reader["Date"]; string Phone = (string)reader["Phone"]; string Email = (string)reader["Email"]; CarDbContext db = new CarDbContext(connectionString); entity = new Application { Id = id, Date = Date, Phone = Phone, Email = Email } as T; } else if (typeof(T).Equals(typeof(Spare))) { int id = (int)reader["id"]; string name = (string)reader["name"]; int costOrigin = (int)reader["costOrigin"]; int costReplacement = (int)reader["costReplacement"]; string replacementProduction = (string)reader["replacementProduction"]; int originDuration = (int)reader["originDuration"]; int replacementDuration = (int)reader["replacementDuration"]; int serviceId = (int)reader["serviceId"]; int applicationId = (int)reader["applicationId"]; CarDbContext db = new CarDbContext(connectionString); Service service = db.Get <Service>(true).FirstOrDefault(s => s.Id == serviceId); Application application = db.Get <Application>(true).FirstOrDefault(app => app.Id == applicationId); entity = new Spare { Id = id, Name = name, CostOrigin = costOrigin, CostReplacement = costReplacement, ReplacementProduction = replacementProduction, OriginDuration = originDuration, ReplacementDuration = replacementDuration, ServiceId = serviceId, ApplicationId = applicationId, Service = service, Application = application } as T; } else { throw new NotSupportedException(); } return(entity); }