示例#1
0
 public DistributeScheduleController(DistributeScheduleService distributeSchedule, ShiftService shiftService, SheriffDbContext db, IConfiguration configuration)
 {
     DistributeScheduleService = distributeSchedule;
     ShiftService  = shiftService;
     Db            = db;
     Configuration = configuration;
 }
        public void PersistingDataTest()
        {
            var lookupCode = new LookupCode
            {
                Description = "desc"
            };

            using (var dbContext = new SheriffDbContext(EnvironmentBuilder.SetupDbOptions()))
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    using (TransactionScope scope2 = new TransactionScope())
                    {
                        dbContext.LookupCode.Add(lookupCode);
                        dbContext.SaveChanges();
                        scope2.Complete();
                        scope.Complete();
                    }
                }
            }

            using (var dbContext = new SheriffDbContext(EnvironmentBuilder.SetupDbOptions()))
            {
                var workSectionCode2 = dbContext.LookupCode.Find(lookupCode.Id);
                Assert.NotNull(workSectionCode2);
                dbContext.Remove(workSectionCode2);
                dbContext.SaveChanges();
            }
        }
示例#3
0
 public ShiftController(ShiftService shiftService, DutyRosterService dutyRosterService, SheriffDbContext db, IConfiguration configuration)
 {
     ShiftService      = shiftService;
     DutyRosterService = dutyRosterService;
     Db            = db;
     Configuration = configuration;
 }
示例#4
0
        private void ExecuteSeedScripts(SheriffDbContext db, IWebHostEnvironment environment)
        {
            var seedPath  = environment.IsDevelopment() ? Path.Combine("docker", "seed") : "data";
            var dbSqlPath = environment.IsDevelopment() ? Path.Combine("db", "sql") : Path.Combine("src", "db", "sql");
            var path      = Path.Combine(Directory.GetParent(Environment.CurrentDirectory).FullName, seedPath);
            var dbPath    = Path.Combine(Directory.GetParent(Environment.CurrentDirectory).FullName, dbSqlPath);

            Logger.LogInformation($"Fresh database detected. Loading SQL from paths: {dbPath} and then {path}");

            var transaction = db.Database.BeginTransaction();
            var lastFile    = "";

            try
            {
                var files = GetSqlFilesOrderedByNumber(dbPath).Concat(GetSqlFilesOrderedByNumber(path)).ToList();
                Logger.LogInformation($"Found {files.Count} files.");
                foreach (var file in files)
                {
                    lastFile = file;
                    Logger.LogInformation($"Executing File: {file}");
                    db.Database.ExecuteSqlRaw(File.ReadAllText(file));
                }
                transaction.Commit();
                Logger.LogInformation($"Executing files successful.");
            }
            catch (Exception e)
            {
                Logger.LogError(e, $"Error while executing {lastFile}. Rolling back all files.");
                transaction.Rollback();
            }
        }
        public void RollbackDataTest()
        {
            var lookupCode = new LookupCode
            {
                Description = "desc"
            };

            using (var dbContext = new SheriffDbContext(EnvironmentBuilder.SetupDbOptions()))
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    using (TransactionScope scope2 = new TransactionScope())
                    {
                        dbContext.LookupCode.Add(lookupCode);
                        dbContext.SaveChanges();
                        scope2.Complete();
                    }
                    //No scope complete, should rollback.
                }
            }

            using (var dbContext = new SheriffDbContext(EnvironmentBuilder.SetupDbOptions()))
            {
                var workSectionCode2 = dbContext.LookupCode.Find(lookupCode.Id);
                Assert.Null(workSectionCode2);
            }
        }
 public DutyRosterService(SheriffDbContext db, IConfiguration configuration, ShiftService shiftService, ILogger <DutyRosterService> logger)
 {
     Db                  = db;
     ShiftService        = shiftService;
     OvertimeHoursPerDay = double.Parse(configuration.GetNonEmptyValue("OvertimeHoursPerDay"));
     Logger              = logger;
 }
 public AuthController(IWebHostEnvironment env, IConfiguration configuration, ChesEmailService chesEmailService, SheriffDbContext db)
 {
     Configuration    = configuration;
     ChesEmailService = chesEmailService;
     IsImpersonated   = env.IsDevelopment() &&
                        configuration.GetNonEmptyValue("ByPassAuthAndUseImpersonatedUser").Equals("true");
     Db = db;
 }
