Beispiel #1
0
        private static void OnCompleted(object sender, WorkerChangedEventArgs e)
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                IPipe worker = _workers.FirstOrDefault(w => w.Id == e.WorkerId);

                if (worker == null)
                {
                    return;
                }

                try
                {
                    using (var context = new TachographContext())
                    {
                        var workerTaskEntity = context.WorkerTasks.FirstOrDefault(t => t.WorkerId == e.WorkerId);
                        if (workerTaskEntity != null)
                        {
                            workerTaskEntity.IsProcessing = false;
                            workerTaskEntity.Processed    = DateTime.Now;
                            workerTaskEntity.Message      = e.Message;
                        }

                        context.SaveChanges();
                    }
                }
                finally
                {
                    _workers.Remove(worker);
                }
            });
        }
        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();
                }
            });
        }
Beispiel #3
0
        public static ISettingsRepository <WorkshopSettings> SeedDatabase()
        {
            //This is a bit of a "fudge" to ensure that seed data has been created
            var settings = ContainerBootstrapper.Resolve <ISettingsRepository <WorkshopSettings> >();

            if (settings.GetWorkshopSettings() == null)
            {
                using (var context = new TachographContext())
                {
                    Seed(context);
                    context.SaveChanges();
                }
            }

            //Check that the 'super user account' exists
            var userRepository = ContainerBootstrapper.Resolve <IRepository <User> >();
            var superUser      = userRepository.FirstOrDefault(user => string.Equals(user.Username, "superuser", StringComparison.CurrentCultureIgnoreCase));

            if (superUser == null)
            {
                UserManagement.AddSuperUser(userRepository);
            }
            else
            {
                UserManagement.UpdateSuperUser(userRepository, superUser);
            }

            UserManagement.AddDefaultUser();

            var themeSettingsRepository = ContainerBootstrapper.Resolve <ISettingsRepository <ThemeSettings> >();

            if (themeSettingsRepository.GetThemeSettings() == null)
            {
                using (var context = new TachographContext())
                {
                    context.ThemeSettings.Add(new ThemeSettings {
                        SelectedTheme = "Silver"
                    });
                    context.SaveChanges();
                }
            }

            var miscellaneousSettingsRepository = ContainerBootstrapper.Resolve <ISettingsRepository <MiscellaneousSettings> >();

            if (miscellaneousSettingsRepository.GetMiscellaneousSettings() == null)
            {
                using (var context = new TachographContext())
                {
                    context.MiscellaneousSettings.Add(new MiscellaneousSettings());
                    context.SaveChanges();
                }
            }

            return(settings);
        }
Beispiel #4
0
 public virtual void Add(T entity)
 {
     Safely(() =>
     {
         using (var context = new TachographContext())
         {
             context.Set <T>().Add(entity);
             context.SaveChanges();
         }
     });
 }
Beispiel #5
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 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();
                }
            });
        }
Beispiel #7
0
        public static ThemeSettings GetThemeSettings(this ISettingsRepository <ThemeSettings> repository)
        {
            var themeSettings = repository.Get(w => !string.IsNullOrEmpty(w.SelectedTheme));

            if (themeSettings == null)
            {
                using (var context = new TachographContext())
                {
                    themeSettings = context.ThemeSettings.Add(new ThemeSettings {
                        SelectedTheme = "Silver"
                    });
                    context.SaveChanges();
                }
            }
            return(themeSettings);
        }
Beispiel #8
0
        public static AdvertisingSettings GetAdvertisingSettings(this ISettingsRepository <AdvertisingSettings> repository)
        {
            var settings = repository.Get();

            if (settings == null)
            {
                using (var context = new TachographContext())
                {
                    settings = context.AdvertisingSettings.Add(new AdvertisingSettings {
                        FontSize = 12, Font = "Arial"
                    });
                    context.SaveChanges();
                }
            }
            return(settings);
        }
        public override void Remove(TachographMake entity)
        {
            Safely(() =>
            {
                using (var context = new TachographContext())
                {
                    var existingEntity = context.TachographMakes.Include(m => m.Models).FirstOrDefault(c => c.Id == entity.Id);
                    if (existingEntity != null)
                    {
                        existingEntity.Models.ForEach(m => m.Deleted = DateTime.Now);
                        existingEntity.Deleted = DateTime.Now;
                    }

                    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();
                    }
                }
            });
        }
Beispiel #11
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();
         }
     });
 }
Beispiel #12
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();
                }
            });
        }