コード例 #1
0
        public List <ReservationDTO> GetHotelReservations(int adminId, DateTime from, DateTime to, int roomId, int guestId)
        {
            HotelAdmin hotelAdmin   = context.HotelAdmins.Get(adminId);
            int        hotelId      = hotelAdmin.Hotel.Id;
            var        reservations = context.Reservations.GetAll()
                                      .Where(r => r.Room.HotelId == hotelId);

            if (from != null)
            {
                reservations = reservations.Where(r => r.StartDate >= from);
            }
            if (to != null && to > from)
            {
                reservations = reservations.Where(r => r.EndDate <= to);
            }
            if (roomId != 0)
            {
                reservations = reservations.Where(r => r.RoomId == roomId);
            }
            if (guestId != 0)
            {
                reservations = reservations.Where(r => r.GuestId == guestId);
            }
            return(reservations
                   .Select(r => r.ToReservationDTO())
                   .ToList());
        }
コード例 #2
0
        public async Task GetAccessByHotelAdmin(int userId, AccessByWorkerDTO workerDTO)
        {
            var room = context.Rooms.Get(workerDTO.RoomId);

            if (room == null)
            {
                throw new NotFoundException("No such room");
            }

            bool       hasPermission = false;
            HotelAdmin hotelAdmin    = context.HotelAdmins.Get(userId);

            hasPermission = room.HotelId == hotelAdmin.Hotel.Id;

            if (!hasPermission)
            {
                throw new PermissionException();
            }

            var  lastAccess = room.RoomAccesses.Where(r => r.UserId == userId).OrderBy(x => x.DateTime).LastOrDefault();
            bool isOpened   = (lastAccess == null) ? true : !lastAccess.IsOpen;
            var  access     = new RoomAccess()
            {
                DateTime      = DateTime.Now,
                IsOpen        = isOpened,
                RoomId        = workerDTO.RoomId,
                UserId        = userId,
                ReasonOfVisit = workerDTO.Reason
            };

            context.RoomAccesses.Create(access);
            await context.SaveAsync();
        }
コード例 #3
0
        public List <UserBlackListDTO> GetBlackLists(int userId)
        {
            HotelAdmin hotelAdmin = context.HotelAdmins.Get(userId);

            if (hotelAdmin == null)
            {
                throw new NotFoundException("No such hotel");
            }

            int hotelId       = hotelAdmin.Hotel.Id;
            var userBlackList = context.UserBlackLists.GetAll()
                                .Where(x => x.HotelId == hotelId).Select(x => x.ToUserBlackListDTO()).ToList();

            return(userBlackList);
        }
コード例 #4
0
        public async Task <UserBlackListDTO> CreateUserBlackList(UserBLCreateDTO userBLCreateDTO, int userId)
        {
            ValidationResults result = ModelValidator.IsValid(userBLCreateDTO);

            if (!result.Successed)
            {
                throw ValidationExceptionBuilder.BuildValidationException(result);
            }

            HotelAdmin hotelAdmin = context.HotelAdmins.Get(userId);

            if (hotelAdmin == null)
            {
                throw new NotFoundException("No such hotel");
            }

            Guest guest = context.Guests.Get(userBLCreateDTO.GuestId);

            if (guest == null)
            {
                throw new NotFoundException("No such user");
            }

            int hotelId   = hotelAdmin.Hotel.Id;
            var countInDB = context.UserBlackLists
                            .GetAll()
                            .Where(x => x.HotelId == hotelId &&
                                   x.GuestId == userBLCreateDTO.GuestId)
                            .Count();

            if (countInDB != 0)
            {
                throw new ValidationException("This user is already in a black list");
            }

            var blackList = userBLCreateDTO.ToUserBlackList();

            blackList.HotelId = hotelId;
            context.UserBlackLists.Create(blackList);
            await context.SaveAsync();

            return(blackList.ToUserBlackListDTO());
        }