示例#8
0
 public ManageTypesControllerTests()
 {
     _dbContext  = new SheriffDbContext(EnvironmentBuilder.SetupDbOptions(useMemoryDatabase: true));
     _controller = new ManageTypesController(new ManageTypesService(_dbContext))
     {
         ControllerContext = HttpResponseTest.SetupMockControllerContext()
     };
 }
示例#9
0
 public SheriffController(SheriffService sheriffService, DutyRosterService dutyRosterService, ShiftService shiftService, UserService userUserService, IConfiguration configuration, SheriffDbContext db) : base(userUserService)
 {
     SheriffService    = sheriffService;
     ShiftService      = shiftService;
     DutyRosterService = dutyRosterService;
     Db = db;
     _uploadPhotoSizeLimitKB = Convert.ToInt32(configuration.GetNonEmptyValue("UploadPhotoSizeLimitKB"));
 }
 public JCDataUpdaterService(SheriffDbContext dbContext, LocationServicesClient locationClient, IConfiguration configuration, ILogger <JCDataUpdaterService> logger)
 {
     LocationClient = locationClient;
     Db             = dbContext;
     Configuration  = configuration;
     Expire         = Configuration.GetNonEmptyValue("JCSynchronization:Expire").Equals("true");
     AssociateUsersWithNoLocationToVictoria = Configuration.GetNonEmptyValue("JCSynchronization:AssociateUsersWithNoLocationToVictoria").Equals("true");
     Logger = logger;
 }
 public JCDataUpdaterService(SheriffDbContext dbContext, LocationServicesClient locationClient, IConfiguration configuration, ILogger <JCDataUpdaterService> logger)
 {
     LocationClient  = locationClient;
     Db              = dbContext;
     Configuration   = configuration;
     ExpireRegions   = Configuration.GetNonEmptyValue("JCSynchronization:ExpireRegions").Equals("true");
     ExpireLocations = Configuration.GetNonEmptyValue("JCSynchronization:ExpireLocations").Equals("true");
     ExpireRooms     = Configuration.GetNonEmptyValue("JCSynchronization:ExpireCourtRooms").Equals("true");
     AssociateUsersWithNoLocationToVictoria = Configuration.GetNonEmptyValue("JCSynchronization:AssociateUsersWithNoLocationToVictoria").Equals("true");
     UpdateEvery = TimeSpan.Parse(configuration.GetNonEmptyValue("JCSynchronization:UpdateEvery"));
     Logger      = logger;
 }
        public static bool HasAccessToLocation(ClaimsPrincipal currentUser, SheriffDbContext db, int?locationId)
        {
            var currentUserId             = currentUser.CurrentUserId();
            var currentUserHomeLocationId = currentUser.HomeLocationId();

            if (!locationId.HasValue || currentUser.HasPermission(Permission.ViewProvince))
            {
                return(true);
            }
            if (currentUser.HasPermission(Permission.ViewHomeLocation) && currentUserHomeLocationId == locationId)
            {
                return(true);
            }

            if (currentUser.HasPermission(Permission.ViewRegion))
            {
                var currentUserRegionId = db.Location.AsNoTracking().FirstOrDefault(l => l.Id == currentUserHomeLocationId)?.RegionId;
                var locationRegionId    = db.Location.AsNoTracking().FirstOrDefault(l => l.Id == locationId)?.RegionId;
                if (currentUserRegionId != null && currentUserRegionId == locationRegionId)
                {
                    return(true);
                }
            }

            if (currentUser.HasPermission(Permission.ViewAssignedLocation))
            {
                //Not sure if we want to put some sort of time limit on this.
                var assignedLocationIds = db.SheriffAwayLocation.AsNoTracking().Where(sal => sal.SheriffId == currentUserId &&
                                                                                      sal.ExpiryDate == null).Select(s => s.LocationId).Distinct().ToList();
                if (assignedLocationIds.Contains(locationId))
                {
                    return(true);
                }
            }
            return(false);
        }
 public ShiftController(ShiftService shiftService, SheriffDbContext db)
 {
     ShiftService = shiftService;
     Db           = db;
 }
示例#14
0
 public RoleService(SheriffDbContext dbContext)
 {
     Db = dbContext;
 }
示例#15
0
 public AuditController(SheriffService sheriffService, SheriffDbContext db)
 {
     SheriffService = sheriffService;
     Db             = db;
 }
