package de.effigenix.application.masterdata;

import de.effigenix.domain.masterdata.*;
import de.effigenix.shared.common.Result;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

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

@Transactional(readOnly = true)
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);
        };
    }
}
