83 lines
2.4 KiB
Java
83 lines
2.4 KiB
Java
package com.jnssd.controller;
|
|
|
|
import com.jnssd.model.Role;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
|
|
/**
|
|
* <h3>spring-boot-openapi</h3>
|
|
* <p>角色</p>
|
|
*
|
|
* @author zxj
|
|
* @since 2023-10-12 16:32:38
|
|
*/
|
|
@RestController
|
|
@RequestMapping("/Role")
|
|
public class RoleController {
|
|
|
|
final static String SUCCESS_TEXT = "操作成功!";
|
|
final static String FAIL_TEXT = "操作失败!";
|
|
|
|
List<Role> list = new java.util.ArrayList<>();
|
|
|
|
@GetMapping("/")
|
|
public ResponseEntity<List<Role>> getAll() {
|
|
return ResponseEntity.ok(list);
|
|
}
|
|
|
|
// 添加方法
|
|
@PostMapping("add")
|
|
public ResponseEntity<String> add(Role entity) {
|
|
try {
|
|
entity.setId(list.size() + 1);
|
|
list.add(entity);
|
|
return ResponseEntity.ok(SUCCESS_TEXT);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return ResponseEntity.status(400).body(FAIL_TEXT);
|
|
}
|
|
}
|
|
|
|
// 添加方法
|
|
@PostMapping("update")
|
|
public ResponseEntity<Object> update(Role entity) {
|
|
try {
|
|
// 修改list下面的数据
|
|
list.replaceAll((role) -> Objects.equals(role.getId(), entity.getId()) ? entity : role);
|
|
|
|
return ResponseEntity.ok(SUCCESS_TEXT);
|
|
} catch (Exception e) {
|
|
return ResponseEntity.status(400).body(FAIL_TEXT);
|
|
}
|
|
}
|
|
|
|
// 删除方法
|
|
@PostMapping("delete")
|
|
public ResponseEntity<String> delete(Role entity) {
|
|
try {
|
|
boolean result = list.removeIf(e -> Objects.equals(e.getId(), entity.getId()));
|
|
return result ? ResponseEntity.ok(SUCCESS_TEXT) : ResponseEntity.status(400).body(FAIL_TEXT);
|
|
} catch (Exception e) {
|
|
return ResponseEntity.status(400).body(null);
|
|
}
|
|
}
|
|
|
|
// 查询方法
|
|
@PostMapping("query")
|
|
public ResponseEntity<Object> query(Integer id) {
|
|
try {
|
|
Role role = (Role) list.stream().filter(e -> Objects.equals(e.getId(), id));
|
|
// list 下查询
|
|
return ResponseEntity.ok(role);
|
|
} catch (Exception e) {
|
|
return ResponseEntity.status(400).body(FAIL_TEXT);
|
|
}
|
|
}
|
|
}
|