Skip to content

Commit

Permalink
[BE/#17] Feat : LabelDao 쿼리 설계
Browse files Browse the repository at this point in the history
- 모든 레이블 목록을 반환하는 쿼리 추가
- 해당 이슈에 할당된 레이블을 반환하는 쿼리 추가
- SQL을 최대한 작은 단위로 활용
  • Loading branch information
MuseopKim committed Jun 12, 2020
1 parent d93bc4c commit b740458
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.codesquad.issuetracker.ragdoll.dao;

import com.codesquad.issuetracker.ragdoll.domain.Label;
import com.codesquad.issuetracker.ragdoll.dto.labelVO.LabelSummary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.util.List;

@Repository
public class LabelDao {

private JdbcTemplate jdbcTemplate;

public LabelDao(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}

public List<LabelSummary> findAttachedLabelsByIssueId(Long issueId) {
String sql = "SELECT l.id, l.name, l.hex_code " +
"FROM label l JOIN issue_has_label il ON l.id = il.label_id " +
"WHERE il.issue_id = ?";
return jdbcTemplate.query(sql, new Object[]{issueId},
(rs, rowNum) -> LabelSummary.create(rs.getInt("l.id"), rs.getString("l.name"),
rs.getString("l.hex_code")));
}

public List<Label> findAllLabels() {
String sql = "SELECT id, name, description, hex_code FROM label";
return jdbcTemplate.query(sql, (rs, rowNum) -> Label.create(rs.getInt("id"), rs.getString("name"),
rs.getString("description"), rs.getString("hex_code")));
}
}

0 comments on commit b740458

Please sign in to comment.