public async Task <RoomDTO> CreateRoom(RoomCreateDTO roomCreateDTO, int userID) { ValidationResults result = ModelValidator.IsValid(roomCreateDTO); if (!result.Successed) { throw ValidationExceptionBuilder.BuildValidationException(result); } var hotel = context.Hotels.Get(roomCreateDTO.HotelId); if (hotel == null) { throw new NotFoundException("No such hotel"); } if (hotel.HotelAdminId != userID) { throw new PermissionException(); } var isExistsWithNumber = hotel.Rooms.Where(r => r.NumberInHotel == roomCreateDTO.NumberInHotel).Count() != 0; if (isExistsWithNumber) { throw new ValidationException("Hotel room with such number already exists"); } var room = roomCreateDTO.ToRoom(); context.Rooms.Create(room); await context.SaveAsync(); return(room.ToRoomDTO()); }
public async Task RegisterUserAsync <TUser>(TUser user, string password, string role) where TUser : User { ValidationResults result = ModelValidator.IsValid(user); if (!result.Successed) { throw ValidationExceptionBuilder.BuildValidationException(result); } await CheckIfThePasswordIsValid(password); await CheckIfTheUserDoesNotExist(user); var isCreated = await _userManager.CreateAsync(user, password); if (!isCreated.Succeeded) { throw new DatabaseException("Cann't create new user"); } var isAddedToRole = await _userManager.AddToRoleAsync(user, role); if (!isAddedToRole.Succeeded) { throw new DatabaseException($"Cann't add new user to {role}'s role"); } }
public async Task RegisterAsync(RegistrationDTO registrationDTO) { ValidationResults result = ModelValidator.IsValid(registrationDTO); if (!result.Successed) { throw ValidationExceptionBuilder.BuildValidationException(result); } User newUser = registrationDTO.ToUser(); string password = registrationDTO.Password; await CheckIfThePasswordIsValid(password); await CheckIfTheUserDoesNotExist(newUser); var isCreated = await _userManager.CreateAsync(newUser, password); if (!isCreated.Succeeded) { throw new DBException("Cann't create new user"); } var isAddedToRole = await _userManager.AddToRoleAsync(newUser, "personal"); if (!isAddedToRole.Succeeded) { throw new DBException("Cann't add new user to personal role"); } var newBoard = new Board { Title = "Personal board", Date = DateTimeOffset.UtcNow, UserId = newUser.Id }; db.Boards.Add(newBoard); await db.SaveChangesAsync(); var plansList = new CardsList { Type = "PlanCard", BoardId = newBoard.Id }; var habitsList = new CardsList { Type = "HabitCard", BoardId = newBoard.Id }; db.CardsLists.AddRange(plansList, habitsList); await db.SaveChangesAsync(); }
public async Task <ReservationDTO> CreateReservation(ReservationCreateDTO reservationCreateDTO, int userId) { ValidationResults result = ModelValidator.IsValid(reservationCreateDTO); if (!result.Successed) { throw ValidationExceptionBuilder.BuildValidationException(result); } var room = context.Rooms.Get(reservationCreateDTO.RoomId); if (room == null) { throw new NotFoundException("No such room"); } Guest guest = context.Guests.Get(userId); if (context.UserBlackLists.GetAll().Where(x => x.GuestId == userId && x.HotelId == room.HotelId).Count() != 0) { throw new ValidationException("You are in a hotel's black list"); } if (reservationCreateDTO.EndDate < reservationCreateDTO.StartDate) { throw new ValidationException("Reservation end date must be greater than a start date"); } if (reservationCreateDTO.EndDate - reservationCreateDTO.StartDate > TimeSpan.FromDays(20)) { throw new ValidationException("Reservation duration can't be greater than a 20 days"); } bool canBeReserved = room.Reservations .Where(r => r.EndDate <reservationCreateDTO.StartDate || r.StartDate> reservationCreateDTO.EndDate) .Count() == room.Reservations.Count(); if (!canBeReserved) { throw new ValidationException("Room is not available is this time"); } var reservation = reservationCreateDTO.ToReservation(); reservation.GuestId = userId; context.Reservations.Create(reservation); await context.SaveAsync(); return(reservation.ToReservationDTO()); }
public async Task RegisterStaffAsync(RegisterStaffDTO registrationDTO, int userId) { var hotelAdmin = context.HotelAdmins.Get(userId); if (hotelAdmin == null) { throw new NotFoundException("No such hotel admin"); } int hotelId = hotelAdmin.Hotel.Id; ValidationResults result = ModelValidator.IsValid(registrationDTO); if (!result.Successed) { throw ValidationExceptionBuilder.BuildValidationException(result); } User newUser = new HotelStaff() { FirstName = registrationDTO.FirstName, LastName = registrationDTO.LastName, Email = registrationDTO.Email, UserName = registrationDTO.Email, Category = registrationDTO.Category }; string password = registrationDTO.Password; await CheckIfThePasswordIsValid(password); await CheckIfTheUserDoesNotExist(newUser); var isCreated = await _userManager.CreateAsync(newUser, password); if (!isCreated.Succeeded) { throw new DBException("Cann't create new user"); } var isAddedToRole = await _userManager.AddToRoleAsync(newUser, "hotel-staff"); if (!isAddedToRole.Succeeded) { throw new DBException("Cann't add new user to hotel-staff's role"); } var code = await _userManager.GenerateEmailConfirmationTokenAsync(newUser); string message = "Ваш код пiдтвердження: " + code; await EmailService.SendEmailAsync(newUser.Email, "HotelLocker пiдтвердження паролю", message); }
public async Task <HotelDTO> EditHotel(HotelDTO hotelDTO, int userId) { ValidationResults result = ModelValidator.IsValid(hotelDTO); if (!result.Successed) { throw ValidationExceptionBuilder.BuildValidationException(result); } var hotel = context.Hotels.Get(hotelDTO.Id); if (hotel == null) { throw new NotFoundException("No such hotel"); } if (hotel.HotelAdminId != userId) { throw new PermissionException(); } if (hotelDTO.Name != null) { hotel.Name = hotelDTO.Name; } if (hotelDTO.Stars != 0) { hotel.Stars = hotelDTO.Stars; } if (hotelDTO.Country != null) { hotel.Country = hotelDTO.Country; } if (hotelDTO.City != null) { hotel.City = hotelDTO.City; } if (hotelDTO.Address != null) { hotel.Address = hotelDTO.Address; } context.Hotels.Update(hotel); await context.SaveAsync(); return(hotel.ToHotelDTO()); }
public async Task <TokenResponse> LoginAsync(LoginDTO loginDTO) { var result = ModelValidator.IsValid(loginDTO); if (!result.Successed) { throw ValidationExceptionBuilder.BuildValidationException(result); } var foundUser = await FindUserByEmail(loginDTO.Email); await CheckIfThePasswordIsCorrect(foundUser, loginDTO.Password); var userRole = await _userManager.GetRolesAsync(foundUser); return(GenerateJwtToken(foundUser, userRole)); }
public async Task <HotelDTO> CreateHotel(HotelCreateDTO hotelCreateDTO) { ValidationResults result = ModelValidator.IsValid(hotelCreateDTO); if (!result.Successed) { throw ValidationExceptionBuilder.BuildValidationException(result); } var hotelAdmin = hotelCreateDTO.GetHotelAdminInfo(); string password = GeneratePassword(); await CheckIfThePasswordIsValid(password); await CheckIfTheUserDoesNotExist(hotelAdmin); hotelAdmin.EmailConfirmed = true; var isCreated = await _userManager.CreateAsync(hotelAdmin, password); if (!isCreated.Succeeded) { throw new DBException("Cann't create new hotel admin"); } var isAddedToRole = await _userManager.AddToRoleAsync(hotelAdmin, "hotel-admin"); if (!isAddedToRole.Succeeded) { throw new DBException("Cann't add new user to hotel admin's role"); } var newHotelAdmin = await _userManager.FindByEmailAsync(hotelAdmin.Email); var hotel = hotelCreateDTO.ToHotel(); hotel.HotelAdminId = newHotelAdmin.Id; context.Hotels.Create(hotel); await context.SaveAsync(); string message = "Ваш пароль у системi: " + password; await EmailService.SendEmailAsync(newHotelAdmin.Email, "HotelLocker", message); return(hotel.ToHotelDTO()); }
public async Task <RoomDTO> EditRoom(RoomEditDTO roomDTO, int userId) { ValidationResults result = ModelValidator.IsValid(roomDTO); if (!result.Successed) { throw ValidationExceptionBuilder.BuildValidationException(result); } var room = context.Rooms.Get(roomDTO.Id); if (room == null) { throw new NotFoundException("No such room"); } if (room.Hotel.HotelAdminId != userId) { throw new PermissionException(); } if (roomDTO.NumberInHotel != 0) { room.NumberInHotel = roomDTO.NumberInHotel; } if (roomDTO.Category != 0) { room.Category = roomDTO.Category; } if (roomDTO.Beds != 0) { room.Beds = roomDTO.Beds; } if (roomDTO.Price != 0) { room.Price = roomDTO.Price; } context.Rooms.Update(room); await context.SaveAsync(); return(room.ToRoomDTO()); }
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()); }
public async Task RegisterAsync(RegistrationDTO registrationDTO) { ValidationResults result = ModelValidator.IsValid(registrationDTO); if (!result.Successed) { throw ValidationExceptionBuilder.BuildValidationException(result); } User newUser = new Guest() { FirstName = registrationDTO.FirstName, LastName = registrationDTO.LastName, Email = registrationDTO.Email, UserName = registrationDTO.Email, Passport = registrationDTO.Passport }; string password = registrationDTO.Password; await CheckIfThePasswordIsValid(password); await CheckIfTheUserDoesNotExist(newUser); var isCreated = await _userManager.CreateAsync(newUser, password); if (!isCreated.Succeeded) { throw new DBException("Cann't create new user"); } var isAddedToRole = await _userManager.AddToRoleAsync(newUser, "user"); if (!isAddedToRole.Succeeded) { throw new DBException("Cann't add new user to user's role"); } var code = await _userManager.GenerateEmailConfirmationTokenAsync(newUser); string message = "Ваш код пiдтвердження: " + code; await EmailService.SendEmailAsync(newUser.Email, "HotelLocker пiдтвердження паролю", message); }
public async Task <ReservationDTO> EditReservation(ReservationEditDTO reservationEditDTO, int userId) { ValidationResults result = ModelValidator.IsValid(reservationEditDTO); if (!result.Successed) { throw ValidationExceptionBuilder.BuildValidationException(result); } var reservation = context.Reservations.Get(reservationEditDTO.Id); if (reservation == null) { throw new NotFoundException("No such reservation"); } if (reservation.GuestId != userId) { throw new PermissionException(); } if (reservationEditDTO.StartDate != null && reservationEditDTO.EndDate != null) { bool canBeReserved = context.Reservations.GetAll() .Where(r => r.RoomId == reservation.RoomId && r.Id != reservation.Id && (r.StartDate <reservationEditDTO.StartDate && r.EndDate> reservationEditDTO.StartDate) || (r.StartDate <reservationEditDTO.EndDate && r.EndDate> reservationEditDTO.EndDate)) .Count() == 0; if (!canBeReserved) { throw new ValidationException("Room is not available is this time"); } reservation.StartDate = reservationEditDTO.StartDate; reservation.EndDate = reservationEditDTO.EndDate; } if (!string.IsNullOrEmpty(reservationEditDTO.AdditionalInfo)) { reservation.AdditionalInfo = reservationEditDTO.AdditionalInfo; } context.Reservations.Update(reservation); await context.SaveAsync(); return(reservation.ToReservationDTO()); }
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()); }