Beispiel #1
0
        public IActionResult PlaceOrder(NewLeaseDto leaseDto)
        {
            var operationStatus = _leaseBookService.LeaseBook(leaseDto);

            if (!operationStatus.Success)
            {
                return(BadRequest(operationStatus.ErrorMessage));
            }

            return(Ok());
        }
        public OperationStatus LeaseBook(NewLeaseDto leaseDto)
        {
            try
            {
                if (_itemService.GetAvailableItemsQty(leaseDto.BookId) > 0)
                {
                    var reader = _readerStore.GetReaderById(leaseDto.ReaderId);
                    reader.LeaseBook(leaseDto.BookId);

                    _itemService.DecreaseItemAvailability(leaseDto.BookId);

                    _readerStore.SaveReader(reader);

                    var events = reader.FlushEvents();
                    foreach (var domainEvent in events)
                    {
                        _eventPublisher.Publish(domainEvent);
                    }

                    return(OperationStatus.CompletedSuccessfully);
                }

                return(new OperationStatus()
                {
                    Success = false,
                    ErrorMessage = "Requested book is not available"
                });
            }
            catch (MaxConcurrentLeasesExceeded e)
            {
                return(new OperationStatus()
                {
                    Success = false,
                    ErrorMessage = e.Message
                });
            }
        }