Ejemplo n.º 1
0
        public static async Task <List <TimeSlot> > CreateTimeSlotsForDateUTC(UULContext context, DateTime dateUtc, int hourToStart)
        {
            var rules = await RulesDao.GetCurrentRulesOrDefault(context);

            DateOperations.GetTimeSlotsBoundsUtc(rules.TimeSlotSpan, dateUtc.Year, dateUtc.Month, dateUtc.Day, out DateTime start, out DateTime end);
            var existent = await TimeSlotsDao.GetTimeSlotsByUtcBounds(context, start, end);

            if (existent.Count != 0)
            {
                return(new List <TimeSlot>());
            }
            var limit     = dateUtc.AddDays(1);
            var slotStart = dateUtc.AddHours(hourToStart);
            var slots     = new List <TimeSlot>();
            var slotSpan  = TimeSpan.FromMinutes(rules.TimeSlotSpan);

            while (slotStart.CompareTo(limit) < 0)
            {
                foreach (Gym gym in rules.Gyms)
                {
                    var slot = new TimeSlot {
                        Start = slotStart.ToUniversalTime(),
                        End   = (slotStart + slotSpan).ToUniversalTime(),
                        Gym   = gym
                    };
                    slots.Add(slot);
                }
                slotStart += slotSpan;
            }
            return(slots);
        }
Ejemplo n.º 2
0
        public static User AddFromDto(UULContext context, NewUserDTO newUser)
        {
            var salt = SecHelper.CreateSalt();

            var habitant = new Habitant(newUser);

            context.Habitants.Add(habitant);

            var userToSave = new User {
                Login         = newUser.Login,
                IsActivated   = false,
                CreatedAt     = DateOperations.Now(),
                Hash          = SecHelper.SaltAndHashPwd(newUser.Pwd, salt),
                Salt          = salt,
                ApartmentCode = newUser.ApartmentCode,
                Habitants     = new List <Habitant>()
                {
                    habitant
                }
            };

            context.Users.Add(userToSave);

            return(userToSave);
        }
Ejemplo n.º 3
0
        public static async Task <User> GetUserFromClaimsOrDefault(UULContext _context, ClaimsPrincipal currentUser)
        {
            var userInfo = SecHelper.GetUserInfo(currentUser.Claims);
            var user     = await _context.Users.Where(u => u.Login.Equals(userInfo.Login) && u.ApartmentCode.Equals(userInfo.ApartmentCode)).SingleOrDefaultAsync();

            return(user);
        }
Ejemplo n.º 4
0
        public static void CreateDummyData(UULContext context)
        {
            for (int i = 0; i < 20; i++)
            {
                var user = new NewUserDTO()
                {
                    Login = "******" + i, AvatarSrc = "Default", Name = "Inhabitant_" + i, ApartmentCode = "T" + i, Pwd = "12345"
                };
                UserDao.AddFromDto(context, user);
            }
            context.SaveChanges();
            List <TimeSlot> timeSlots = new();
            var             today     = DateTime.UtcNow;

            for (int i = 0; i < 3; i++)
            {
                var slots = TimeSlotsFactory.CreateTimeSlotsForDateUTC(context, new DateTime(today.Year, today.Month, today.Day - i).ToUniversalTime(), 5);
                slots.Wait();
                timeSlots.AddRange(slots.Result);
            }
            var habitants = context.Habitants.ToList();
            var size      = habitants.Count;
            var rnd       = new Random();

            foreach (TimeSlot timeSlot in timeSlots)
            {
                for (int i = 0; i < rnd.Next(4); i++)
                {
                    if (timeSlot.OccupiedBy == null)
                    {
                        timeSlot.OccupiedBy = new List <Habitant>();
                    }
                    timeSlot.OccupiedBy.Add(habitants.ElementAt(rnd.Next(size)));
                }
            }
            context.TimeSlots.AddRange(timeSlots);

            var newsList = new List <News>();

            for (int i = 0; i < 5; i++)
            {
                newsList.Add(new News()
                {
                    Title = "Title " + i, Content = " Content " + i + dummyContent, Author = "Dummy data generator", CreatedAt = DateTime.UtcNow, Auditory = Auditory.GUESTS, NewsType = NewsType.INFO
                });
            }
            newsList.Add(new News()
            {
                Title = "Title Registered", Content = " Content Registered", Author = "Dummy data generator", CreatedAt = DateTime.UtcNow, Auditory = Auditory.REGISTERED, NewsType = NewsType.CALL_TO_ACTION
            });
            newsList.Add(new News()
            {
                Title = "Title Activated", Content = " Content Activated", Author = "Dummy data generator", CreatedAt = DateTime.UtcNow, Auditory = Auditory.ACTIVATED, NewsType = NewsType.ALERT
            });
            context.News.AddRange(newsList);
        }
Ejemplo n.º 5
0
 public static Task <List <TimeSlotDTO> > GetTimeSlotsByUtcBounds(UULContext context, DateTime start, DateTime end)
 {
     return(context.TimeSlots
            .Where(ts => ts.Start.CompareTo(start) >= 0 && ts.End.CompareTo(end) < 0)
            .Include(ts => ts.Gym)
            .Include(slot => slot.OccupiedBy)
            .ThenInclude(h => h.User)
            .OrderBy(ts => ts.Start)
            .Select(e => e.ToDTO())
            .ToListAsync());
 }
