Beispiel #1
0
 public async Task <IList <TemperatureMeasurementModel> > GetAllAsync()
 {
     return(await SmartHomeAppDbContext.Query <TemperatureMeasurement>()
            .ProjectTo <TemperatureMeasurementModel>(Mapper.ConfigurationProvider)
            .OrderByDescending(x => x.MeasurementDateTime)
            .ToListAsync());
 }
Beispiel #2
0
        public async Task <IList <PermissionRoleModel> > GetAllUserPermissionsWithRolesAsync(long userId)
        {
            var user = await SmartHomeAppDbContext.SingleAsync <User>(userId);

            var userPermissions = await SmartHomeAppDbContext.Query <UserPermission>()
                                  .Where(x => x.UserId == userId)
                                  .ProjectTo <PermissionRoleModel>(Mapper.ConfigurationProvider)
                                  .ToListAsync();

            var rolesStrings = (List <string>) await userManager.GetRolesAsync(user);

            var rolePermissions = await SmartHomeAppDbContext.Query <RolePermission>()
                                  .Where(x => rolesStrings.Contains(x.Role.Name))
                                  .ProjectTo <PermissionRoleModel>(Mapper.ConfigurationProvider)
                                  .ToListAsync();

            var comparer = EqualityComparerFactory.Create <PermissionRoleModel>(
                x => (int)x.PermissionId,
                (x, y) => x.PermissionId == y.PermissionId && string.Equals(x.RoleName, y.RoleName));
            var resultPermissions = userPermissions.Union(rolePermissions, comparer);

            resultPermissions = resultPermissions.OrderBy(x => x.PermissionName);

            return(resultPermissions.ToList());
        }
Beispiel #3
0
 public UserRepository(SmartHomeAppDbContext smartHomeAppDbContext,
                       IMapper mapper,
                       UserManager <User> userManager,
                       SignInManager <User> signInManager) : base(smartHomeAppDbContext, mapper)
 {
     this.userManager   = userManager;
     this.signInManager = signInManager;
 }
Beispiel #4
0
        public async Task <IdentityResult> AddToRoleAsync(long userId, long roleId)
        {
            var user = await SmartHomeAppDbContext.SingleAsync <User>(userId);

            var roleName = (await SmartHomeAppDbContext.SingleAsync <Role>(roleId)).Name;

            return(await userManager.AddToRoleAsync(user, roleName));
        }
Beispiel #5
0
        public async Task RemovePermissionsFromUserAsync(long userId, List <long> permissions)
        {
            var permissionsToRemove = await SmartHomeAppDbContext.Query <UserPermission>()
                                      .Where(x => x.UserId == userId)
                                      .Where(x => permissions.Contains(x.Permission.Id))
                                      .ToListAsync();

            await SmartHomeAppDbContext.DeleteRangeAsync(permissionsToRemove);
        }
Beispiel #6
0
        public async Task <IdentityResult> RemoveFromRolesAsync(long userId, List <long> roleIds)
        {
            var user = await SmartHomeAppDbContext.SingleAsync <User>(userId);

            var roleNames = await SmartHomeAppDbContext.Query <Role>().Where(x => roleIds.Contains(x.Id))
                            .Select(x => x.Name).ToListAsync();

            return(await userManager.RemoveFromRolesAsync(user, roleNames));
        }
Beispiel #7
0
        public async Task <IList <PermissionModel> > GetUserOnlyPermissionsAsync(long userId)
        {
            var userPermissions = await SmartHomeAppDbContext.Query <UserPermission>()
                                  .Where(x => x.UserId == userId)
                                  .Select(x => x.Permission)
                                  .ProjectTo <PermissionModel>(Mapper.ConfigurationProvider)
                                  .ToListAsync();

            return(userPermissions);
        }
