Skip to content
Snippets Groups Projects
Commit 2809e372 authored by Yibo Zhu's avatar Yibo Zhu
Browse files

develope

parent f14a4762
No related branches found
No related tags found
3 merge requests!48Resolve issue 70,!47Revert "Merge branch '26-as-a-user-i-want-to-see-the-rankings-of-each-member' into 'main'",!33Resolve "As a user, I want to view and edit my personal information on my profile page so that I can manage and update my details"
package uk.ac.cf.spring.demo.sports.Userdetail; package uk.ac.cf.spring.demo.sports.Userdetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import java.util.List; import java.util.List;
@RestController
@Controller @Controller
public class UserController { public class UserController {
// 依赖注入 UserService
private final UserService userService; private final UserService userService;
// 使用 @Autowired 进行依赖注入(构造函数注入)
@Autowired
public UserController(UserService userService) { public UserController(UserService userService) {
this.userService = userService; this.userService = userService;
} }
// 显示所有用户列表 // 获取所有用户列表
@GetMapping("/users") @GetMapping
public ModelAndView getUsers() { public List<User> getAllUsers() {
ModelAndView modelAndView = new ModelAndView("user/allUserList"); return userService.getAllUsers();
List<User> users = userService.getAllUsers();
modelAndView.addObject("users", users);
return modelAndView;
} }
// 通过用户 ID 查看用户详情 // 通过用户 ID 获取用户详情
@GetMapping("/users/{id}") @GetMapping("/api/users/{id}")
public ModelAndView getUserById(@PathVariable Long id) { public User getUserById(@PathVariable Long id) {
ModelAndView modelAndView = new ModelAndView("user/userDetails"); return userService.getUserById(id);
User user = userService.getUserById(id);
modelAndView.addObject("user", user);
return modelAndView;
} }
// 显示添加用户的表单 // 添加用户
@GetMapping("/users/add") @PostMapping
public ModelAndView addUser() { public void addUser(@RequestBody User user) {
ModelAndView modelAndView = new ModelAndView("user/userForm"); userService.addUser(user);
Userform emptyUserform = new Userform();
modelAndView.addObject("user", emptyUserform);
return modelAndView;
} }
// 显示编辑用户信息的表单 // 更新用户信息
@GetMapping("/users/edit/{id}") // @PutMapping("/{id}")
public ModelAndView editUser(@PathVariable("id") Long id) { // public void updateUser(@PathVariable Long id, @RequestBody User user) {
ModelAndView modelAndView = new ModelAndView("user/userForm"); // user.setId(id); // 确保更新的对象有正确的 ID
// 获取需要编辑的用户 // userService.updateUser(user);
User userToEdit = userService.getUserById(id); // }
// 创建表单对象 // 删除用户
Userform userform = new Userform( @DeleteMapping("/{id}")
userToEdit.getId(), public void deleteUser(@PathVariable Long id) {
userToEdit.getEmail(), userService.deleteUser(id);
userToEdit.getUsername()
);
modelAndView.addObject("user", userform);
return modelAndView;
} }
} }
\ No newline at end of file
...@@ -18,4 +18,6 @@ public interface UserService { ...@@ -18,4 +18,6 @@ public interface UserService {
// 添加用户 // 添加用户
void addUser(User user); void addUser(User user);
void deleteUser(Long id);
} }
\ No newline at end of file
...@@ -34,4 +34,9 @@ public class UserServiceImpl implements UserService{ ...@@ -34,4 +34,9 @@ public class UserServiceImpl implements UserService{
public void addUser(User user) { public void addUser(User user) {
} }
@Override
public void deleteUser(Long id) {
}
} }
// userTable.js // userTable.js
// 动态加载用户数据 // 动态加载用户数据
async function loadUsers() { async function loadUsers(id) {
try { try {
const response = await fetch('/api/users'); // 确保后端提供了此 API const response = await fetch('/api/users/${id}'); // 确保后端提供了此 API
if (!response.ok) { if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`); throw new Error(`HTTP error! Status: ${response.status}`);
} }
...@@ -28,10 +28,9 @@ async function loadUsers() { ...@@ -28,10 +28,9 @@ async function loadUsers() {
`; `;
tbody.appendChild(row); tbody.appendChild(row);
}); });
} catch (error) { }
catch (error) {
console.error('Error loading users:', error); console.error('Error loading users:', error);
const tbody = document.querySelector('tbody');
tbody.innerHTML = `<tr><td colspan="4">Failed to load user data.</td></tr>`;
} }
} }
......
...@@ -3,8 +3,10 @@ ...@@ -3,8 +3,10 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Title</title> <title>Title</title>
<link rel="stylesheet" href="../css/UserCenter.css">
</head> </head>
<body> <body>
<table> <table>
<thead> <thead>
<tr> <tr>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment