public async Task PointsEarned(Guid id, double points, MembershipPointsType type)
        {
            var entity = await GenericEntityFactory <Membership> .GetEntityAsync(_simpleEventStoreDbContext, id.ToString());

            entity.PointsEarned(points, type);

            await _simpleEventStoreDbContext.SavePendingEventsAsync(entity.PendingEvents, 1, "Sample");

            await _eventBusService.Publish(entity.PendingEvents);
        }
        public async Task Handle(CustomerRegisteredEvent notification, CancellationToken cancellationToken)
        {
            var membership = new Membership(notification.Id);
            await _eventStoreDbContext.SavePendingEventsAsync(membership.PendingEvents, 1, "Sample");

            await _eventBusService.Publish(membership.PendingEvents);
        }
        public async Task Handle(MembershipPointsEarnedEvent notification, CancellationToken cancellationToken)
        {
            var membership = await _readModelDbContext.Memberships
                             .Include(m => m.Points)
                             .FirstOrDefaultAsync(m => m.Id == notification.Id);

            // this is require, as otherwise, the place where it save to the read mokdel will throw some exceptions
            _readModelDbContext.Entry(membership).State = EntityState.Detached;

            var newPoints       = membership.TotalPoints;
            var newPointsPer100 = (int)(newPoints / 100);

            var previousPoints       = membership.TotalPoints - notification.Amount;
            var previousPointsPer100 = (int)(previousPoints / 100);

            var rewardPointsEarned = newPointsPer100 - previousPointsPer100;

            if (rewardPointsEarned > 0)
            {
                var rewardType = (rewardPointsEarned == 1)
                   ? RewardType.GiftVoucher
                   : RewardType.FreeMeal;
                var entity = new Reward(membership.CustomerId, rewardType);
                await _simpleEventStoreDbContext.SavePendingEventsAsync(entity.PendingEvents, 1, "Sample");

                await _eventBusService.Publish(entity.PendingEvents);
            }
        }
Пример #4
0
        public async Task <bool> RegisterCustomerAsync(string username, string firstname, string lastname, string email)
        {
            // pretend to run some sort of validation here.
            // username must be unique.
            var isUsernameExists = _readModelDbContext.Customers.Any(c => c.Username == username);

            if (isUsernameExists)
            {
                return(false);
            }

            var customer = new Customer(Guid.NewGuid(), username, firstname, lastname, email);
            await _eventStoreDbContext.SavePendingEventsAsync(customer.PendingEvents, 1, "Sample");

            await _eventBusService.Publish(customer.PendingEvents);

            return(true);
        }