package de.hftstuttgart.rest.v1.jenkins;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class RestAPIController {

    private static final Logger LOG = LogManager.getLogger(RestAPIController.class);

    public static final Map<String, String> JOB_MAP = new HashMap<>();

    @PostMapping("/v1/uploaduserresults")
    public void uploadsResults(@RequestParam("jobID") String jobId, @RequestBody String userResult) {
        LOG.info("result: " + userResult);
        if (!JOB_MAP.containsKey(jobId)) {
            String keys = String.join(", ", JOB_MAP.keySet());
            throw new IllegalArgumentException(
                    String.format("Key %s does not exist in JOB_MAP, available Keys: [%s]", jobId, keys));
        }
        JOB_MAP.put(jobId, userResult);
    }
}