package lan.dk.podcastserver.business;

import javaslang.collection.List;
import javaslang.collection.Set;
import javaslang.control.Option;
import lan.dk.podcastserver.entity.Tag;
import lan.dk.podcastserver.repository.TagRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * Created by kevin on 07/06/2014.
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class TagBusiness {

    private final TagRepository tagRepository;

    public List<Tag> findAll() {
        return List.ofAll(tagRepository.findAll());
    }

    public Tag findOne(UUID id) {
        return tagRepository.findOne(id);
    }

    public Set<Tag> findByNameLike(String name) {
        return tagRepository.findByNameContainsIgnoreCase(name);
    }

    Set<Tag> getTagListByName(Set<Tag> tagList) {
        return tagList.map(t -> findByName(t.getName()));
    }

    private Tag findByName(String name) {
        return tagRepository
                .findByNameIgnoreCase(name)
                .getOrElse(() -> tagRepository.save(Tag.builder().name(name).build()));
    }

    public Set<Tag> findAllByName(Set<String> names){
        return names
                .map(tagRepository::findByNameIgnoreCase)
                .filter(Option::isDefined)
                .map(Option::get);
    }
}

@@ -104,41 +104,8 @@
 et;%0A
-import javaslang.control.Option;%0A
 impo
@@ -1199,35 +1199,22 @@
 rn names
-%0A                .m
+.flatM
 ap(tagRe
@@ -1248,85 +1248,8 @@
 ase)
-%0A                .filter(Option::isDefined)%0A                .map(Option::get)
 ;%0A  

use flatmap to simplify code of searchByName