wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 1 | package com.example.g8backend.controller; |
| 2 | |
| 3 | import com.example.g8backend.entity.User; |
| 4 | import com.example.g8backend.service.IUserService; |
| 5 | import org.springframework.beans.factory.annotation.Autowired; |
| 6 | import org.springframework.web.bind.annotation.*; |
| 7 | |
| 8 | import java.util.List; |
| 9 | |
| 10 | @RestController |
| 11 | @RequestMapping("/users") |
| 12 | public class UserController { |
| 13 | |
| 14 | @Autowired |
| 15 | private IUserService userService; |
| 16 | |
| 17 | // 获取所有用户 |
| 18 | @GetMapping |
| 19 | public List<User> getUsers() { |
| 20 | return userService.list(); |
| 21 | } |
| 22 | |
| 23 | // 通过ID获取用户 |
| 24 | @GetMapping("/{id}") |
| 25 | public User getUserById(@PathVariable Long id) { |
| 26 | return userService.getById(id); |
| 27 | } |
| 28 | |
| 29 | // 通过用户名获取用户 |
| 30 | @GetMapping("/name/{name}") |
| 31 | public User getUserByName(@PathVariable String name) { |
| 32 | return userService.getUserByName(name); |
| 33 | } |
| 34 | |
| 35 | // 添加用户 |
| 36 | @PostMapping |
wuchimedes | 079c163 | 2025-04-02 22:01:20 +0800 | [diff] [blame] | 37 | public void addUser(@RequestBody User user) { |
| 38 | // return userService.save(user); |
| 39 | userService.registerUser(user); |
wuchimedes | a1bf278 | 2025-03-27 15:08:54 +0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | // 修改用户 |
| 43 | @PutMapping |
| 44 | public boolean updateUser(@RequestBody User user) { |
| 45 | return userService.updateById(user); |
| 46 | } |
| 47 | |
| 48 | // 删除用户 |
| 49 | @DeleteMapping("/{id}") |
| 50 | public boolean deleteUser(@PathVariable Long id) { |
| 51 | return userService.removeById(id); |
| 52 | } |
| 53 | } |
| 54 | |