public void AddUser(UserViewModel user) { using (ISession session = NHibernateHelper.OpenSession()) using (ITransaction transaction = session.BeginTransaction()) { _userRepository = new Repository <User>(session); _roleRepository = new Repository <Role>(session); _rentRepository = new Repository <Rent>(session); if (_userRepository.All().Any(x => x.Email.ToLower() == user.Email.ToLower() && x.Id != user.Id)) { throw new ApplicationException("Existing user"); } //user.Rents = new List<RentViewModel>(); //foreach (var rent in _rentRepository.All().Where(x => x.User.Id == user.Id).ToList()) // user.Rents.Add(rent.toRentViewModel()); //var userDomain = user.toUserDomainModel(); if (user.Role == null) { throw new ApplicationException("Role must be selected"); } var role = _roleRepository.FindBy(user.Role.Id); if (role == null) { throw new ApplicationException("Role does not exist"); } var userDomain = _userRepository.FindBy(user.Id) ?? new User(); userDomain.FirstName = user.FirstName; userDomain.LastName = user.LastName; userDomain.Password = user.Password; userDomain.PhoneNumber = user.PhoneNumber; userDomain.Email = user.Email; userDomain.Embg = user.Embg; userDomain.Role = role; _userRepository.AddOrUpdate(userDomain); MailProvider mailProvider = new MailProvider(); mailProvider.SendMailToUser(userDomain); transaction.Commit(); } }
public bool makeAReservation(ReservationViewModel reservation) { using (ISession session = NHibernateHelper.OpenSession()) using (ITransaction transaction = session.BeginTransaction()) { _rentRepository = new Repository <Rent>(session); _userRepository = new Repository <User>(session); _roomRepository = new Repository <Room>(session); Rent rent = new Rent(); rent.User = _userRepository.FindBy(reservation.UserId); rent.DateEnd = reservation.DateEnd; rent.DateStart = reservation.DateStart; rent.Room = _roomRepository.FindBy(reservation.RoomId); var usersRent = _rentRepository.All().Where(x => x.User.Id == reservation.UserId).ToList(); bool flag = true; foreach (var tmpRent in usersRent) { if (InValidRent(tmpRent.DateStart.Value, tmpRent.DateEnd.Value, reservation.DateStart, reservation.DateEnd)) { flag = false; } } if (flag) { _rentRepository.Add(rent); transaction.Commit(); MailProvider mailProvider = new MailProvider(); mailProvider.SuccessReservation(rent); } return(flag); } }