package com.mpalourdio.hello.controllers;

import com.mpalourdio.hello.model.Task;
import com.mpalourdio.hello.model.TaskRepository;
import com.mpalourdio.hello.service.UselessBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
class HomeController {

    private final String myProperty;
    private final UselessBean uselessBean;
    private final TaskRepository taskRepository;

    @Autowired
    public HomeController(
            final TaskRepository taskRepository,
            final UselessBean uselessBean,
            @Value("${property.whatever}") final String myProperty
    ) {
        this.taskRepository = taskRepository;
        this.uselessBean = uselessBean;
        this.myProperty = myProperty;
    }

    @RequestMapping("/")
    String indexAction(final Model model) {
        final List task = this.taskRepository.findByTaskStatus("ACTIVE");
        final Task activity = this.taskRepository.findOne(1);
        model.addAttribute("iwantthisinmyview", this.uselessBean.getTestPro());
        model.addAttribute("iwantthisinmyviewfromhibernate", activity.getTaskName());
        return "home/index";
    }

    @RequestMapping("/other")
    String otherAction(final Model model) {
        this.uselessBean.setTestPro("imsetinthecontrolleronthefly");
        final List task = this.taskRepository.findByTaskStatus("ACTIVE");
        final Task activity = this.taskRepository.findOne(1);
        model.addAttribute("iwantthisinmyview", this.uselessBean.getTestPro());
        model.addAttribute("iwantthisinmyviewfromhibernate", activity.getTaskName());
        model.addAttribute("iwantthisinmyviewfromproperties", this.myProperty);
        return "home/index";
    }

    @RequestMapping("custorepo")
    String customRepoAction(final Model model) {
        //use a custom method repository
        final List<Task> mediumTasks = this.taskRepository.customFindByPriority("MEDIUM");
        //print only those who have 'Implementation' as task_name
        mediumTasks.
                stream().
                filter(s -> s.getTaskName().equals("Implementation")).
                forEach(System.out::println);

        return "home/index";
    }

}

@@ -431,22 +431,18 @@
 otation.
-Reques
+Ge
 tMapping
@@ -971,38 +971,34 @@
 ty;%0A    %7D%0A%0A    @
-Reques
+Ge
 tMapping(%22/%22)%0A  
@@ -1057,37 +1057,32 @@
 nal List task = 
-this.
 taskRepository.f
@@ -1130,37 +1130,32 @@
 Task activity = 
-this.
 taskRepository.f
@@ -1323,32 +1323,33 @@
 getTaskName());%0A
+%0A
         return %22
@@ -1365,38 +1365,34 @@
 x%22;%0A    %7D%0A%0A    @
-Reques
+Ge
 tMapping(%22/other
@@ -1889,16 +1889,17 @@
 perty);%0A
+%0A
         
@@ -1935,14 +1935,10 @@
    @
-Reques
+Ge
 tMap
@@ -1943,16 +1943,17 @@
 apping(%22
+/
 custorep
@@ -2397,17 +2397,16 @@
 index%22;%0A
+
     %7D%0A
-%0A
 %7D%0A

Refactor with new mapping annotations