Beispiel #8
0
        public async Task <IdentityResult> AddToRolesAsync(long userId, List <long> roleIds)
        {
            var user = await SmartHomeAppDbContext.SingleAsync <User>(userId);

            // this query needs for some reason roleIds to be List
            var roleNames = await SmartHomeAppDbContext.Query <Role>().Where(x => roleIds.Contains(x.Id))
                            .Select(x => x.Name).ToListAsync();

            return(await userManager.AddToRolesAsync(user, roleNames));
        }
Beispiel #9
0
        public async Task <TemperatureMeasurementModel?> GetSensorLastTemperatureMeasurementAsync(long sensorId)
        {
            var query = SmartHomeAppDbContext.Query <TemperatureMeasurement>()
                        .Where(x => x.SensorId == sensorId);

            // take temperature measurement of given sensor with maximum date time
            var lastDateTime           = query.DefaultIfEmpty().Max(y => y.MeasurementDateTime);
            var temperatureMeasurement = await query.FirstOrDefaultAsync(x => x.MeasurementDateTime == lastDateTime);

            return(Mapper.Map <TemperatureMeasurementModel>(temperatureMeasurement));
        }
Beispiel #10
0
        public async Task <IList <RoleModel> > GetUserRolesAsync(long userId)
        {
            var user = await SmartHomeAppDbContext.SingleAsync <User>(userId);

            var rolesStrings = (List <string>) await userManager.GetRolesAsync(user);

            return(SmartHomeAppDbContext.Query <Role>()
                   .Where(x => rolesStrings.Contains(x.Name))
                   .ProjectTo <RoleModel>(Mapper.ConfigurationProvider)
                   .ToList());
        }
Beispiel #11
0
        public async Task <IList <PermissionModel> > GetRolePermissionsAsync(long roleId)
        {
            var rolePermissions = SmartHomeAppDbContext.Query <RolePermission>()
                                  .Where(x => x.RoleId == roleId)
                                  .Select(x => x.Permission)
                                  .OrderBy(x => x.Name)
                                  .ProjectTo <PermissionModel>(Mapper.ConfigurationProvider);
            var result = await rolePermissions.ToListAsync();

            return(result);
        }
Beispiel #12
0
        public async Task AddPermissionsToUserAsync(long userId, List <string> permissions)
        {
            var permissionIds = await SmartHomeAppDbContext.Query <Permission>()
                                .Where(x => permissions.Contains(x.Name))
                                .Select(x => x.Id)
                                .ToListAsync();

            var entities = permissionIds.Select(x => new UserPermission()
            {
                UserId       = userId,
                PermissionId = x
            });

            await SmartHomeAppDbContext.AddRangeAsync(entities);
        }
Beispiel #13
0
        public async Task <IList <OverviewTemperatureMeasurementModel> > GetAllSensorsLastTemperatureMeasurementAsync()
        {
            var orderedMeasurements = from item in SmartHomeAppDbContext.Query <TemperatureMeasurement>()
                                      orderby item.MeasurementDateTime descending
                                      select item;

            // TODO: ToListAsync() because EF Core 2.2 cannot translate GroupBy
            var query = await orderedMeasurements
                        .Include(x => x.Place)
                        .Include(x => x.Sensor)
                        .ThenInclude(x => x.SensorType)
                        .ToListAsync();

            var groupedMeasurements = query.GroupBy(x => x.SensorId);

            // last temperature measurements for each sensor
            var lastTemperatureMeasurements = groupedMeasurements.Select(x => x.First());

            return(lastTemperatureMeasurements.Select(x => Mapper.Map <OverviewTemperatureMeasurementModel>(x)).ToList());
        }
Beispiel #14
0
        private IQueryable <TemperatureMeasurement> GetTemperatureMeasurementsQuery(MeasurementFilter filter)
        {
            var query = SmartHomeAppDbContext.Query <TemperatureMeasurement>();

            if (filter.From != null)
            {
                query = query.Where(x => x.MeasurementDateTime >= filter.From);
            }

            if (filter.To != null)
            {
                query = query.Where(x => x.MeasurementDateTime <= filter.To.Value);
            }

            if (filter.SensorId != null)
            {
                query = query.Where(x => x.SensorId == filter.SensorId.Value);
            }

            return(query);
        }
