Exemple #1
0
        /// <summary>
        /// Creert een order uit het gegeven viewmodel
        /// </summary>
        /// <param name="dto">Viewmodel met de nodige data voor het aanmaken van een nieuw order</param>
        /// <returns>Nieuw aangemaakt order</returns>
        /// <exception cref="EntityNotFoundException">Wordt gegooid wanneer een persoon in het order of in een orderline niet gevonden werd.</exception>
        public async Task <Order> CreateOrder(CreateOrderDTO dto, string userEmail)
        {
            var orderedBy = await _leidingRepository.FindByEmailAsync(userEmail);

            if (orderedBy == null)
            {
                throw new EntityNotFoundException($"Besteller '{userEmail}' werd niet gevonden.");
            }

            var newOrder = Order.Create(orderedBy);

            await _orderRepository.AddAsync(newOrder);

            foreach (var lineModel in dto.Orderlines)
            {
                var drank = await _drankRepository.FindByIdAsync(lineModel.DrankId);

                if (drank == null)
                {
                    throw new EntityNotFoundException($"Drank met id {lineModel.DrankId} werd niet gevonden");
                }

                var orderedFor = await _leidingRepository.FindByIdAsync(lineModel.OrderedForId);

                if (orderedFor == null)
                {
                    throw new EntityNotFoundException($"Persoon met id {lineModel.OrderedForId} werd niet gevonden.");
                }

                var personCanOrder = orderedFor.Tak.TabIsAllowed;

                if (!personCanOrder)
                {
                    throw new InvalidOperationException($"Leden van {orderedFor.Tak.Naam} kunnen geen gebruik maken van de Poef.");
                }

                var orderline = Orderline.Create(drank, orderedFor, newOrder, lineModel.Quantity);


                await _orderlineRepository.AddAsync(orderline);

                //Create a transaction for the submitted order
                var amount = orderline.DrinkPrice * orderline.Quantity;
                amount = -amount;
                var transaction = new Transaction(amount, $"{orderline.Quantity}x {orderline.Drank.Naam}");

                var account = await _accountRepository.FindAccountAsync(orderedFor.Id, AccountType.Tab);

                if (account == null)
                {
                    account = await _accountService.CreateAccountAsync(orderedFor.Id);
                }

                account.AddTransaction(transaction);
            }

            await _orderRepository.SaveChangesAsync();

            return(newOrder);
        }
Exemple #2
0
        public async Task Cant_Place_Order_If_OrderedFor_Section_Cant_Use_Tab()
        {
            var section = new Tak()
            {
                TabIsAllowed = false
            };
            var person = new Leiding()
            {
                Tak = section
            };
            var account = new Account(AccountType.Tab);

            person.Accounts = new List <Account> {
                account
            };


            var order     = Order.Create(person);
            var orderline = Orderline.Create(_defaultDrank, person, order, 1);

            var orderRepository = new Mock <IOrderRepository>();
            var leidingRepo     = new Mock <ILeidingRepository>();

            leidingRepo.Setup(x => x.FindByEmailAsync(It.IsAny <string>())).Returns(Task.FromResult <Leiding>(person));
            leidingRepo.Setup(x => x.FindByIdAsync(It.IsAny <int>())).Returns(Task.FromResult <Leiding>(person));
            var drankRepo = new Mock <IDrankRepository>();

            drankRepo.Setup(x => x.FindByIdAsync(It.IsAny <int>())).Returns(Task.FromResult <Drank>(_defaultDrank));
            var orderlineRepo = new Mock <IOrderlineRepository>();

            var accountRepo = new Mock <IAccountRepository>();

            accountRepo.Setup(x => x.FindAccountAsync(It.IsAny <int>(), AccountType.Tab))
            .Returns(Task.FromResult <Account>(account));

            var accountService = new Mock <IAccountService>();

            var service = new OrderService(orderRepository.Object, leidingRepo.Object, drankRepo.Object, orderlineRepo.Object, accountRepo.Object, accountService.Object);

            var orderlines = new List <CreateOrderlineDTO>();

            orderlines.Add(item: new CreateOrderlineDTO
            {
                DrankId      = 10,
                OrderedForId = 10,
                Quantity     = 10
            });

            var orderDto = new CreateOrderDTO()
            {
                Orderlines = orderlines
            };


            await Assert.ThrowsAsync <InvalidOperationException>(() => service.CreateOrder(orderDto, String.Empty));
        }
 public void OrderlineCantHaveAZeroQuantity() => Assert.Throws <ArgumentOutOfRangeException>(() => Orderline.Create(_drank, _leiding, _order, 0));