public async Task <IResultModel> Handle(CreateProductCommand request, CancellationToken cancellationToken) { var productName = ProductName.CreateInstance(request.Name); var price = Price.CreateInstance(request.Price); var resultModel = await ResultModelExtensions.Validate <Product>(productName, price) .OnSuccess(() => { var product = new Product(productName.Value, request.Description, price.Value, request.ImagePath, request.ProductType); _ = this.productsRepository.Add(product); return(this.productsRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken)); }) .OnFail((rs) => { this.logger.LogError($"Could not add product due to: {rs.ApiResult.Message}, errors: {string.Join(",", rs.ApiResult.ValidationErrors)}"); }); return(resultModel); }
public async Task <IResultModel <Guid> > Handle(CreateOrderCommand request, CancellationToken cancellationToken) { var table = Table.CreateInstance(request.Table); var resultModel = await ResultModelExtensions.Validate <Order>(table) .OnSuccess(async() => { var order = new Order(Guid.NewGuid(), table.Value); _ = this.ordersRepository.Add(order); await this.ordersRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken); return(order.Id); }, Guid.Empty) .OnFail((rs) => { this.logger.LogError($"Could not add product due to: {rs.ApiResult.Message}, errors: {string.Join(",", rs.ApiResult.ValidationErrors)}"); }); return(resultModel); }