From 95c09a607f49780ed1ec3cdbe32490f3f2a5d264 Mon Sep 17 00:00:00 2001 From: silrenan <75952546+silrenan@users.noreply.github.com> Date: Mon, 2 Jun 2025 10:13:14 -0300 Subject: [PATCH] implement update method in MySQLDAO and editById in TaskResource to update tasks in the database Signed-off-by: silrenan <75952546+silrenan@users.noreply.github.com>renan <75952546+silrenan@users.noreply.github.com> Signed-off-by: sirenan <75952546+silrenan@users.noreply.github.com> --- SETUP.md | 20 +++++++ .../pedr0limpio/resources/TaskResource.java | 28 +++++----- .../br/com/pedr0limpio/services/MySQLDAO.java | 56 ++++++++++++++++++- 3 files changed, 89 insertions(+), 15 deletions(-) diff --git a/SETUP.md b/SETUP.md index 6f20de8..5d05621 100644 --- a/SETUP.md +++ b/SETUP.md @@ -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: diff --git a/src/main/java/br/com/pedr0limpio/resources/TaskResource.java b/src/main/java/br/com/pedr0limpio/resources/TaskResource.java index d82932d..67b9356 100644 --- a/src/main/java/br/com/pedr0limpio/resources/TaskResource.java +++ b/src/main/java/br/com/pedr0limpio/resources/TaskResource.java @@ -39,26 +39,26 @@ public List 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); } } diff --git a/src/main/java/br/com/pedr0limpio/services/MySQLDAO.java b/src/main/java/br/com/pedr0limpio/services/MySQLDAO.java index d67c9fa..4bed662 100644 --- a/src/main/java/br/com/pedr0limpio/services/MySQLDAO.java +++ b/src/main/java/br/com/pedr0limpio/services/MySQLDAO.java @@ -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