|
|
@ -1,25 +1,32 @@
|
|
|
|
package com.example.demo.controller;
|
|
|
|
package com.example.controller;
|
|
|
|
|
|
|
|
|
|
|
|
import com.example.demo.entity.User;
|
|
|
|
import com.example.entity.User;
|
|
|
|
|
|
|
|
import com.example.service.UserService;
|
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
|
import io.swagger.v3.oas.annotations.Parameter;
|
|
|
|
import io.swagger.v3.oas.annotations.Parameter;
|
|
|
|
import io.swagger.v3.oas.annotations.parameters.RequestBody;
|
|
|
|
import io.swagger.v3.oas.annotations.parameters.RequestBody;
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
import org.springframework.transaction.reactive.TransactionalOperator;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
@RestController
|
|
|
|
@RequestMapping("/users")
|
|
|
|
@RequestMapping("/users")
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
public class UserController {
|
|
|
|
public class UserController {
|
|
|
|
|
|
|
|
|
|
|
|
static Map<Long, User> users = Collections.synchronizedMap(new HashMap<>());
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private UserService userService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("/")
|
|
|
|
@GetMapping("/")
|
|
|
|
@Operation(summary = "获取用户列表")
|
|
|
|
@Operation(summary = "获取用户列表")
|
|
|
|
public List<User> getUserList(){
|
|
|
|
public List<User> getUserList(){
|
|
|
|
List<User> userList = new ArrayList<>(users.values());
|
|
|
|
log.info("获取用户列表");
|
|
|
|
return userList;
|
|
|
|
return userService.list();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -27,32 +34,37 @@ public class UserController {
|
|
|
|
@Operation(summary = "创建用户",description = "根据user对象创建用户")
|
|
|
|
@Operation(summary = "创建用户",description = "根据user对象创建用户")
|
|
|
|
public String postUser(@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "用户实体类")
|
|
|
|
public String postUser(@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "用户实体类")
|
|
|
|
@org.springframework.web.bind.annotation.RequestBody User user) {
|
|
|
|
@org.springframework.web.bind.annotation.RequestBody User user) {
|
|
|
|
users.put(user.getId(), user);
|
|
|
|
log.info("创建用户:{}",user);
|
|
|
|
|
|
|
|
userService.save(user);
|
|
|
|
return "success";
|
|
|
|
return "success";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Operation(summary = "获取用户详细信息",description = "根据url的id来获取用户详细信息")
|
|
|
|
@Operation(summary = "获取用户详细信息",description = "根据url的id来获取用户详细信息")
|
|
|
|
@GetMapping("/{id}")
|
|
|
|
@GetMapping("/{id}")
|
|
|
|
public User getUser(@Parameter(description ="用户id" )@PathVariable Long id) {
|
|
|
|
public User getUser(@Parameter(description ="用户id" )@PathVariable Long id) {
|
|
|
|
return users.get(id);
|
|
|
|
log.info("根据id获取用户,id={}",id);
|
|
|
|
|
|
|
|
return userService.selectById(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@PutMapping("/{id}")
|
|
|
|
@PutMapping("/{id}")
|
|
|
|
@Operation(summary = "更新用户详细信息",description = "根据url的id指定更新对象,并根据传过来的user信息更新用户详细信息")
|
|
|
|
@Operation(summary = "更新用户详细信息",description = "根据url的id指定更新对象,并根据传过来的user信息更新用户详细信息")
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
public String putUser(@Parameter(description = "用户ID")@PathVariable Long id,@RequestBody(description = "用户实体类")
|
|
|
|
public String putUser(@Parameter(description = "用户ID")@PathVariable Long id,@RequestBody(description = "用户实体类")
|
|
|
|
@org.springframework.web.bind.annotation.RequestBody User user){
|
|
|
|
@org.springframework.web.bind.annotation.RequestBody User user){
|
|
|
|
User u = users.get(id);
|
|
|
|
log.info("更新用户,id={}",user.getId());
|
|
|
|
|
|
|
|
User u = userService.selectById(id);
|
|
|
|
u.setName(user.getName());
|
|
|
|
u.setName(user.getName());
|
|
|
|
u.setAge(user.getAge());
|
|
|
|
u.setAge(user.getAge());
|
|
|
|
users.put(id, u);
|
|
|
|
userService.update(u);
|
|
|
|
return "success";
|
|
|
|
return "success";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@DeleteMapping("/{id}")
|
|
|
|
@DeleteMapping("/{id}")
|
|
|
|
@Operation(summary = "删除用户",description = "根据url的id来指定删除对象")
|
|
|
|
@Operation(summary = "删除用户",description = "根据url的id来指定删除对象")
|
|
|
|
public String deleteUser(@Parameter(description = "用户ID")@PathVariable Long id) {
|
|
|
|
public String deleteUser(@Parameter(description = "用户ID")@PathVariable Long id) {
|
|
|
|
users.remove(id);
|
|
|
|
log.info("产出用户,id={}",id);
|
|
|
|
|
|
|
|
userService.remove(id);
|
|
|
|
return "success";
|
|
|
|
return "success";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|