저번에 오류 수정한 부분의 중복되는 코드를 없애고 코드 전체 길이를 확 줄였다.
return this.jdbcTemplate.query(getReplyListQuery,
(rs, rowNum) -> {
GetReplyRes getReplyRes;
if(rs.getString("replyImgUrl") != null) {
getReplyRes = new GetReplyRes(
rs.getLong("replyIdx"),
rs.getLong("questionIdx"),
rs.getLong("userIdx"),
rs.getString("nickname"),
rs.getString("profileImgUrl"),
rs.getString("createdAt"),
rs.getString("replyUrl"),
rs.getString("content"),
Arrays.asList(rs.getString("replyImgUrl").split(",")),
rs.getInt("likeCount"),
rs.getInt("reReplyCount"),
rs.getString("status"),
rs.getString("isLiked"));
} else {
getReplyRes = new GetReplyRes(
rs.getLong("replyIdx"),
rs.getLong("questionIdx"),
rs.getLong("userIdx"),
rs.getString("nickname"),
rs.getString("profileImgUrl"),
rs.getString("createdAt"),
rs.getString("replyUrl"),
rs.getString("content"),
Arrays.asList(),
rs.getInt("likeCount"),
rs.getInt("reReplyCount"),
rs.getString("status"),
rs.getString("isLiked"));
}
return getReplyRes;
}, userIdx, questionIdx);
return this.jdbcTemplate.query(getReplyListQuery,
(rs, rowNum) -> {
GetReplyRes getReplyRes;
List<String> replyImgUrlList = new ArrayList<>();
if(rs.getString("replyImgUrl") != null) {
replyImgUrlList = Arrays.asList(rs.getString("replyImgUrl").split(","));
}
return getReplyRes = new GetReplyRes(rs.getLong("replyIdx"),
rs.getLong("questionIdx"),
rs.getLong("userIdx"),
rs.getString("nickname"),
rs.getString("profileImgUrl"),
rs.getString("createdAt"),
rs.getString("replyUrl"),
rs.getString("content"),
replyImgUrlList,
rs.getInt("likeCount"),
rs.getInt("reReplyCount"),
rs.getString("status"),
rs.getString("isLiked"));
}, userIdx, questionIdx);
}
Arrays.asList() :
sql문 돌려서 받아온 String[] 데이터(String split해서 배열로 받음)를 리스트로 변경한다.
List 변수를 미리 별도로 생성해주고 생성자에 변수를 이용한다.
항상 중복 코드를 최소한으로 구성하자!!!