|
| 1 | +package com.codestuff.springcache.controller; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.http.MediaType; |
| 9 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 10 | +import org.springframework.web.bind.annotation.RequestParam; |
| 11 | +import org.springframework.web.bind.annotation.RestController; |
| 12 | + |
| 13 | +import com.codestuff.springcache.dao.EmployeeDAO; |
| 14 | +import com.codestuff.springcache.pojo.Employee; |
| 15 | +import com.codestuff.springcache.service.EmployeeService; |
| 16 | + |
| 17 | + |
| 18 | +@RestController |
| 19 | +public class EmployeeController { |
| 20 | + |
| 21 | + private static final Logger LOGGER = LoggerFactory.getLogger(EmployeeController.class); |
| 22 | + |
| 23 | + @Autowired |
| 24 | + private EmployeeDAO employeeDAO; |
| 25 | + |
| 26 | + @Autowired |
| 27 | + private EmployeeService service; |
| 28 | + |
| 29 | + @RequestMapping("/") |
| 30 | + public String index() { |
| 31 | + return "OK"; |
| 32 | + } |
| 33 | + |
| 34 | + @RequestMapping(path="/insert") |
| 35 | + public void insert(@RequestParam String name, @RequestParam String place) { |
| 36 | + Employee emp = new Employee(); |
| 37 | + emp.setEmpId(""+System.currentTimeMillis()); |
| 38 | + emp.setName(name); |
| 39 | + emp.setPlace(place); |
| 40 | + employeeDAO.insert(emp); |
| 41 | + LOGGER.info("Employee has been created. "+emp); |
| 42 | + } |
| 43 | + |
| 44 | + @RequestMapping(path="/update") |
| 45 | + public void update(@RequestParam String empid, @RequestParam String name, @RequestParam String place) { |
| 46 | + Employee emp = new Employee(); |
| 47 | + emp.setEmpId(empid); |
| 48 | + emp.setName(name); |
| 49 | + emp.setPlace(place); |
| 50 | + service.update(emp); |
| 51 | + LOGGER.info("Employee updated. "+emp); |
| 52 | + } |
| 53 | + |
| 54 | + @RequestMapping(path="/fetchName") |
| 55 | + public String get(String empID) { |
| 56 | + return empID+" : "+service.getName(empID); |
| 57 | + } |
| 58 | + |
| 59 | + @RequestMapping(path="/fetchAll",produces=MediaType.APPLICATION_JSON_VALUE) |
| 60 | + public List<Employee> getAll() { |
| 61 | + LOGGER.info("Fetching all employees."); |
| 62 | + return service.getAll(); |
| 63 | + } |
| 64 | + |
| 65 | + @RequestMapping(path="/flush") |
| 66 | + public void flushCache() { |
| 67 | + service.flushCache(); |
| 68 | + LOGGER.info("All cache has been flushed."); |
| 69 | + } |
| 70 | +} |
0 commit comments