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
5 changes: 5 additions & 0 deletions SETUP.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ POST http://localhost:8080/graphql
})\n
}"
}
###
GRAPHQL http://localhost:8080/graphql

mutation {
deleteById(id: 14)
}
###
```
3. Useful end-points:
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/br/com/pedr0limpio/resources/TaskResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,14 @@ public Task editById(int id, Task taskFor) { //TODO[#4]: Implement the editById(

@Mutation
@Transactional
public void deleteById(int id) { //TODO[#5]: Implement the deleteById(int id) method to delete a task.
public String deleteById(int 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.

The String return type is an anti-pattern in this specific case. I'll approve it anyway. Everything else is fine. Let's create an issue to implement the best practices for API design with GraphQL later.

try {
Task existing = taskBaseDAO.getById(id);
if (existing == null) {
return "task id " + id + " not found";
}
taskBaseDAO.removeById(id);
return "task id " + id + " deleted";
} catch (Exception e) {
LOG.error("Error deleting task", e);
throw new RuntimeException("Error deleting task", e);
Expand Down
27 changes: 24 additions & 3 deletions src/main/java/br/com/pedr0limpio/services/MySQLDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,29 @@ public void update(int idFrom, Task taskFor) { //TODO[#11]: Implement update(int
}

@Override
public void removeById(int id) { //TODO[#12]: Implement removeById(int id) to delete a task by id in DB.

public boolean removeById(int id) {
String deleteTaskTagsSql = "DELETE FROM TASK_TAGS WHERE task_id = ?";
String deleteTaskSql = "DELETE FROM TASKS WHERE task_id = ?";
int affectedRows = 0;
try (Connection conn = DriverManager.getConnection(url, username, password)) {
conn.setAutoCommit(false);
try (PreparedStatement stmt1 = conn.prepareStatement(deleteTaskTagsSql)) {
stmt1.setInt(1, id);
stmt1.executeUpdate();
}
try (PreparedStatement stmt2 = conn.prepareStatement(deleteTaskSql)) {
stmt2.setInt(1, id);
affectedRows = stmt2.executeUpdate();
}
conn.commit();
} catch (SQLException e) {
LOGGER.error(e.getMessage());
try (Connection conn = DriverManager.getConnection(url, username, password)) {
conn.rollback();
} catch (SQLException rollbackException) {
LOGGER.error(rollbackException.getMessage());
}
}
return affectedRows > 0;
}
}

2 changes: 1 addition & 1 deletion src/main/java/br/com/pedr0limpio/services/TaskBaseDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public abstract class TaskBaseDAO {
public abstract List<Task> getAllTasks();
public abstract Task getById(int id);
public abstract void update(int idFrom, Task taskFor);
public abstract void removeById(int id);
public abstract boolean removeById(int id);
}