package com.eactive.eai.rms.common.cache;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class CacheController {

	private CacheCleaner cacheService;

	@Autowired
	public CacheController(CacheCleaner cacheService) {
		this.cacheService = cacheService;
	}

	@GetMapping("/common/acl/cache/cacheMan.view")
	public String values(ModelMap model) {
		model.addAttribute("values", cacheService.findCacheNames());
		return "/common/acl/cache/cacheMan";
	}

	@PostMapping(value = "/common/acl/cache/cacheMan.json", params = "cmd=UPDATE")
	public ResponseEntity<String> clear(String cacheName) {

		if ("CLEAR_ALL".equals(cacheName)) {
			cacheService.clear();
		} else {
			cacheService.clear(cacheName);
		}

		return ResponseEntity.ok("ok");
	}

}