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
20 changes: 20 additions & 0 deletions SETUP.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ GRAPHQL http://localhost:8080/graphql
mutation {
deleteById(id: 14)
}

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

mutation {
editById(id: 2, updatedTask: {
description: "Updated Task",
priority: Low,
tagList: [Personal],
creation: "2025-06-27T16:20:30.1Z",
conclusion: null
}) {
description
priority
tagList
creation
conclusion
}
}

###
```
3. Useful end-points:
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/br/com/pedr0limpio/resources/TaskResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,26 @@ public List<Task> listAll() { //TODO[#2]: Implement the listAll() method to list
return taskBaseDAO.getAllTasks();
}

@Query
@Description("Fetch a task by id")
public Task searchById(int id) {
try {
return taskBaseDAO.getById(id);
} catch (Exception e) {
LOG.error("Error fetching task by id", e);
throw new RuntimeException("Error fetching task by id", e);
@Query
@Description("Fetch a task by id")
public Task searchById(int id) {
try {
return taskBaseDAO.getById(id);
} catch (Exception e) {
LOG.error("Error fetching task by id", e);
throw new RuntimeException("Error fetching task by id", e);
}
}
}

@Mutation
@Transactional
public Task editById(int id, Task taskFor) { //TODO[#4]: Implement the editById(int id, Task taskFor) method to update a task
public Task editById(int id, Task updatedTask) {
try {
taskBaseDAO.update(id, taskFor);
return taskFor;
taskBaseDAO.update(id, updatedTask);
return updatedTask;
} catch (Exception e) {
LOG.error("Error editing task", e);
throw new RuntimeException("Error editing task", e);
LOG.error("Error updating task with id " + id, e);
throw new RuntimeException("Could not update task", e);
}
}

Expand Down
56 changes: 55 additions & 1 deletion src/main/java/br/com/pedr0limpio/services/MySQLDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,62 @@ public Task getById(int id) { //Implement getById to fetch in DB for a task.
}

@Override
public void update(int idFrom, Task taskFor) { //TODO[#11]: Implement update(int idFrom, Task taskFor) to update a task in DB.
public void update(int idFrom, Task taskFor) {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, username, password);
conn.setAutoCommit(false);

// update main task fields
String sql = "UPDATE TASKS SET description = ?, priority = ?, created_at = ?, conclusion_at = ? WHERE task_id = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, taskFor.getDescription());
stmt.setString(2, taskFor.getPriority().name());
stmt.setTimestamp(3, new Timestamp(taskFor.getCreation().getTime()));
if (taskFor.getConclusion() != null) {
stmt.setTimestamp(4, new Timestamp(taskFor.getConclusion().getTime()));
} else {
stmt.setNull(4, java.sql.Types.TIMESTAMP);
}
stmt.setInt(5, idFrom);
stmt.executeUpdate();
}

// remove old tags
String deleteTagsSql = "DELETE FROM TASK_TAGS WHERE task_id = ?";
try (PreparedStatement stmt = conn.prepareStatement(deleteTagsSql)) {
stmt.setInt(1, idFrom);
stmt.executeUpdate();
}

// insert new tags
if (taskFor.getTagList() != null) {
for (Tag tag : taskFor.getTagList()) {
int tagId = insertOrGetTagId(conn, tag.name());
insertTaskTag(conn, idFrom, tagId);
}
}

conn.commit();
} catch (SQLException e) {
if (conn != null) {
try {
conn.rollback();
} catch (SQLException ex) {
LOGGER.error("Rollback failed: " + ex.getMessage());
}
}
LOGGER.error("Update failed: " + e.getMessage());
throw new RuntimeException("Database update failed", e);
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
LOGGER.error("Connection close failed: " + e.getMessage());
}
}
}
}

@Override
Expand Down