package nl.vpro.poel.controller;

import nl.vpro.poel.domain.User;
import nl.vpro.poel.service.RankingService;
import nl.vpro.poel.domain.CurrentUser;
import nl.vpro.poel.dto.RankingEntry;
import nl.vpro.poel.UserUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.security.Principal;
import java.util.Optional;

@Controller
class IndexController {

    private final RankingService rankingService;

    @Autowired
    public IndexController(RankingService rankingService) {
        this.rankingService = rankingService;
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    String showIndex(Principal principal, Model model) {

        CurrentUser currentUser = UserUtil.getCurrentUser(principal).orElse(null);

        if (currentUser != null) {
            Optional<RankingEntry<User>> ranking = rankingService.getRankingEntry( currentUser.getUser());
            model.addAttribute("ranking", ranking.orElse(null));
            model.addAttribute("user", currentUser.getUser() );
        }

        return "index";
    }

    @RequestMapping(value = "/admin", method = RequestMethod.GET)
    String showAdminIndex() {
        return "admin/index";
    }
}

@@ -891,16 +891,25 @@
         
+Optional%3C
 CurrentU
@@ -911,16 +911,17 @@
 rentUser
+%3E
  current
@@ -961,29 +961,16 @@
 incipal)
-.orElse(null)
 ;%0A%0A     
@@ -991,19 +991,76 @@
 User
- != null) %7B
+.isPresent()) %7B%0A            User user = currentUser.get().getUser();
 %0A   
@@ -1142,98 +1142,58 @@
 try(
- currentUser.getUser());%0A            model.addAttribute(%22ranking%22, ranking.orElse(null));%0A
+user);%0A            if (ranking.isPresent()) %7B%0A    
     
@@ -1224,40 +1224,48 @@
 te(%22
-user%22, currentUser.getUser
+ranking%22, ranking.get
 ()
- 
 );%0A
+            %7D%0A
     

Use the Optional, Luke!