示例#1
0
        public Bestelling PostBestelling(BestellingDTO bestellingDTO, int id)
        {
            var passagier     = _passagierRepository.GetbyId(id);
            var newBestelling = new Bestelling()
            {
                Afgehandeld = false, Passagier = passagier
            };

            _bestellingRepository.Add(newBestelling);
            _bestellingRepository.SaveChanges();

            var grouping = bestellingDTO.BestellingOpties.GroupBy(x => x.Id).Select(x => new { x.Key, Count = x.Count() });

            foreach (var bo in grouping)
            {
                var optie = _bestellingRepository.GetOptieById(bo.Key);

                _bestellingRepository.AddOptieToBestelling(new BestellingTK()
                {
                    BestellingId = newBestelling.Id, BestellingOptie = optie, Aantal = bo.Count
                });
            }
            _bestellingRepository.SaveChanges();
            return(newBestelling);
        }
        public BestellingDTO AddBestellingToCustomer(string custId)
        {
            var bestelling = new BestellingCreateUpdateDTO
            {
                Producten = new List <BestellingItem>(),
                Datum     = DateTime.Now,
                Klant     = _klantRepository.GetKlantByID(custId)
            };
            var winkelwagenGebruiker = _repositoryWinkelwagen.GetWinkelwagenByKlantId(custId);

            foreach (VerkoopItem item in winkelwagenGebruiker.Producten)
            {
                bestelling.Producten.Add(new BestellingItem()
                {
                    Aantal = item.Aantal, Product = item.Product
                });
            }
            bestelling.TotaalPrijs = _costCalculator.CalculateCost(winkelwagenGebruiker);
            var newBestelling     = _bestellingMapper.MapToModel(bestelling);
            var createdBestelling = _repositoryBestelling.AddBestellingToCustomer(newBestelling);

            winkelwagenGebruiker.TotaalPrijs = 0.0;
            winkelwagenGebruiker.Producten   = new List <WinkelwagenItem>();

            try
            {
                _repositoryBestelling.SaveChanges();
            }
            catch (DbUpdateException)
            {
                return(null);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            return(_bestellingMapper.MapToDTO(createdBestelling));
        }