public async Task <CommandResponse> Handle(EditContactCommand request, CancellationToken cancellationToken)
        {
            var contact = await _contactRepository.GetByIdAsync(request.ContactId);

            if (contact == null)
            {
                await MediatorHandler.NotifyDomainNotification(DomainNotification.Fail("The Contact is invalid !"));

                return(CommandResponse.Fail());
            }

            var contactType = await _contactTypeRepository.GetByIdAsync(request.ContactTypeId);

            if (contactType == null)
            {
                await MediatorHandler.NotifyDomainNotification(DomainNotification.Fail("The Contact Type is invalid !"));

                return(CommandResponse.Fail());
            }

            contact.ChangeName(request.ContactName);

            contact.ChangeBirthDate(request.ContactBirthDate);

            contact.ChangePhoneNumber(request.ContactPhone);

            contact.ChangeContactType(contactType);

            if (!contact.IsValid())
            {
                foreach (var item in contact.ValidationResult.Errors)
                {
                    await MediatorHandler.NotifyDomainNotification(DomainNotification.Fail(item.ErrorMessage));
                }

                return(CommandResponse.Fail("Contact invalid !"));
            }

            _contactRepository.Update(contact);

            var result = await _contactRepository.CommitAsync();

            return(result.Success
                ? CommandResponse.Ok()
                : CommandResponse.Fail("Fail recording the register in database !"));
        }
示例#2
0
        public async Task <CommandResponse> Handle(CreateReservationCommand request, CancellationToken cancellationToken)
        {
            if (!request.ContactId.HasValue)
            {
                return(CommandResponse.Fail("Contact Invalid"));
            }

            var contact = await _contactRepository.GetByIdAsync(request.ContactId.Value);

            if (contact == null)
            {
                var contactCreated = CreateContact(request);
                if (contactCreated == null)
                {
                    return(CommandResponse.Fail("Contact Invalid"));
                }
                contact = contactCreated;
            }

            var reservation = new Reservation(
                id: request.ReservationId,
                message: request.Message,
                contact: contact,
                ranking: 1,
                favorited: false
                );

            if (!reservation.IsValid())
            {
                foreach (var item in reservation.ValidationResult.Errors)
                {
                    DomainNotification.Fail(item.ErrorMessage);
                }

                return(CommandResponse.Fail("Reservation invalid !"));
            }


            await _reservationRepository.AddAsync(reservation);

            var result = await _reservationRepository.CommitAsync();

            return(result.Success
                ? CommandResponse.Ok()
                : CommandResponse.Fail("Fail recording the register in database !"));
        }
        public async void AddDomainNotificationError_Ok()
        {
            var faker = new Faker();

            var message = faker.Lorem.Sentence();

            var handler = new DomainNotificationHandler();

            var domainNotification = DomainNotification.Fail(message);

            await handler.Handle(domainNotification, new CancellationToken());

            var notifications = handler.GetNotificationsError();

            Assert.True(handler.HasNotificationsError());

            Assert.True(notifications.Count == 1);
        }
        public async void GetNotificationsError_Ok()
        {
            var faker = new Faker();

            var message = faker.Lorem.Sentence();

            var handler = new DomainNotificationHandler();

            var domainNotification = DomainNotification.Fail(message);

            await handler.Handle(domainNotification, new CancellationToken());

            var notifications = handler.GetNotificationsError();

            var result = notifications.Select(x => x.Value);


            Assert.Contains(message, result);
        }
        public async void DomainNotificationClear_Ok()
        {
            var faker = new Faker();


            var handler = new DomainNotificationHandler();


            var qtError   = faker.Random.Int(min: 3, max: 10);
            var qtSuccess = faker.Random.Int(min: 3, max: 10);


            for (var i = 0; i < qtError; i++)
            {
                var message = faker.Lorem.Sentence();

                var domainNotification = DomainNotification.Fail(message);
                await handler.Handle(domainNotification, new CancellationToken());
            }

            for (var i = 0; i < qtSuccess; i++)
            {
                var message = faker.Lorem.Sentence();

                var domainNotification = DomainNotification.Success(message);
                await handler.Handle(domainNotification, new CancellationToken());
            }

            handler.Clear();

            var notificationsError   = handler.GetNotificationsError();
            var notificationsSuccess = handler.GetNotificationsSuccess();

            Assert.False(handler.HasNotificationsError());
            Assert.False(handler.HasNotificationsSucess());


            Assert.True(notificationsError.Count == 0);
            Assert.True(notificationsSuccess.Count == 0);
        }
        public async Task <CommandResponse> Handle(DeleteContactCommand request, CancellationToken cancellationToken)
        {
            var contact = await _contactRepository.GetByIdAsync(request.ContactId);

            if (contact == null)
            {
                await MediatorHandler.NotifyDomainNotification(DomainNotification.Fail("The Contact is invalid !"));

                return(CommandResponse.Fail());
            }

            //the system does not exclude, just mark the Valid as false;
            contact.Remove();

            _contactRepository.Update(contact);

            var result = await _contactRepository.CommitAsync();

            return(result.Success
                ? CommandResponse.Ok()
                : CommandResponse.Fail("Fail recording the register in database !"));
        }
示例#7
0
        private Contact CreateContact(CreateReservationCommand request)
        {
            var contactType = _contactTypeRepository.GetByIdAsync(request.ContactTypeId).GetAwaiter().GetResult();

            if (contactType == null)
            {
                MediatorHandler.NotifyDomainNotification(
                    DomainNotification.Fail("The contact type is invalid !"));

                return(null);
            }


            var contact = new Contact(
                name: request.ContactName,
                phoneNumber: request.ContactPhone,
                birthDate: request.ContactBirthdate,
                contactType: contactType
                );

            if (!contact.IsValid())
            {
                foreach (var item in contact.ValidationResult.Errors)
                {
                    DomainNotification.Fail(item.ErrorMessage);
                }

                return(null);
            }


            _contactRepository.AddAsync(contact);

            var result = _contactRepository.CommitAsync().GetAwaiter().GetResult();

            return(result.Success ? contact : null);
        }
        public async Task <CommandResponse> Handle(CreateContactCommand request, CancellationToken cancellationToken)
        {
            var contactType = await _contactTypeRepository.GetByIdAsync(request.ContactTypeId);

            if (contactType == null)
            {
                await MediatorHandler.NotifyDomainNotification(DomainNotification.Fail("The Contact Id is not valid !"));

                return(CommandResponse.Fail());
            }

            var contact = new Contact(
                name: request.ContactName,
                phoneNumber: request.ContactPhone,
                birthDate: request.ContactBirthDate,
                contactType: contactType
                );

            if (!contact.IsValid())
            {
                foreach (var item in contact.ValidationResult.Errors)
                {
                    await MediatorHandler.NotifyDomainNotification(DomainNotification.Fail(item.ErrorMessage));
                }

                return(CommandResponse.Fail("Contact invalid !"));
            }

            await _contactRepository.AddAsync(contact);

            var result = await _contactRepository.CommitAsync();

            return(result.Success
                ? CommandResponse.Ok(contact.Id)
                : CommandResponse.Fail("Fail recording the register in database !"));
        }
示例#9
0
 protected void NotifyError(string message)
 {
     Mediator.NotifyDomainNotification(DomainNotification.Fail(message));
 }