package com.github.jchartier.slackbot.pomodoro.service;

import com.github.jchartier.slackbot.pomodoro.dto.Pomodoro;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Component
public class PomodoroService {

    @Autowired
    private StringRedisTemplate template;

    private ValueOperations<String, String> ops;

    @PostConstruct
    public void init() {

         ops = template.opsForValue();
    }

    public void create(String username, long delay, TimeUnit timeUnit) {

        template.opsForValue().set(username, "", delay, timeUnit);
    }

    public List<Pomodoro> list() {

        List<String> userNames = new ArrayList<>(ops.getOperations().keys("*"));

        return userNames.stream()
                .map(userName -> new Pomodoro(userName, getDelay(userName)))
                .collect(Collectors.toList());
    }

    public void delete(String username) {

        template.delete(username);
    }

    private long getDelay(String username) {

        return ops.getOperations().getExpire(username, TimeUnit.MINUTES);
    }
}

@@ -173,16 +173,76 @@
 owired;%0A
+import org.springframework.data.redis.core.RedisOperations;%0A
 import o
@@ -297,16 +297,16 @@
 mplate;%0A
-
 import o
@@ -699,149 +699,8 @@
 e;%0A%0A
-    private ValueOperations%3CString, String%3E ops;%0A%0A    @PostConstruct%0A    public void init() %7B%0A%0A         ops = template.opsForValue();%0A    %7D%0A%0A
     
@@ -928,23 +928,24 @@
 yList%3C%3E(
-ops.get
+getRedis
 Operatio
@@ -1277,15 +1277,16 @@
 urn 
-ops.get
+getRedis
 Oper
@@ -1338,11 +1338,141 @@
 );%0A    %7D
+%0A%0A    private RedisOperations%3CString, String%3E getRedisOperations() %7B%0A%0A        return template.opsForValue().getOperations();%0A    %7D
 %0A%7D%0A

Clean code in PomodoroService