package ch.heigvd.quaris.api;

import ch.heigvd.quaris.api.definitions.RulesApi;
import ch.heigvd.quaris.api.dto.RuleDTO;
import ch.heigvd.quaris.models.Application;
import ch.heigvd.quaris.repositories.ApplicationRepository;
import ch.heigvd.quaris.repositories.RuleRepository;
import ch.heigvd.quaris.services.ApplicationService;
import io.swagger.annotations.ApiParam;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Fabien Salathe
 */
@RestController
public class RulesEndpoint implements RulesApi {

    @Autowired
    private final ApplicationRepository applicationsRepository = null;

    @Autowired
    private final RuleRepository rulesRepository = null;

    // public ResponseEntity<List<Rule>> rulesGet() {
    @Override
    public ResponseEntity rulesGet() {
        ApplicationService as = new ApplicationService();

        List<ch.heigvd.quaris.models.Rule> allRulesModel = rulesRepository.findByApplicationName(as.getCurrentApplicationName());

        // Create DTOs
        Stream<RuleDTO> allRulesDTO = allRulesModel
                .parallelStream()
                .map(rm -> new ModelMapper().map(rm, RuleDTO.class));

        return ResponseEntity.ok(allRulesDTO.collect(Collectors.toList()));
    }

    @Override
    public ResponseEntity<Void> rulesIdPut(@ApiParam(value = "Rule id", required = true) @PathVariable("id") String id, @ApiParam(value = "new Rule data", required = true) @RequestBody RuleDTO data) {
        return null;
    }

    @Override
    public ResponseEntity<Void> rulesPost(@ApiParam(value = "Rule to add", required = true) @RequestBody RuleDTO rule) {
        if (rule == null) {
            return ResponseEntity.badRequest().build();
        }

        ApplicationService as = new ApplicationService();

        Application app = applicationsRepository.findByName(as.getCurrentApplicationName());
        System.out.println("targetApplicationName: " + app.getName()); // DEBUG
        try {
            ch.heigvd.quaris.models.Rule ruleModel = new ch.heigvd.quaris.models.Rule();
            ruleModel.setName(rule.getName());
            ruleModel.setAction(rule.getAction());
            ruleModel.setApplication(app);
            ruleModel.setCriteria(rule.getCriteria());
            ruleModel.setEnabled(rule.getEnabled());

            rulesRepository.save(ruleModel);

        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.CONFLICT).build();
        }

        return ResponseEntity.ok().build();
    }
}

@@ -159,16 +159,53 @@
 cation;%0A
+import ch.heigvd.quaris.models.Rule;%0A
 import c
@@ -501,16 +501,79 @@
 owired;%0A
+import org.springframework.dao.EmptyResultDataAccessException;%0A
 import o
@@ -1968,24 +1968,25 @@
 data) %7B%0A
+%0A
         
 return n
@@ -1981,20 +1981,813 @@
     
-return null;
+ch.heigvd.quaris.models.Rule rule = rulesRepository.findOne(Long.parseLong(id));%0A%0A        try %7B%0A            if(data.getName() != null)%0A                rule.setName(data.getName());%0A            if(data.getEnabled() != null)%0A                rule.setEnabled(data.getEnabled());%0A            if(data.getAction() != null)%0A                rule.setAction(data.getAction());%0A            if(data.getCriteria() != null)%0A                rule.setCriteria(data.getCriteria());%0A            rulesRepository.save(rule);%0A            return new ResponseEntity%3C%3E(HttpStatus.OK);%0A        %7D%0A        catch (EmptyResultDataAccessException e)%0A        %7B%0A            System.out.println(e.getMessage());%0A            System.out.println(e.getClass());%0A            return new ResponseEntity%3C%3E(HttpStatus.UNPROCESSABLE_ENTITY);%0A        %7D
 %0A   

Add put to Rules endpoint