Example #1
0
        public async Task <Contact> Save(Contact contact, CancellationToken cancellationToken)
        {
            var dbContact        = _mapper.Map(contact);
            var existingContacts = await _dataLayer.GetContacts(new[] { (Guid)contact.Id }, cancellationToken);

            if (!existingContacts.Any())
            {
                await _dataLayer.CreateContact(dbContact, cancellationToken);
            }
            else
            {
                await _dataLayer.UpdateContact(dbContact, cancellationToken);
            }

            var createdOrUpdatedContact = (await _dataLayer.GetContacts(new[] { (Guid)contact.Id }, cancellationToken))
                                          .Single();
            var domainContact = _mapper.Map(createdOrUpdatedContact);

            return(domainContact);
        }
        public async Task <CoffeeRoastingEvent> Map(Domain.CoffeeRoastingEvent domainEvent, CancellationToken cancellationToken)
        {
            var contacts = await _contactDataLayer.GetContacts(domainEvent.NotifiedContacts.Select(id => (Guid)id), cancellationToken);

            var @event = new CoffeeRoastingEvent
            {
                Id          = domainEvent.Id,
                Date        = domainEvent.RoastDate.ToDateTimeUnspecified(),
                OrderByDate = domainEvent.OrderByDate.ToDateTimeUnspecified(),
                Name        = domainEvent.Name,
                IsActive    = domainEvent.IsActive,
                Contacts    = contacts,
                Orders      = domainEvent.Orders.Select(order => new Order
                {
                    Id               = order.Id,
                    ContactId        = order.Contact,
                    CreatedTimestamp = order.ReceivedTimestamp.ToDateTimeOffset(),
                    IsConfirmed      = order.IsConfirmed,
                    Invoice          = new Invoice
                    {
                        Id            = order.Invoice.Id,
                        OrderId       = order.Id,
                        Amount        = order.Invoice.Amount,
                        IsPaid        = order.Invoice.IsPaid,
                        PaymentMethod = order.Invoice.PaymentMethod,
                    },
                    OrderCoffees = order.Details.Select(x => new OrderCoffee
                    {
                        Quantity = x.Value,
                        CoffeeId = x.Key,
                        OrderId  = order.Id,
                    }).ToList(),
                }).ToList(),
                CoffeeRoastingEventCoffees = domainEvent.OfferedCoffees.Select(x => new CoffeeRoastingEventCoffee
                {
                    Label    = x.Key,
                    CoffeeId = x.Value.Id,
                    CoffeeRoastingEventId = domainEvent.Id,
                }).ToList(),
            };

            return(@event);
        }