springInterview/src/main/java/com/chantha/springdemo/controller/HomeController.java
2020-04-28 16:27:31 +07:00

46 lines
1.3 KiB
Java

package com.chantha.springdemo.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.chantha.springdemo.model.Food;
import com.chantha.springdemo.service.FoodService;
@Controller
public class HomeController {
@Autowired
private final FoodService foodService;
public HomeController(FoodService foodService) {
super();
this.foodService = foodService;
}
@RequestMapping("/home")
public String show(Model mod) {
List<Food> food=foodService.findFoodLimit();
mod.addAttribute("foods", food);
return "index";
}
@PostMapping("/food/update")
public String updateProduct(@ModelAttribute(name="food") Food food) {
foodService.saveFood(food);
return "redirect:/home";
}
@RequestMapping("/food/delete/{id}")
public String deleteFoodById(@PathVariable("id") int id) {
foodService.deleteFoodById(id);
return "redirect:/home";
}
@RequestMapping(value="/food/add",method=RequestMethod.POST)
public String addProduct(@ModelAttribute(name="food") Food food) {
foodService.saveFood(food);
return "redirect:/home";
}
}