Beispiel #1
0
        /// <summary>
        /// Implementation of <see cref="ICustomerCommands.ChangePersonInfo(Guid, string, string, string, Gender, DateTime)"/>
        /// </summary>
        /// <param name="customerId">The customer id</param>
        /// <param name="firstName">The first name</param>
        /// <param name="lastName">The last name</param>
        /// <param name="nationalIdentificationNumber">The national identification number</param>
        /// <param name="gender">The gender</param>
        /// <param name="birthDate">The birth date</param>
        /// <returns></returns>
        public async Task ChangePersonInfo(Guid customerId, string firstName, string lastName, string nationalIdentificationNumber, Gender gender, DateTime birthDate)
        {
            if (customerId == Guid.Empty)
            {
                throw new ArgumentException("value cannot be empty", nameof(customerId));
            }

            if (string.IsNullOrWhiteSpace(firstName))
            {
                throw new ArgumentException("value cannot be empty", nameof(firstName));
            }

            if (string.IsNullOrWhiteSpace(lastName))
            {
                throw new ArgumentException("value cannot be empty", nameof(lastName));
            }

            var customer = await Repository.GetByKeyAsync <Person>(customerId);

            if (customer == null)
            {
                throw new ArgumentOutOfRangeException(nameof(customerId));
            }

            if (firstName != customer.FirstName)
            {
                customer.ChangeFirstName(firstName);
            }
            if (lastName != customer.LastName)
            {
                customer.ChangeLastName(lastName);
            }
            if (nationalIdentificationNumber != customer.NationalIdentificationNumber)
            {
                customer.SetNationalIdentificationNumber(nationalIdentificationNumber);
            }
            if (gender != customer.Gender)
            {
                customer.ChangeGender(gender);
            }
            if (birthDate != customer.BirthDate)
            {
                customer.ChangeBirthDate(birthDate);
            }

            await Repository.SaveChangesAsync();

            var @event = new PersonInfoChangedEvent(customerId, firstName, lastName, nationalIdentificationNumber, gender, birthDate);

            EventBus.RaiseEvent(@event);
        }
        public void ToString_Should_Return_Event_Formatted_As_String()
        {
            Guid     personId  = Guid.NewGuid();
            string   firstName = "firstname";
            string   lastName  = "lastname";
            string   nationalIdentificationNumber = "number";
            Gender   gender    = Gender.Female;
            DateTime birthDate = new DateTime(1980, 1, 1);

            var ev = new PersonInfoChangedEvent(personId, firstName, lastName, nationalIdentificationNumber, gender, birthDate);

            string eventAsString = $"Person info changed: first name {firstName}, last name {lastName}, national identification number {nationalIdentificationNumber}, gender {gender.ToString()}, birth date {birthDate.ToShortDateString()}";

            Assert.Equal(eventAsString, ev.ToString());
        }
        public void Ctor_Should_Set_All_Values_Correctly()
        {
            Guid     personId  = Guid.NewGuid();
            string   firstName = "firstname";
            string   lastName  = "lastname";
            string   nationalIdentificationNumber = "number";
            Gender   gender    = Gender.Female;
            DateTime birthDate = new DateTime(1980, 1, 1);

            var ev = new PersonInfoChangedEvent(personId, firstName, lastName, nationalIdentificationNumber, gender, birthDate);

            Assert.Equal(personId, ev.PersonId);
            Assert.Equal(firstName, ev.FirstName);
            Assert.Equal(lastName, ev.LastName);
            Assert.Equal(gender, ev.Gender);
            Assert.Equal(birthDate, ev.BirthDate);
            Assert.Equal(nationalIdentificationNumber, ev.NationalIdentificationNumber);

            Assert.Equal(personId, ev.AggregateId);
            Assert.Equal(typeof(Registries.Models.Customer), ev.AggregateType);
            Assert.Equal(DateTime.Today, ev.FiredOn.Date);
        }