package cz.inqool.dl4dh.krameriusplus.api;

import cz.inqool.dl4dh.krameriusplus.dto.ScheduleMultipleDto;
import cz.inqool.dl4dh.krameriusplus.service.SolrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * @author Norbert Bodnar
 */
@RestController
@RequestMapping("/api/index")
@ConditionalOnProperty(prefix = "solr", value = "enabled", havingValue = "true")
public class IndexApi {

    private final SolrService solrService;

    @Autowired
    public IndexApi(SolrService solrService) {
        this.solrService = solrService;
    }

    @PostMapping("/{pid}")
    public void index(@PathVariable("pid") String rootPid) {
        solrService.index(rootPid);
    }

    @PostMapping
    public void indexMultiple(@Valid @NotNull @RequestBody ScheduleMultipleDto scheduleMultipleDto) {
        for (String publicationId : scheduleMultipleDto.getPublications()) {
            solrService.indexAsync(publicationId);
        }
    }
}
