private async Task <bool> HandleAsync(CustomerUpdated e) { Log.Information("Updated Customer: {CustomerId}, {Name}, {TelephoneNumber}", e.CustomerId, e.Name, e.TelephoneNumber); try { using (var transaction = await _dbContext.Database.BeginTransactionAsync()) { var customer = await _dbContext.Customers.FirstAsync(c => c.CustomerId == e.CustomerId); customer.CustomerId = e.CustomerId; customer.Name = e.Name; customer.TelephoneNumber = e.TelephoneNumber; _dbContext.Customers.Update(customer); await _dbContext.SaveChangesAsync(); await transaction.CommitAsync(); } } catch (DbUpdateException) { Log.Warning("Skipped adding customer with customer id {CustomerId}.", e.CustomerId); } return(true); }
/** * Edit given customer */ public bool Edit(Customer responseCustomer) { //validate if (responseCustomer.Id == 0 || string.IsNullOrEmpty(responseCustomer.FirstName) || string.IsNullOrEmpty(responseCustomer.LastName) || string.IsNullOrEmpty(responseCustomer.Password) || string.IsNullOrEmpty(responseCustomer.Email)) { Debug.WriteLine("ID, First name, Last name, password and email cannot be null"); return(false); } using (var context = new ShopDbContext()) { //Get Customer. If it doesn't exist return null Customer oldCustomer = context.Customers.Find(responseCustomer.Id); if (oldCustomer == null) { Debug.WriteLine("Customer with given ID doesn't exist"); return(false); } var config = new MapperConfiguration(cfg => cfg.CreateMap <Customer, CustomerUpdated>()); var mapper = config.CreateMapper(); CustomerUpdated updatedCustomer = mapper.Map <CustomerUpdated>(responseCustomer); context.Entry(oldCustomer).CurrentValues.SetValues(updatedCustomer); context.SaveChanges(); return(true); } }
public void WhenApplyingCustomerUpdatedEvent_ThenShouldHaveETagSet() { var customer = new Customer(); var customerUpdated = new CustomerUpdated(customer); customer.Apply(customerUpdated); Assert.Equal(customerUpdated.EntityETag, customer.ETag); }
public async Task Handle(CustomerUpdated notification, CancellationToken cancellationToken) { await _context.Update(f => f.Id == notification.Id, new Customer() { Id = notification.Id, Honorific = notification.Honorific, FirstName = notification.FirstName, LastName = notification.LastName }); }
public async Task Handle(CustomerUpdated notification, CancellationToken cancellationToken) { await _context.Update(f => f.Id == notification.Id, new CustomerOrders { Id = notification.Id, FirstName = notification.FirstName, LastName = notification.LastName, Honorific = notification.Honorific, Orders = new List <CustomerOrder>() }); }
private async Task HandleAsync(CustomerUpdated cu) { Customer customer = new Customer { CustomerId = cu.CustomerId, Name = cu.Name, TelephoneNumber = cu.TelephoneNumber, EmailAddress = cu.EmailAddress }; Log.Information("Customer profile updated: {Id}, {Name}, {TelephoneNumber}, {Email}", customer.CustomerId, customer.Name, customer.TelephoneNumber, customer.EmailAddress); await _repo.UpdateCustomerAsync(customer); }
Customer IEntityBuilder <Customer, CustomerUpdated> .Apply(Customer entity, CustomerUpdated @event) { if (@event.Name != null) { entity.Name = @event.Name; } if (@event.Owner != null) { entity.Owner = @event.Owner; } entity.SequenceNumber = @event.SequenceNumber; return(entity); }
public async Task NotificationManager_HandleMessageAsync_Should_Handle_Customer_Updated_EventAsync() { var mockMessageHandler = new Mock <IMessageHandler>(); var mockRepository = new Mock <INotificationRepository>(); var mockEmailNotifier = new Mock <IEmailNotifier>(); var customer = new CustomerUpdated(Guid.NewGuid(), Guid.NewGuid().ToString(), "*****@*****.**", "John Doe", "0798745715"); mockRepository .Setup(x => x.UpdateCustomerAsync(It.IsAny <Customer>())) .Returns(Task.CompletedTask); var notifyManager = new NotificationManager(mockMessageHandler.Object, mockRepository.Object, mockEmailNotifier.Object); await notifyManager.HandleMessageAsync("CustomerUpdated", JsonConvert.SerializeObject(customer)); mockRepository.Verify(x => x.UpdateCustomerAsync(It.IsAny <Customer>())); }
/// <summary> /// Setup the Commands /// </summary> private void InitCommands() { Command <CreateCustomer>(cmd => { var evt = new CustomerCreated(cmd.Id, cmd.FirstName, cmd.LastName); Persist(evt, e => { _readStore.CreateCustomer(new Customer { CustomerId = evt.Id, CustomerName = $"{evt.FirstName} {evt.LastName}", }); Context.System.EventStream.Publish(e); }); }); Command <UpdateCustomer>(cmd => { var evt = new CustomerUpdated(cmd.Id, cmd.FirstName, cmd.LastName); Persist(evt, e => { _readStore.UpdateCustomer(new Customer { CustomerId = evt.Id, CustomerName = $"{evt.FirstName} {evt.LastName}", }); Context.System.EventStream.Publish(e); }); }); Command <DeleteCustomer>(cmd => { var evt = new CustomerDeleted(cmd.Id); Persist(evt, e => { _readStore.DeleteCustomerById(evt.Id); Context.System.EventStream.Publish(e); }); }); }
private void btnAdd_Click(object sender, EventArgs e) { if (txtName.isEmptyOrHint()) { txtName.Focus(); return; } if (txtAddress1.isEmptyOrHint()) { txtAddress1.Focus(); return; } if (txtCity.isEmptyOrHint()) { txtCity.Focus(); return; } if (txtCountry.isEmptyOrHint()) { txtCountry.Focus(); return; } if (txtPostalAddress.isEmptyOrHint()) { txtPostalAddress.Focus(); return; } if (txtPhoneNumber.isEmptyOrHint()) { txtPhoneNumber.Focus(); return; } Customer c = new Customer(); c.Name = txtName.Text.Trim(); c.PhoneNumber = txtPhoneNumber.Text.Trim(); c.PostalAddress = txtPostalAddress.Text.Trim(); c.City = txtCity.Text.Trim(); c.Country = txtCountry.Text.Trim(); c.Address2 = txtAddress2.isEmptyOrHint() ? "":txtAddress2.Text.Trim(); c.Address1 = txtAddress1.Text.Trim(); CurrentCustomer = c; if (Update) { CurrentCustomer.ID = UpdateID; c.ID = UpdateID; m.UpdateCustomer(c); CustomerUpdated?.Invoke(); } else { CurrentCustomer.ID = m.AddCustomer(c); //MessageBox.Show(c.ID); NewCustomerAdded?.Invoke(); } Reset(); this.Hide(); }