Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions SETUP.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,19 @@ mutation {
}
}

###
GRAPHQL http://localhost:8080/graphql

{
listAll {
description
priority
tagList
creation
conclusion
}
}

###
```
3. Useful end-points:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public int newTask(Task task) {
}

@Query
@Description("Lists all tasks")
public List<Task> listAll() { //TODO[#2]: Implement the listAll() method to list all tasks.
return taskBaseDAO.getAllTasks();
}
Expand Down
43 changes: 41 additions & 2 deletions src/main/java/br/com/pedr0limpio/services/MySQLDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,47 @@ private void insertTaskTag(Connection conn, int taskId, int tagId) throws SQLExc
}

@Override
public List<Task> getAllTasks() { //TODO[#9]: Implement getAllTasks() to fetch in DB for all tasks.
return List.of();
public List<Task> getAllTasks() {
List<Task> tasks = new java.util.ArrayList<>();
String sql = "SELECT task_id, description, priority, created_at, conclusion_at FROM TASKS";
try (Connection conn = DriverManager.getConnection(url, username, password);
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery()) {

while (rs.next()) {
Task task = new Task();
int taskId = rs.getInt("task_id");
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, here we come to the part of incorporating GraphQL good practice into the code.

task.setDescription(rs.getString("description"));
task.setPriority(br.com.pedr0limpio.enums.Priority.valueOf(rs.getString("priority")));
task.setCreation(rs.getTimestamp("created_at"));
task.setConclusion(rs.getTimestamp("conclusion_at"));

// Fetch tags for this task
List<Tag> tags = new java.util.ArrayList<>();
String tagSql = "SELECT t.name FROM TAGS t " +
"JOIN TASK_TAGS tt ON t.tag_id = tt.tag_id " +
"WHERE tt.task_id = ?";
try (PreparedStatement tagStmt = conn.prepareStatement(tagSql)) {
tagStmt.setInt(1, taskId);
try (ResultSet tagRs = tagStmt.executeQuery()) {
while (tagRs.next()) {
String tagName = tagRs.getString("name");
try {
tags.add(Tag.valueOf(tagName));
} catch (IllegalArgumentException e) {
LOGGER.warn("Unknown tag: " + tagName);
}
}
}
}
task.setTagList(tags);

tasks.add(task);
}
} catch (SQLException e) {
LOGGER.error("Error fetching all tasks: " + e.getMessage());
}
return tasks;
}

@Override
Expand Down