Skip to content

Commit

Permalink
[BE/#58] Feat : 필터 기능 DAO 쿼리 추가
Browse files Browse the repository at this point in the history
- 클래스로 처리하여 null 값을 활용
- COALESCE 함수 활용
  • Loading branch information
MuseopKim committed Jun 18, 2020
1 parent c7b9389 commit 57c3204
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.codesquad.issuetracker.ragdoll.domain.Comment;
import com.codesquad.issuetracker.ragdoll.domain.Issue;
import com.codesquad.issuetracker.ragdoll.dto.FilterParameters;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
Expand Down Expand Up @@ -147,4 +148,34 @@ public void updateComment(Long issueId, Long commentId, String description) {
String sql = "UPDATE comment SET description = ? WHERE issue_id = ? AND id = ?";
jdbcTemplate.update(sql, new Object[]{description, issueId, commentId});
}

public List<Issue> findIssuesByFilterParameters(FilterParameters filterParameters) {
String sql = "SELECT i.id, i.title, i.created_date_time, i.is_opened, i.user_id, i.milestone_id " +
"FROM issue i LEFT OUTER JOIN issue_has_label il ON i.id = il.issue_id " +
"LEFT OUTER JOIN assignee a ON i.id = a.issue_id " +
"WHERE i.is_opened = COALESCE(:open, true) " +
"AND " +
"i.user_id = COALESCE(:author, user_id) " +
"AND " +
"il.label = COALESCE(:label, label) " +
"AND " +
"i.milestone_id = COALESCE(:milestones, milestone_id) " +
"AND " +
"a.user_id = COALESCE(:assignee, user_id)";
SqlParameterSource namedParameters = new MapSqlParameterSource()
.addValue("open", filterParameters.getOpen())
.addValue("author", filterParameters.getAuthor())
.addValue("label", filterParameters.getLabel())
.addValue("milestones", filterParameters.getMilestones())
.addValue("assignee", filterParameters.getAssignee());
return namedParameterJdbcTemplate.query(sql, namedParameters,
(rs, rowNum) -> new Issue.Builder()
.id(rs.getLong("i.id"))
.title(rs.getString("i.title"))
.createdDateTime(rs.getTimestamp("i.created_date_time").toLocalDateTime())
.opened(rs.getBoolean("is_opened"))
.userId(rs.getLong("user_id"))
.milestoneId(rs.getInt("milestone_id"))
.build());
}
}

0 comments on commit 57c3204

Please sign in to comment.