public async Task <ErrorCodeResponse <ProductCategoriesErrorCodesContract> > UpsertAsync(
            [FromBody] AddProductCategoryRequest request)
        {
            var result =
                await _productCategoriesService.GetOrCreate(request.Category, request.UserName,
                                                            this.TryGetCorrelationId());

            var response = new ErrorCodeResponse <ProductCategoriesErrorCodesContract>();

            if (result.IsFailed)
            {
                response.ErrorCode =
                    _convertService.Convert <ProductCategoriesErrorCodes, ProductCategoriesErrorCodesContract>(
                        result.Error.GetValueOrDefault());
            }

            return(response);
        }
Exemplo n.º 2
0
        private async Task <Result <Product, ProductsErrorCodes> > SetCategoryIdAsync(Product value,
                                                                                      string userName,
                                                                                      string correlationId, Product existing = null)
        {
            var categoryResult = await _productCategoriesService.GetOrCreate(value.Category, userName, correlationId);

            if (categoryResult.IsFailed)
            {
                return(new Result <Product, ProductsErrorCodes>(ProductsErrorCodes.CannotCreateCategory));
            }

            var category = categoryResult.Value;

            if (!category.IsLeaf)
            {
                return(new Result <Product, ProductsErrorCodes>(ProductsErrorCodes.CannotCreateProductInNonLeafCategory));
            }

            value.Category = category.Id;
            return(new Result <Product, ProductsErrorCodes>(value));
        }