public override void AddOrUpdate(TachographMake entity)
        {
            Safely(() =>
            {
                using (var context = new TachographContext())
                {
                    TachographMake existingMake = context.Set <TachographMake>().Find(entity.Id);
                    if (existingMake != null)
                    {
                        context.Entry(existingMake).CurrentValues.SetValues(entity);
                        foreach (var tachographModel in entity.Models)
                        {
                            if (tachographModel.IsNewEntity)
                            {
                                if (!string.IsNullOrEmpty(tachographModel.Name))
                                {
                                    tachographModel.TachographMake       = existingMake;
                                    context.Entry(tachographModel).State = EntityState.Added;
                                }
                            }
                        }
                    }
                    else
                    {
                        context.Set <TachographMake>().Add(entity);
                    }

                    context.SaveChanges();
                }
            });
        }
Esempio n. 2
0
 public virtual void Remove(T entity)
 {
     Safely(() =>
     {
         using (var context = new TachographContext())
         {
             entity.Deleted = DateTime.Now;
             context.Entry(entity).State = EntityState.Modified;
             context.SaveChanges();
         }
     });
 }
        public void Remove(TachographModel entity)
        {
            Safely(() =>
            {
                using (var context = new TachographContext())
                {
                    var tachographModels = context.TachographMakes.SelectMany(c => c.Models);
                    var existingEntity   = tachographModels.FirstOrDefault(c => c.Id == entity.Id);

                    if (existingEntity != null)
                    {
                        existingEntity.Deleted = DateTime.Now;
                        context.Entry(existingEntity).State = EntityState.Modified;
                        context.SaveChanges();
                    }
                }
            });
        }
Esempio n. 4
0
 public virtual void Save(T settings)
 {
     Safely(() =>
     {
         using (var context = new TachographContext())
         {
             if (settings == null)
             {
                 context.Set <T>().Add(new T());
             }
             else
             {
                 context.Set <T>().Attach(settings);
                 context.Entry(settings).State = EntityState.Modified;
             }
             context.SaveChanges();
         }
     });
 }
Esempio n. 5
0
        public virtual void AddOrUpdate(T entity)
        {
            Safely(() =>
            {
                using (var context = new TachographContext())
                {
                    T existing = context.Set <T>().Find(entity.Id);
                    if (existing != null)
                    {
                        context.Entry(existing).CurrentValues.SetValues(entity);
                    }
                    else
                    {
                        context.Set <T>().Add(entity);
                    }

                    context.SaveChanges();
                }
            });
        }
        public override void AddOrUpdate(TachographDocument entity)
        {
            Safely(() =>
            {
                using (var context = new TachographContext())
                {
                    TachographDocument existing = context.TachographDocuments.Find(entity.Id);
                    if (existing != null)
                    {
                        context.Entry(existing).CurrentValues.SetValues(entity);
                    }
                    else
                    {
                        context.Set <TachographDocument>().Add(entity);
                    }

                    CheckVehicleExists(context, entity);
                    context.SaveChanges();
                }
            });
        }
        public override void Save(WorkshopSettings settings)
        {
            Safely(() =>
            {
                using (var context = new TachographContext())
                {
                    var originalEntity = context.WorkshopSettings.AsNoTracking().FirstOrDefault(c => c.Id == settings.Id);
                    if (originalEntity != null)
                    {
                        if (!originalEntity.Equals(settings))
                        {
                            settings.Uploaded = null;
                        }
                    }

                    //Bin old data, don't need it
                    context.CustomDayOfWeeks.RemoveRange(context.CustomDayOfWeeks);
                    context.SaveChanges();

                    var daysOfWeek = settings.CustomDayOfWeeks.Clone();

                    //Save settings
                    settings.CustomDayOfWeeks = null;
                    context.Set <WorkshopSettings>().Attach(settings);
                    context.Entry(settings).State = EntityState.Modified;
                    context.SaveChanges();

                    //Save days of week
                    daysOfWeek.ForEach(c => c.WorkshopSettings = settings);
                    context.CustomDayOfWeeks.AddRange(daysOfWeek);

                    context.SaveChanges();

                    settings.CustomDayOfWeeks = context.CustomDayOfWeeks.ToList();
                }
            });
        }