Ejemplo n.º 6
0
        public static async Task <Rules> GetCurrentRulesOrDefault(UULContext _context)
        {
            var rules = await _context.Rules
                        .AsSplitQuery()
                        .Include(r => r.Towers)
                        .Include(r => r.SpecialFloors)
                        .Include(r => r.BannedApartments)
                        .Include(r => r.Gyms)
                        .OrderByDescending(r => r.Version)
                        .FirstOrDefaultAsync();

            return(rules);
        }
Ejemplo n.º 7
0
 public static async Task <User> GetUserFromClaimsOrThrow(UULContext _context, ClaimsPrincipal currentUser)
 {
     return(await GetUserFromClaimsOrDefault(_context, currentUser) ?? throw new Exception("User not found"));
 }
Ejemplo n.º 8
0
 public static async Task <User> GetUserByDetailsOrThrow(UULContext context, string login, string apartment)
 {
     return((await context.Users.Where(u => u.Login.Equals(login) && u.ApartmentCode.Equals(apartment)).SingleOrDefaultAsync()) ?? throw new UserProfileNotFoundException());
 }
Ejemplo n.º 9
0
 public static async Task <ICollection <News> > GetNewsAsync(UULContext context, Auditory auditory)
 {
     return(await context.News.Where(n => (int)n.Auditory <= (int)auditory).OrderByDescending(n => n.UpdatedAt).ThenByDescending(n => n.CreatedAt).ToListAsync());
 }
Ejemplo n.º 10
0
 public static async Task <News> GetNewsByIdAsync(UULContext context, Auditory auditory, long Id)
 {
     return(await context.News.Where(n => (int)n.Auditory <= (int)auditory && n.ID == Id).SingleOrDefaultAsync());
 }
Ejemplo n.º 11
0
 public RulesController(UULContext context, ILogger <RulesController> logger)
 {
     _context = context;
     _logger  = logger;
 }
Ejemplo n.º 12
0
 public UsersController(UULContext context, IConfiguration config, ILogger <UsersController> logger)
 {
     _context = context;
     _config  = config;
     _logger  = logger;
 }
Ejemplo n.º 13
0
 public TimeSlotsController(UULContext context, ILogger <TimeSlotsController> logger)
 {
     _context = context;
     _logger  = logger;
 }
Ejemplo n.º 14
0
 public static Task <List <TimeSlot> > CreateTodayTimeSlots(UULContext context, int hourToStart) => CreateTimeSlotsForDateUTC(context, DateOperations.Today(), hourToStart);
Ejemplo n.º 15
0
        public static void Initialize(UULContext context, IConfiguration config)
        {
            if (config.GetValue <bool>("DropDataOnStart") == true)
            {
                context.Database.EnsureDeleted();
            }
            context.Database.EnsureCreated();
            if (!context.Users.Any())
            {
                context.Users.Add(SecHelper.CreateDefaultAdmin());
            }
            if (!context.Rules.Any())
            {
                context.Towers.RemoveRange(context.Towers);
                context.SpecialFloors.RemoveRange(context.SpecialFloors);
                context.BannedApartments.RemoveRange(context.BannedApartments);
                context.Gyms.RemoveRange(context.Gyms);

                var towers = new List <Tower>()
                {
                    new Tower()
                    {
                        Name = "A", FloorsCount = 10
                    },
                    new Tower()
                    {
                        Name = "B", FloorsCount = 10
                    },
                    new Tower()
                    {
                        Name = "C", FloorsCount = 12
                    },
                    new Tower()
                    {
                        Name = "D", FloorsCount = 12
                    }
                };

                var specialFloors = new List <SpecialFloor>()
                {
                    new SpecialFloor()
                    {
                        Name = "A10", Alias = "PH"
                    },
                    new SpecialFloor()
                    {
                        Name = "B10", Alias = "PH"
                    },
                    new SpecialFloor()
                    {
                        Name = "C12", Alias = "PH"
                    },
                    new SpecialFloor()
                    {
                        Name = "D12", Alias = "PH"
                    },
                };

                var gyms = new List <Gym>()
                {
                    new Gym()
                    {
                        Name = "A", IsOpen = true
                    },
                    new Gym()
                    {
                        Name = "B", IsOpen = true
                    }
                };

                context.Towers.AddRange(towers);
                context.SpecialFloors.AddRange(specialFloors);
                context.Gyms.AddRange(gyms);

                var rules = new Rules()
                {
                    Version               = 0,
                    PersonsPerTimeSlot    = 4,
                    HabitantsPerApartment = 4,
                    DoorsPerFloor         = 8,
                    TimeSlotSpan          = DefaultTimeSlotSpan,
                    Towers           = towers,
                    SpecialFloors    = specialFloors,
                    BannedApartments = { },
                    Gyms             = gyms
                };

                context.Rules.Add(rules);
                context.SaveChanges();
            }
            if (config.GetValue <bool>("CreateDummyDataOnStart") == false)
            {
                var newSlots = TimeSlotsFactory.CreateTodayTimeSlots(context, 11); // 11 Utc is 5 am at Gdl
                newSlots.Wait();
                context.TimeSlots.AddRange(newSlots.Result);
            }
            else
            {
                DummyDataFactory.CreateDummyData(context);
            }
            context.SaveChanges();
        }
Ejemplo n.º 16
0
 public WebUsersController(UULContext context)
 {
     _context = context;
 }
Ejemplo n.º 17
0
 public WebNewsController(UULContext context)
 {
     _context = context;
 }
Ejemplo n.º 18
0
 public HabitantsController(UULContext context, ILogger <HabitantsController> logger)
 {
     _context = context;
     _logger  = logger;
 }
Ejemplo n.º 19
0
 public NewsController(UULContext context, ILogger <NewsController> logger)
 {
     _context = context;
     _logger  = logger;
 }