示例#16
0
 public DistributeScheduleController(DistributeScheduleService distributeSchedule, ShiftService shiftService, SheriffDbContext db)
 {
     DistributeScheduleService = distributeSchedule;
     ShiftService = shiftService;
     Db           = db;
 }
 public WrapInTransactionScope(bool useMemoryDatabase)
 {
     Db     = new SheriffDbContext(EnvironmentBuilder.SetupDbOptions(useMemoryDatabase: useMemoryDatabase));
     _scope = new TransactionScope(TransactionScopeOption.Required, TransactionScopeAsyncFlowOption.Enabled);
 }
示例#18
0
 public DutyRosterController(DutyRosterService dutyRosterService, SheriffDbContext db, IConfiguration configuration)
 {
     DutyRosterService = dutyRosterService;
     Db            = db;
     Configuration = configuration;
 }
 public DistributeScheduleService(SheriffDbContext db)
 {
     Db = db;
 }
示例#20
0
 public AssignmentController(AssignmentService assignmentService, SheriffDbContext db)
 {
     AssignmentService = assignmentService;
     Db = db;
 }
示例#21
0
        /// <summary>
        /// Applies all of the IEntityTypeConfiguration objects in the specified assembly.
        /// </summary>
        /// <param name="modelBuilder"></param>
        /// <param name="assembly"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static ModelBuilder ApplyAllConfigurations(this ModelBuilder modelBuilder, Assembly assembly, SheriffDbContext context = null)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException(nameof(assembly));
            }

            // Find all the configuration classes.
            var type           = typeof(IEntityTypeConfiguration <>);
            var configurations = assembly.GetTypes().Where(t => t.IsClass && t.GetInterfaces().Any(i => i.Name.Equals(type.Name)));

            // Fetch the ApplyConfiguration method so that it can be called on each configuration.
            var method = typeof(ModelBuilder).GetMethods(BindingFlags.Instance | BindingFlags.Public).Where(m => m.Name.Equals(nameof(ModelBuilder.ApplyConfiguration)) && m.GetParameters()[0].ParameterType.GetGenericTypeDefinition() == type).First();

            foreach (var config in configurations)
            {
                if (!config.ContainsGenericParameters)
                {
                    var includeContext           = config.GetConstructors().Any(c => c.GetParameters().Any(p => p.ParameterType == typeof(SheriffDbContext)));
                    var entityConfig             = includeContext ? Activator.CreateInstance(config, context) : Activator.CreateInstance(config);
                    var entityType               = config.GetInterfaces().FirstOrDefault().GetGenericArguments()[0];
                    var applyConfigurationMethod = method.MakeGenericMethod(entityType);
                    applyConfigurationMethod.Invoke(modelBuilder, new[] { entityConfig });
                }
            }

            return(modelBuilder);
        }
示例#22
0
 /// <summary>
 /// Applies all of the IEntityTypeConfiguration objects in the assembly of the specified type.
 /// </summary>
 /// <param name="modelBuilder"></param>
 /// <param name="type"></param>
 /// <param name="context"></param>
 /// <returns></returns>
 public static ModelBuilder ApplyAllConfigurations(this ModelBuilder modelBuilder, Type type, SheriffDbContext context = null)
 {
     if (type == null)
     {
         throw new ArgumentNullException(nameof(type));
     }
     return(modelBuilder.ApplyAllConfigurations(type.Assembly, context));
 }
示例#23
0
 public AssignmentService(SheriffDbContext db)
 {
     Db = db;
 }
示例#24
0
 public ManageTypesController(ManageTypesService manageTypesService, SheriffDbContext db)
 {
     ManageTypesService = manageTypesService;
     Db = db;
 }
示例#25
0
 public ShiftService(SheriffDbContext db, SheriffService sheriffService, IConfiguration configuration)
 {
     Db                  = db;
     SheriffService      = sheriffService;
     OvertimeHoursPerDay = double.Parse(configuration.GetNonEmptyValue("OvertimeHoursPerDay"));
 }
示例#26
0
 public LocationController(SheriffDbContext dbContext)
 {
     Db = dbContext;
 }
示例#27
0
 public RegionController(SheriffDbContext dbContext)
 {
     Db = dbContext;
 }
示例#28
0
 public ConcurrencyExceptionTest()
 {
     Db = new SheriffDbContext(EnvironmentBuilder.SetupDbOptions(false));
 }
示例#29
0
 public ClaimsService(SheriffDbContext dbContext)
 {
     Db = dbContext;
 }
示例#30
0
 public SheriffService(SheriffDbContext db, IConfiguration configuration, IHttpContextAccessor httpContextAccessor = null)
 {
     Db            = db;
     Configuration = configuration;
     User          = httpContextAccessor?.HttpContext.User;
 }