package org.jeasy.jobs.server.web;

import org.jeasy.jobs.Utils;
import org.jeasy.jobs.job.JobRepository;
import org.jeasy.jobs.request.JobRequest;
import org.jeasy.jobs.request.JobRequestRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class JobRequestController {

    private static final Logger LOGGER = LoggerFactory.getLogger(JobRequestController.class);

    @Autowired
    private JobRepository jobRepository;
    @Autowired
    private JobRequestRepository jobRequestRepository;

    @RequestMapping("/requests/{id}")
    JobRequest getJobRequest(@PathVariable int id) {
        return jobRequestRepository.findById(id);
    }

    @RequestMapping(path = "/requests", method = RequestMethod.GET)
    List<JobRequest> getAllJobRequests() {
        return jobRequestRepository.findAllJobRequests();
    }

    @RequestMapping(path = "/requests", method = RequestMethod.POST, consumes = {"application/json"})
    @ResponseBody
    String postJobRequest(@RequestBody String parameters) throws Exception {
        if (parameters == null || parameters.isEmpty()) {
            return "You must at least provide the jobId for which you want to request an execution";
        }
        Map<String, String> parsedParameters = Utils.parseParameters(parameters);
        String jobId = parsedParameters.get("jobId");
        if (jobId == null) {
            return "jobId parameter is mandatory";
        }
        Integer jobIdentifier;
        try {
            jobIdentifier = Integer.parseInt(jobId);
        } catch (NumberFormatException e) {
            return "jobId parameter must be an integer";
        }
        if (jobRepository.findById(jobIdentifier) == null) {
            return "No job registered with id = " + jobId;
        }
        JobRequest jobRequest = new JobRequest(jobIdentifier, parameters);
        jobRequestRepository.save(jobRequest);
        LOGGER.info("Submitted a new job request for job " + jobId + " with parameters " + parsedParameters);
        return "Job request submitted successfully";
    }

}

@@ -1362,19 +1362,21 @@
 xecution
+%5Cn
 %22;%0A
-
         
@@ -1590,16 +1590,18 @@
 andatory
+%5Cn
 %22;%0A     
@@ -1801,16 +1801,18 @@
  integer
+%5Cn
 %22;%0A     
@@ -1934,16 +1934,23 @@
  + jobId
+ + %22%5Cn%22
 ;%0A      
@@ -2100,15 +2100,14 @@
 fo(%22
-Submitt
+Receiv
 ed a
@@ -2234,16 +2234,18 @@
 essfully
+%5Cn
 %22;%0A    %7D

add line feed in the body of rest response