package de.effigenix.application.masterdata.productcategory;

import de.effigenix.domain.masterdata.productcategory.ProductCategory;
import de.effigenix.domain.masterdata.productcategory.ProductCategoryError;
import de.effigenix.domain.masterdata.productcategory.ProductCategoryRepository;
import de.effigenix.shared.common.Result;

import java.util.List;

import static de.effigenix.shared.common.Result.*;

public class ListProductCategories {

    private final ProductCategoryRepository categoryRepository;

    public ListProductCategories(ProductCategoryRepository categoryRepository) {
        this.categoryRepository = categoryRepository;
    }

    public Result<ProductCategoryError, List<ProductCategory>> execute() {
        return switch (categoryRepository.findAll()) {
            case Failure(var err) ->
                Result.failure(new ProductCategoryError.RepositoryFailure(err.message()));
            case Success(var categories) ->
                Result.success(categories);
        };
    }
}
