public async Task <IResultModel <ProductDto> > Handle(GetProductRequest request, CancellationToken cancellationToken)
        {
            var maybeProduct = await this.productsProvider.GetAsync(request.Id);

            var resultModel = maybeProduct.Unwrap(
                (product) => ResultModel <ProductDto> .Ok((ProductDto)product),
                () => ResultModel <ProductDto> .Failure(ApiResultExtensions.NotFound(nameof(Product), request.Id), ProductDto.None));

            return(resultModel);
        }
Example #2
0
        public async Task <IResultModel> Handle(UpdateProductCommand request, CancellationToken cancellationToken)
        {
            var maybeProduct = await this.productsRepository.GetAsync(request.Id);

            var resultModel = await maybeProduct.Unwrap((product) => this.EditProductInformationAsync(request, product, cancellationToken),
                                                        () =>
            {
                this.logger.LogWarning($"Product with id: {request.Id} not found in database.");

                return(ResultModel.Fail(ApiResultExtensions.NotFound(nameof(Product), request.Id)));
            });

            return(resultModel);
        }
        public async Task <IResultModel> Handle(DeleteProductCommand request, CancellationToken cancellationToken)
        {
            var maybeProduct = await this.productsRepository.GetAsync(request.Id);

            var resultModel = await maybeProduct.Unwrap((async product =>
            {
                product.Remove();
                await this.productsRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);

                return(ResultModel.Ok());
            }),
                                                        () =>
            {
                this.logger.LogWarning($"Product with id: {request.Id} not found in database.");

                return(Task.FromResult(ResultModel.Fail(ApiResultExtensions.NotFound(nameof(Product), request.Id))));
            });

            return(resultModel);
        }
Example #4
0
 public static IResultModel ToResultModel <T>(this Result <T> result)
 {
     return(result.Success ?
            ResultModel.Ok() :
            ResultModel.Fail(ApiResultExtensions.ValidationFailed($"Error: {result.Error}, Field: {result.Field}")));
 }