Skip to content

Commit 7095ea8

Browse files
fix: Fix project endpoint.
1 parent b24ed81 commit 7095ea8

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

src/main/java/net/modgarden/backend/data/event/Project.java

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,17 @@ public static void getProject(Context ctx) {
5959

6060
public static Project queryFromId(String id) {
6161
try (Connection connection = ModGardenBackend.createDatabaseConnection();
62-
PreparedStatement prepared = connection.prepareStatement(selectStatement("id = ?"))) {
62+
PreparedStatement prepared = connection.prepareStatement(selectStatement())) {
6363
prepared.setString(1, id);
6464
ResultSet result = prepared.executeQuery();
6565
if (!result.isBeforeFirst())
6666
return null;
67-
List<String> authors = List.of(result.getString("authors").split(","));
68-
List<String> builders = List.of(result.getString("builders").split(","));
67+
List<String> authors = Arrays.stream(result.getString("authors").split(","))
68+
.filter(s -> !s.isBlank())
69+
.toList();
70+
List<String> builders = Arrays.stream(result.getString("builders").split(","))
71+
.filter(s -> !s.isBlank())
72+
.toList();
6973
return new Project(
7074
result.getString("id"),
7175
result.getString("slug"),
@@ -157,31 +161,29 @@ public static void getProjectsByEvent(Context ctx) {
157161
}
158162

159163

160-
private static String selectStatement(String whereStatement) {
161-
return "SELECT " +
162-
"p.id, " +
163-
"p.slug, " +
164-
"p.modrinth_id, " +
165-
"p.attributed_to, " +
166-
"CASE " +
167-
"WHEN a.user_id IS NOT NULL THEN group_concat(DISTINCT a.user_id)" +
168-
"ELSE '' " +
169-
"END AS authors " +
170-
"CASE " +
171-
"WHEN b.user_id IS NOT NULL THEN group_concat(DISTINCT b.user_id)" +
172-
"ELSE '' " +
173-
"END AS builders " +
174-
"FROM " +
175-
"projects p " +
176-
"LEFT JOIN " +
177-
"project_authors a ON p.id = a.project_id " +
178-
"LEFT JOIN " +
179-
"project_builders b ON p.id = b.project_id " +
180-
"WHERE " +
181-
"p." + whereStatement + " " +
182-
"GROUP BY " +
183-
"p.id, p.slug, p.modrinth_id, p.attributed_to";
184-
}
164+
private static String selectStatement() {
165+
return """
166+
SELECT
167+
p.id,
168+
p.slug,
169+
p.modrinth_id,
170+
p.attributed_to,
171+
COALESCE(Group_concat(DISTINCT a.user_id), '') AS authors,
172+
COALESCE(Group_concat(DISTINCT b.user_id), '') AS builders
173+
FROM projects p
174+
LEFT JOIN project_authors a
175+
ON p.id = a.project_id
176+
LEFT JOIN project_builders b
177+
ON p.id = b.project_id
178+
WHERE
179+
p.id = ?
180+
GROUP BY
181+
p.id,
182+
p.slug,
183+
p.modrinth_id,
184+
p.attributed_to
185+
""";
186+
}
185187

186188
private static String selectAllByUser() {
187189
return """

0 commit comments

Comments
 (0)