新增游客查看各个用户项目的功能之显示用户列表后端

This commit is contained in:
高子兴 2024-07-14 17:50:51 +08:00
parent 5c2ae6902e
commit fb3abb686b
2 changed files with 37 additions and 0 deletions

View File

@ -188,6 +188,21 @@ public class DatabaseService {
return null; return null;
} }
public ArrayList<String> getAllUsers() {
ArrayList<String> users = new ArrayList<>();
String sql = "SELECT username FROM users";
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
users.add(rs.getString("username"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return users;
}
public void saveProjects(String username, List<Project> projects) throws SQLException { public void saveProjects(String username, List<Project> projects) throws SQLException {
try (Connection conn = getConnection()) { try (Connection conn = getConnection()) {
for (Project project : projects) { for (Project project : projects) {

View File

@ -37,6 +37,8 @@ public class ProjectBean implements Serializable {
return "jdbc:derby:testDB;create=true"; return "jdbc:derby:testDB;create=true";
} }
private List<String> users;
@PostConstruct @PostConstruct
public void init() { public void init() {
loadProjects(); // Load projects from the database loadProjects(); // Load projects from the database
@ -46,6 +48,7 @@ public class ProjectBean implements Serializable {
Project emptyProject = new Project("", "", "", new ArrayList<>(), "", new ArrayList<>(), "", ""); Project emptyProject = new Project("", "", "", new ArrayList<>(), "", new ArrayList<>(), "", "");
} }
setProject(projects.get(0)); // Set the first project as the current project setProject(projects.get(0)); // Set the first project as the current project
users = databaseService.getAllUsers();
} }
@PreDestroy @PreDestroy
@ -202,6 +205,17 @@ public class ProjectBean implements Serializable {
return username; return username;
} }
public String visitorSetUsername(String username) {
this.username = username;
loadProjects();
if (!projects.isEmpty()) {
setProject(projects.get(0));
} else {
setProject(new Project("", "", "", new ArrayList<>(), "", new ArrayList<>(), "", ""));
}
return "project";
}
public void setUsername(String username) { public void setUsername(String username) {
this.username = username; this.username = username;
} }
@ -273,4 +287,12 @@ public class ProjectBean implements Serializable {
public Boolean getIsLoggedIn() { public Boolean getIsLoggedIn() {
return isLoggedIn; return isLoggedIn;
} }
public List<String> getUsers() {
return users;
}
public void setUsers(List<String> users) {
this.users = users;
}
} }