Beispiel #15
0
        public async Task <long> AddAsync(long sensorId, double voltage, DateTime measurementDateTime)
        {
            var sensor = await SmartHomeAppDbContext.SingleAsync <Sensor>(sensorId);

            if (sensor.BatteryPowerSourceTypeId == null)
            {
                throw new ArgumentNullException(nameof(sensor.BatteryPowerSourceType), $"Sensor with id {sensor.Id} " +
                                                $"doesn't have any battery power source type, " +
                                                $"and thus can't have voltage measurements.");
            }

            var batteryMeasurement = new BatteryMeasurement()
            {
                BatteryPowerSourceTypeId = sensor.BatteryPowerSourceTypeId.Value,
                MeasurementDateTime      = measurementDateTime,
                SensorId = sensor.Id,
                Voltage  = voltage,
                PlaceId  = sensor.PlaceId.Value
            };

            return(await AddOrUpdateAsync(batteryMeasurement));
        }
Beispiel #16
0
        public async Task <bool> HasPermissionAsync(long userId, string permissionName)
        {
            var user = await SmartHomeAppDbContext.SingleAsync <User>(userId);

            bool hasPermission = await SmartHomeAppDbContext.Query <UserPermission>()
                                 .AnyAsync(x => x.UserId == userId && x.Permission.Name == permissionName);

            if (hasPermission)
            {
                return(true);
            }

            // has role that has that permission
            var roles = (List <string>) await userManager.GetRolesAsync(user);

            hasPermission = await SmartHomeAppDbContext.Query <RolePermission>()
                            // filter only for roles that user has
                            .Where(x => roles.Contains(x.Role.Name))
                            // does the role have that permission?
                            .AnyAsync(x => x.Permission.Name == permissionName);

            return(hasPermission);
        }
Beispiel #17
0
 public PermissionRepository(SmartHomeAppDbContext smartHomeAppDbContext,
                             IMapper mapper, UserManager <User> userManager) : base(smartHomeAppDbContext, mapper)
 {
     this.userManager = userManager;
 }
Beispiel #18
0
        public async Task <TemperatureMeasurementModel> GetByIdAsync(long id)
        {
            var temperatureMeasurement = await SmartHomeAppDbContext.SingleAsync <TemperatureMeasurement>(id);

            return(Mapper.Map <TemperatureMeasurementModel>(temperatureMeasurement));
        }
Beispiel #19
0
        public async Task <IdentityResult> ChangePasswordAsync(ChangePasswordModel changePasswordModel)
        {
            var user = await SmartHomeAppDbContext.SingleAsync <User>(changePasswordModel.Id);

            return(await userManager.ChangePasswordAsync(user, changePasswordModel.OldPassword, changePasswordModel.NewPassword));
        }
Beispiel #20
0
        public async Task <UserModel> GetByIdAsync(long id)
        {
            var entity = await SmartHomeAppDbContext.SingleAsync <User>(id);

            return(Mapper.Map <UserModel>(entity));
        }
Beispiel #21
0
 public virtual async Task <IList <TModel> > GetAllAsync()
 {
     return(await SmartHomeAppDbContext.Query <TEntity>().ProjectTo <TModel>(Mapper.ConfigurationProvider).ToListAsync());
 }
Beispiel #22
0
 public TemperatureMeasurementRepository(SmartHomeAppDbContext smartHomeAppDbContext,
                                         IMapper mapper, IDateTimeProvider dateTimeProvider) : base(smartHomeAppDbContext, mapper)
 {
     this.dateTimeProvider = dateTimeProvider;
 }
Beispiel #23
0
 public async Task <IList <UserModel> > GetAllAsync()
 {
     return(await SmartHomeAppDbContext.Query <User>().ProjectTo <UserModel>(Mapper.ConfigurationProvider).ToListAsync());
 }
