Пример #1
0
        public async Task <TransferOrderHeaderDto> CreateOrderHeaderAsync(TransferOrderHeaderDto dto)
        {
            dto.OrderTypeId = _context.OrderTypes.AsNoTracking().FirstOrDefault(o => o.Code == "LT").Id;
            dto.CreatedOn   = DateTime.Now;

            Order order = new Order();
            var   entry = await _context.AddAsync(order);

            entry.CurrentValues.SetValues(dto);
            await _context.SaveChangesAsync();

            dto.Id = order.Id;
            return(dto);
        }
Пример #2
0
        public async Task DeleteCustomer(int id)
        {
            var customer = await _context.Customers
                           .Include(c => c.Contacts)
                           .Include(c => c.Addresses)
                           .FirstOrDefaultAsync(c => c.Id == id);

            _context.Remove(customer);
            await _context.SaveChangesAsync();
        }
Пример #3
0
            public async Task <bool> Handle(Command request, CancellationToken cancellationToken)
            {
                var productBase = await _context.ProductBases.FindAsync(request.ProductBasicsDto.Id);

                _context.Entry(productBase).CurrentValues.SetValues(request.ProductBasicsDto);

                await _context.SaveChangesAsync();

                return(true);
            }
Пример #4
0
            public async Task <ProductBasicsDto> Handle(Command request, CancellationToken cancellationToken)
            {
                //var productBase = new ProductBase();
                //var entry = await _context.AddAsync(productBase);
                //entry.CurrentValues.SetValues(request.Dto);
                var productBase = _mapper.Map <ProductBase>(request.Dto);
                await _context.AddAsync(productBase);

                await _context.SaveChangesAsync();

                return(_mapper.Map <ProductBasicsDto>(productBase));
            }
Пример #5
0
        public async Task CreateCustomer(Customer customer)
        {
            if (customer.Id == 0)
            {
                customer.CreatedOn = DateTime.Now;

                if (customer.PayerAddress.Country != null)
                {
                    customer.PayerAddress.Country = null;
                }

                if (customer.PayerAddress.Id == 0)
                {
                    customer.Addresses.Add(customer.PayerAddress);
                }

                _context.Customers.Add(customer);
                await _context.SaveChangesAsync();
            }
        }
Пример #6
0
        public async Task UpdateCustomer(Customer customer)
        {
            if (customer.Id != 0)
            {
                var existingCustomer = _context.Customers
                                       .Include(c => c.Addresses)
                                       .Include(c => c.Contacts)
                                       .FirstOrDefault(c => c.Id == customer.Id);

                // Update Existing Customer
                // update modified datetime
                _context.Entry(existingCustomer).CurrentValues.SetValues(customer);

                // Delete Contacts
                foreach (var existingContact in existingCustomer.Contacts.ToList())
                {
                    if (!customer.Contacts.Any(c => c.Id == existingContact.Id))
                    {
                        _context.CustomerContacts.Remove(existingContact);
                    }
                }

                // Update or Insert Contacts
                foreach (var contact in customer.Contacts)
                {
                    var existingContact = existingCustomer.Contacts.FirstOrDefault(ec => ec.Id == contact.Id);
                    if (existingContact != null)
                    {
                        _context.Entry(existingContact).CurrentValues.SetValues(contact);
                    }
                    else
                    {
                        var newContact = new CustomerContact
                        {
                            Id     = contact.Id,
                            Name   = contact.Name,
                            Job    = contact.Job,
                            Mobile = contact.Mobile,
                            Phone  = contact.Phone,
                            Email  = contact.Email
                        };

                        existingCustomer.Contacts.Add(newContact);
                    }
                }

                // Delete Consignee Addresses
                foreach (var existingAddress in existingCustomer.Addresses.ToList())
                {
                    if (!customer.Addresses.Any(a => a.Id == existingAddress.Id))
                    {
                        _context.CustomerAddresses.Remove(existingAddress);
                    }
                }

                // Update and Insert Addresses
                foreach (var address in customer.Addresses)
                {
                    var existingAddress = existingCustomer.Addresses.FirstOrDefault(a => a.Id == address.Id);

                    if (existingAddress != null)
                    {
                        _context.Entry(existingAddress).CurrentValues.SetValues(address);
                    }
                    else
                    {
                        var newAddress = new CustomerAddress
                        {
                            Id            = address.Id,
                            CountryId     = address.CountryId,
                            County        = address.County,
                            City          = address.City,
                            Address       = address.Address,
                            PostCode      = address.PostCode,
                            ConsigneeName = address.ConsigneeName
                        };

                        existingCustomer.Addresses.Add(newAddress);
                    }
                }
                await _context.SaveChangesAsync();
            }
        }