Exemple #1
0
        public async Task <Unit> Handle(UpdateProductCommand request, CancellationToken cancellationToken)
        {
            var original = await productRepository.GetAsync(request.Id); // -> Get entity from db.

            if (original == null)                                        // -> If it is null, notificate and stop to avoid null exception.
            {
                await bus.InvokeDomainNotificationAsync("Not found.");

                return(Unit.Value);
            }

            request
            .IsNullEmptyOrWhitespace(e => e.Name, async() => await bus.InvokeDomainNotificationAsync("Invalid name."))
            .Is(e => e.Price <= 0, async() => await bus.InvokeDomainNotificationAsync("Invalid price."));

            original.UpdateInfo(request.Name, request.Description, request.Price);

            await productRepository.UpdateAsync(original);

            Commit();
            await bus.InvokeAsync(new UpdateProductEvent(original.Id, original.Name, original.Description, original.Price));

            return(Unit.Value);
        }