package web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import pojo.Users;
import service.UsersService;

import java.util.List;

@Controller
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UsersService usersService;

    @RequestMapping("/addUsers")
    @Transactional
    public String addUsers(Users users){
        this.usersService.addUsers(users);
        return "redirect:/ok";
    }

    @RequestMapping("/findUsers")
    public String findUsers(Model model){
        List<Users> list = this.usersService.findUsers();
        model.addAttribute("list",list);
        return "showusers";
    }

    @RequestMapping("/updateUsers")
    public String preUpdate(int userid,Model model){
        Users users = this.usersService.findUserById(userid);
        model.addAttribute("user",users);
        return "preupdate";
    }

    @RequestMapping("/update")
    public String updateUser(Users users){
        this.usersService.modifyUsers(users);
        return "redirect:/ok";
    }

    @RequestMapping("/deleteUsers")
    @Transactional
    public String deleteUsers(int userid){
        this.usersService.dropUsers(userid);
        return "redirect:/ok";
    }
}