Beispiel #24
0
        public async Task <IList <MeasurementStatisticsModel> > GetTemperatureMeasurementsAsync(StatisticsFilter filter)
        {
            var query = SmartHomeAppDbContext.Query <TemperatureMeasurement>();

            if (filter.IsInside != null)
            {
                query = query.Where(x => x.Place.IsInside == filter.IsInside.Value);
            }

            if (filter.DateFrom != null)
            {
                query = query.Where(x => x.MeasurementDateTime >= filter.DateFrom);
            }
            if (filter.DateTo != null)
            {
                query = query.Where(x => x.MeasurementDateTime <= filter.DateTo);
            }

            IEnumerable <MeasurementStatisticsModel> modelQuery;

            switch (filter.AggregateOver)
            {
            case AggregateOver.DayOfYear:
            {
                modelQuery = AggregateOverDayOfYear(query, filter.AggregateOverPlace);
                break;
            }

            case AggregateOver.Month:
            {
                modelQuery = AggregateOverMonth(query, filter.AggregateOverPlace);
                break;
            }

            case AggregateOver.Year:
            {
                modelQuery = AggregateOverYear(query, filter.AggregateOverPlace);
                break;
            }

            case null:
            {
                IQueryable <Tuple <long?, DateTime, int, double> > groupedQuery;
                if (!filter.AggregateOverPlace)
                {
                    groupedQuery = query
                                   // make groups that have same date + hour and place and make temperature average over them
                                   .GroupBy(x => new { x.MeasurementDateTime.Date, x.MeasurementDateTime.Hour, x.PlaceId })
                                   .Select(x => Tuple.Create((long?)x.Key.PlaceId, x.Key.Date, x.Key.Hour,
                                                             x.Average(y => y.Temperature)));
                }
                else
                {
                    groupedQuery = query
                                   // make groups that have same date + hour and place and make temperature average over them
                                   .GroupBy(x => new { x.MeasurementDateTime.Date, x.MeasurementDateTime.Hour })
                                   .Select(x =>
                                           Tuple.Create((long?)null, x.Key.Date, x.Key.Hour, x.Average(y => y.Temperature)));
                }

                modelQuery = groupedQuery.AsEnumerable()
                             .Select(x => new MeasurementStatisticsModel()
                    {
                        MeasurementDateTime = new DateTime(x.Item2.Year, x.Item2.Month, x.Item2.Day, x.Item3, 0, 0),
                        Value   = x.Item4,
                        PlaceId = x.Item1
                    });
                break;
            }

            default:
                throw new ArgumentOutOfRangeException();
            }

            var list = modelQuery
                       .ToList();

            return(list);
        }
Beispiel #25
0
 public SensorRepository(SmartHomeAppDbContext smartHomeAppDbContext, IMapper mapper) : base(smartHomeAppDbContext, mapper)
 {
 }
Beispiel #26
0
        public async Task <IdentityResult> DeleteUserAsync(long id)
        {
            var user = await SmartHomeAppDbContext.SingleAsync <User>(id);

            return(await userManager.DeleteAsync(user));
        }
Beispiel #27
0
        public async Task <PermissionModel?> GetByNameAsync(string name)
        {
            var permission = await SmartHomeAppDbContext.Query <Permission>().SingleOrDefaultAsync(x => x.Name == name);

            return(Mapper.Map <PermissionModel>(permission));
        }
Beispiel #28
0
        public async Task SignOutAsync(long userId)
        {
            var user = await SmartHomeAppDbContext.SingleAsync <User>(userId);

            await userManager.UpdateSecurityStampAsync(user);
        }
Beispiel #29
0
 public BatteryPowerSourceTypeRepository(SmartHomeAppDbContext smartHomeAppDbContext, IMapper mapper) : base(smartHomeAppDbContext, mapper)
 {
 }
Beispiel #30
0
 protected StandardRepository(SmartHomeAppDbContext smartHomeAppDbContext, IMapper mapper) : base(smartHomeAppDbContext, mapper)
 {
 }