Пример #1
0
        public async Task GetQuote_ReturnOkDiscount20()
        {
            using (var context = new BeerContext(ContextOptions))
            {
                var service      = new WholesalerService(context);
                var quoteCommand = new GetQuoteCommand
                {
                    CommandLines = new List <BeerQuantity>()
                    {
                        new BeerQuantity {
                            BeerId = 1, Quantity = 12
                        },
                        new BeerQuantity {
                            BeerId = 2, Quantity = 12
                        }
                    }
                };

                var quote = await service.GetQuote(1, quoteCommand);

                Assert.Equal(12, quote.Discount);
                Assert.Equal(60, quote.Total);
                Assert.Equal(48, quote.Price);
            }
        }
Пример #2
0
        public async Task GetQuote_NotOk_ListIsNull()
        {
            using (var context = new BeerContext(ContextOptions))
            {
                var service      = new WholesalerService(context);
                var quoteCommand = new GetQuoteCommand
                {
                };

                await Assert.ThrowsAsync <CustomBadRequestException>(() => service.GetQuote(1, quoteCommand));
            }
        }
Пример #3
0
        public string this[string columnName]
        {
            get
            {
                var message = string.Empty;
                switch (columnName)
                {
                case nameof(VolatilityInPercent):
                    if (VolatilityInPercent <= 0)
                    {
                        message = "Volatility must be strictly positive";
                    }

                    break;

                case nameof(UnderlyingPrice):
                    if (UnderlyingPrice <= 0)
                    {
                        message = "Price must be strictly positive";
                    }

                    break;

                case nameof(Strike):
                    if (Strike <= 0)
                    {
                        message = "Price must be strictly positive";
                    }

                    break;

                case nameof(DaysUntilExpiration):
                    if (DaysUntilExpiration <= 0)
                    {
                        message = "Time must be strictly positive";
                    }

                    break;

                case nameof(RiskFreeInterestRateInPercent):
                    if (RiskFreeInterestRateInPercent < 0)
                    {
                        message = "Interest Rate must be positive";
                    }

                    break;
                }

                GetQuoteCommand.RaiseCanExecuteChanged();
                return(message);
            }
        }
Пример #4
0
        public async Task GetQuote_NotOk_EmptyList()
        {
            using (var context = new BeerContext(ContextOptions))
            {
                var service      = new WholesalerService(context);
                var quoteCommand = new GetQuoteCommand
                {
                    CommandLines = new List <BeerQuantity>()
                };

                await Assert.ThrowsAsync <CustomBadRequestException>(() => service.GetQuote(1, quoteCommand));
            }
        }
Пример #5
0
        public async Task GetQuote_NotOk_WholesalerNotFound()
        {
            using (var context = new BeerContext(ContextOptions))
            {
                var service      = new WholesalerService(context);
                var quoteCommand = new GetQuoteCommand
                {
                    CommandLines = new List <BeerQuantity>()
                    {
                        new BeerQuantity {
                            BeerId = 1, Quantity = 3
                        },
                        new BeerQuantity {
                            BeerId = 2, Quantity = 4
                        }
                    }
                };

                await Assert.ThrowsAsync <CustomBadRequestException>(() => service.GetQuote(5, quoteCommand));
            }
        }
Пример #6
0
        public async Task <ActionResult <GetQuoteViewModel> > GetQuote(int wholesalerId, GetQuoteCommand command)
        {
            var quote = await _wholesalerService.GetQuote(wholesalerId, command);

            var quoteViewModel = _mapper.Map <Quote, GetQuoteViewModel.Quote>(quote);

            return(Ok(quoteViewModel));
        }
Пример #7
0
 public RestFunnyQuotesClient(GetQuoteCommand getQuoteCommand, IDiscoveryClient discoveryClient)
 {
     _getQuoteCommand = getQuoteCommand;
     _discoveryClient = discoveryClient;
 }
Пример #8
0
        // Dans l'enonce, "Le brasseur doit exister" => que quoi?
        public async Task <Quote> GetQuote(int id, GetQuoteCommand command)
        {
            // Empty command
            if (command.CommandLines == null)
            {
                throw new CustomBadRequestException("Item list cannot be null");
            }

            if (command.CommandLines.Count() <= 0)
            {
                throw new CustomBadRequestException("Item list cannot be null");
            }

            // Duplicates
            var totalDistinct = command.CommandLines.Select(c => c.BeerId).Distinct().Count();

            if (command.CommandLines.Count() != totalDistinct)
            {
                throw new CustomBadRequestException("Item list cannot contains duplicates");
            }

            var wholesaler = await _beerContext.Wholesalers
                             .Include(w => w.WholesalerBeers)
                             .ThenInclude(wb => wb.Beer)
                             .SingleOrDefaultAsync(w => w.Id == id);

            if (wholesaler == null)
            {
                throw new CustomBadRequestException($"Wholesaler does not exist");
            }

            // Generate quote
            var quote = new Quote();

            foreach (var item in command.CommandLines)
            {
                var wb = wholesaler.WholesalerBeers.SingleOrDefault(wb => wb.BeerId == item.BeerId);
                if (wb == null)
                {
                    throw new CustomBadRequestException($"Beer with id: {item.BeerId} is not sell by this wholesaler");
                }

                if (wb.Stock < item.Quantity)
                {
                    throw new CustomBadRequestException($"Not enough stock for beer {item.BeerId}");
                }

                quote.Items.Add(new CommandLine
                {
                    Beer     = wb.Beer,
                    Quantity = item.Quantity
                });
                quote.Total += item.Quantity * wb.Beer.Price;
            }

            // Apply discount
            quote.Total    = Math.Round(quote.Total, 2);
            quote.Discount = quote.GetQuantityDiscount();
            quote.Price    = quote.Total - quote.Discount;

            return(quote);
        }
Пример #9
0
 public RestFunnyQuotesClient(GetQuoteCommand getQuoteCommand)
 {
     _getQuoteCommand = getQuoteCommand;
 }