コード例 #1
0
        public void BookReservationAsync_WhenUserOverBookedException_ContentIsExpected_Test()
        {
            // Arrange
            var header = new Dictionary <string, string>
            {
                { "UserId", "1" }
            };

            var criteria = new BookReservationCriteria
            {
                ReservationDateTime = DateTime.Now,
                RestaurantId        = 1,
                UserIds             = new List <long> {
                    1, 2
                }
            };
            var message    = string.Empty;
            var mockFacade = new Mock <IReservationFacade>();

            mockFacade.Setup(facade => facade.BookReservationAsync(It.IsAny <BookReservationCriteria>(), It.IsAny <CancellationToken>())).ThrowsAsync(new UserOverBookedException("ex"));
            var controller = GetControllerInstance(mockFacade.Object, header);

            // Act
            var contentResult = controller.GetRestaurantAsync(criteria, CancellationToken.None);

            // Assert
            Assert.IsType <ContentResult>(contentResult.Result);
        }
コード例 #2
0
        public async Task <ActionResult> GetRestaurantAsync([FromBody] BookReservationCriteria criteria, CancellationToken cancellationToken)
        {
            try
            {
                var message = await _reservationFacade.BookReservationAsync(criteria, cancellationToken);

                return(Ok(message));
            }
            catch (UserNotRegisteredException ex)
            {
                return(Content(ex.Message));
            }
            catch (UserOverBookedException ex)
            {
                return(Content(ex.Message));
            }
            catch (TableNotFoundException ex)
            {
                return(Content(ex.Message));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex));
            }
        }
コード例 #3
0
        public async Task <string> BookReservationAsync(BookReservationCriteria criteria, CancellationToken cancellationToken)
        {
            await CheckOverBookedUser(criteria.UserIds, cancellationToken);

            // if users are not overbooked, then we create the reservation
            // first we need to find a table in the selected restaurant
            var tables = _dbContext.GetAvailableTablesByRestaurantIds(new List <long> {
                criteria.RestaurantId
            }, criteria.UserIds.Count);

            if (tables == null || !tables.Any())
            {
                throw new TableNotFoundException("There are not available tables at the moment. Please try again later.");
            }

            var reservation = new Reservation
            {
                DateTime      = DateTime.Now,
                MarkForDelete = false,
                RestaurantId  = criteria.RestaurantId,
                TableId       = tables.FirstOrDefault().Id
            };

            using (var transaction = _dbContext.Database.BeginTransaction())
            {
                try
                {
                    _dbContext.Reservations.Add(reservation);
                    await _dbContext.SaveChangesAsync(cancellationToken);

                    var affectedUsers = await _dbContext.Users
                                        .Where(user => criteria.UserIds.Contains(user.Id))
                                        .ToListAsync(cancellationToken);

                    foreach (var user in affectedUsers)
                    {
                        user.ReservationId = reservation.Id;
                    }

                    _dbContext.UpdateRange(affectedUsers);
                    await _dbContext.SaveChangesAsync(cancellationToken);

                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }
            }

            return(string.Format("Reservation at restaurant {0} at {1} was successfully booked. Your reservation id is {2}, if you want to cancel, you'll need it :)", reservation.RestaurantId, reservation.DateTime, reservation.Id));
        }
コード例 #4
0
 public async Task <string> BookReservationAsync(BookReservationCriteria criteria, CancellationToken cancellationToken)
 {
     return(await _reservationManager.BookReservationAsync(criteria, cancellationToken));
 }