コード例 #5
0
        public async Task <UserBlackListDTO> EditBlackList(UserBLEditDTO userBLEditDTO, int userId)
        {
            ValidationResults result = ModelValidator.IsValid(userBLEditDTO);

            if (!result.Successed)
            {
                throw ValidationExceptionBuilder.BuildValidationException(result);
            }

            HotelAdmin hotelAdmin = context.HotelAdmins.Get(userId);

            if (hotelAdmin == null)
            {
                throw new NotFoundException("No such hotel");
            }

            Guest guest = context.Guests.Get(userBLEditDTO.GuestId);

            if (guest == null)
            {
                throw new NotFoundException("No such user");
            }
            int hotelId       = hotelAdmin.Hotel.Id;
            var userBlackList = context.UserBlackLists.GetAll()
                                .Where(x => x.HotelId == hotelId &&
                                       x.GuestId == userBLEditDTO.GuestId).FirstOrDefault();

            if (userBlackList == null)
            {
                throw new NotFoundException("No such user in the hotel's black list");
            }
            userBlackList.Reason = userBLEditDTO.Reason;
            context.UserBlackLists.Update(userBlackList);
            await context.SaveAsync();

            return(userBlackList.ToUserBlackListDTO());
        }
コード例 #6
0
        public async Task <UserBlackListDTO> DeleteBlackList(int questId, int userId)
        {
            HotelAdmin hotelAdmin = context.HotelAdmins.Get(userId);

            if (hotelAdmin == null)
            {
                throw new NotFoundException("No such hotel");
            }

            int hotelId       = hotelAdmin.Hotel.Id;
            var userBlackList = context.UserBlackLists.GetAll()
                                .Where(x => x.HotelId == hotelId &&
                                       x.GuestId == questId).FirstOrDefault();

            if (userBlackList == null)
            {
                throw new NotFoundException("No such user in the hotel's black list");
            }

            context.UserBlackLists.Delete(userBlackList);
            await context.SaveAsync();

            return(userBlackList.ToUserBlackListDTO());
        }
コード例 #7
0
        private static async Task InitializeRolesAsync(UserManager <User> userManager, RoleManager <IdentityRole <int> > roleManager)
        {
            string adminEmail     = "*****@*****.**";
            string adminFirstName = "admin";
            string adminLastName  = "admin";
            string password       = "******";

            if (await roleManager.FindByNameAsync("admin") == null)
            {
                await roleManager.CreateAsync(new IdentityRole <int>("admin"));
            }
            if (await roleManager.FindByNameAsync("user") == null)
            {
                await roleManager.CreateAsync(new IdentityRole <int>("user"));
            }
            if (await roleManager.FindByNameAsync("hotel-admin") == null)
            {
                await roleManager.CreateAsync(new IdentityRole <int>("hotel-admin"));
            }
            if (await roleManager.FindByNameAsync("hotel-staff") == null)
            {
                await roleManager.CreateAsync(new IdentityRole <int>("hotel-staff"));
            }
            if (await userManager.FindByNameAsync(adminEmail) == null)
            {
                User admin = new User {
                    Email          = adminEmail,
                    UserName       = adminEmail,
                    EmailConfirmed = true
                };
                IdentityResult result = await userManager.CreateAsync(admin, password);

                if (result.Succeeded)
                {
                    await userManager.AddToRoleAsync(admin, "admin");
                }
            }
            string userEmail     = "*****@*****.**";
            string userFirstName = "user";
            string userLastName  = "user";

            if (await userManager.FindByNameAsync(userEmail) == null)
            {
                User user = new Guest
                {
                    Email          = userEmail,
                    UserName       = userEmail,
                    FirstName      = userFirstName,
                    LastName       = userLastName,
                    EmailConfirmed = true
                };
                IdentityResult result = await userManager.CreateAsync(user, password);

                if (result.Succeeded)
                {
                    await userManager.AddToRoleAsync(user, "user");
                }
            }
            string hotelEmail     = "*****@*****.**";
            string hotelFirstName = "hotel";
            string hotelLastName  = "hotel";

            if (await userManager.FindByNameAsync(hotelEmail) == null)
            {
                User hotel = new HotelAdmin
                {
                    Email          = hotelEmail,
                    UserName       = hotelEmail,
                    EmailConfirmed = true
                };
                IdentityResult result = await userManager.CreateAsync(hotel, password);

                if (result.Succeeded)
                {
                    await userManager.AddToRoleAsync(hotel, "hotel-admin");
                }
            }
            string staffEmail     = "*****@*****.**";
            string staffFirstName = "staff";
            string staffLastName  = "staff";

            if (await userManager.FindByNameAsync(staffEmail) == null)
            {
                User staff = new HotelStaff
                {
                    Email          = staffEmail,
                    UserName       = staffEmail,
                    FirstName      = staffFirstName,
                    LastName       = staffLastName,
                    EmailConfirmed = true
                };
                IdentityResult result = await userManager.CreateAsync(staff, password);

                if (result.Succeeded)
                {
                    await userManager.AddToRoleAsync(staff, "hotel-staff");
                }
            }
        }