Exemplo n.º 1
0
        public async Task <BookingViewModel> Create(BookingViewModel model)
        {
            var booking = _mapper.Map <BookingViewModel, BookingModel>(model);
            var hasUser = await _userRepository.AnyAsync(w => w.Id == booking.UserId);

            var isReserved = await _bookingRepository
                             .AnyAsync(b => ((b.Start <= booking.End && b.End >= booking.End) ||
                                             (b.Start <= booking.Start && b.End >= booking.Start) ||
                                             (booking.Start <b.Start && booking.End> b.End)) &&
                                       booking.ApartmentId == b.ApartmentId);

            if (!hasUser)
            {
                throw new Exception("User not found");
            }
            if (isReserved)
            {
                throw new Exception("Apartment is reserved");
            }

            await _bookingRepository.Create(booking);

            return(_mapper.Map <BookingModel, BookingViewModel>(booking));
        }