Skip to content

Commit

Permalink
codesquad-issue-team-05#80 feat : issue 전체 조회에서 이슈들 상태변경 api 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
DOEKYONG committed Aug 14, 2023
1 parent c51d2cb commit db405cc
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public enum ErrorCode implements StatusCode {
ALREADY_DELETED_COMMENT(HttpStatus.BAD_REQUEST, "이미 삭제된 댓글입니다."),
FAILED_CREATE_COMMENT(HttpStatus.SERVICE_UNAVAILABLE, "DB 서버 오류로 인해 댓글 생성에 실패했습니다."),
FAILED_UPDATE_COMMENT(HttpStatus.SERVICE_UNAVAILABLE, "DB 서버 오류로 인해 댓글 수정에 실패했습니다."),
FAILED_DELETE_COMMENT(HttpStatus.SERVICE_UNAVAILABLE,"DB 서버 오류로 인해 댓글 삭제에 실패했습니다."),
FAILED_DELETE_COMMENT(HttpStatus.SERVICE_UNAVAILABLE, "DB 서버 오류로 인해 댓글 삭제에 실패했습니다."),

// -- [Milestone] -- //
INVALIDATE_DATE(HttpStatus.BAD_REQUEST, "현재 날짜보다 이전 날짜 입니다."),
Expand All @@ -51,10 +51,9 @@ public enum ErrorCode implements StatusCode {
LABEL_DELETE_FAILED(HttpStatus.BAD_REQUEST, "DB에서 라벨 삭제에 실패했습니다"),
LABEL_FIND_FAILED(HttpStatus.BAD_REQUEST, "서버 오류로 라벨을 조회할 수 없습니다"),


// -- [Issue] -- //
DUPLICATE_OBJECT_FOUND(HttpStatus.BAD_REQUEST, "중복된 항목 선택입니다.");

DUPLICATE_OBJECT_FOUND(HttpStatus.BAD_REQUEST, "중복된 항목 선택입니다."),
NOT_FOUND_ISSUES(HttpStatus.BAD_REQUEST, "이슈를 찾을 수 없습니다.");

private HttpStatus status;
private String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import codesquad.issueTracker.global.common.ApiResponse;
import codesquad.issueTracker.issue.dto.IssueWriteRequestDto;
import codesquad.issueTracker.issue.dto.ModifyIssueStatusRequestDto;
import codesquad.issueTracker.issue.service.IssueService;
import lombok.RequiredArgsConstructor;

Expand All @@ -29,4 +31,11 @@ public ApiResponse<String> postIssues(@Valid @RequestBody IssueWriteRequestDto r
issueService.save(request, id);
return ApiResponse.success(SUCCESS.getStatus(), SUCCESS.getMessage());
}

@PatchMapping("/issues/status")
public ApiResponse<String> patchStatus(@RequestBody ModifyIssueStatusRequestDto request) {
issueService.modifyIssueStatus(request);
return ApiResponse.success(SUCCESS.getStatus(), SUCCESS.getMessage());
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package codesquad.issueTracker.issue.repository;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Optional;

import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
Expand Down Expand Up @@ -49,4 +55,31 @@ public Long insertAssignees(Long issueId, Long userId) {
jdbcTemplate.update(sql, parameters, keyHolder);
return keyHolder.getKey().longValue();
}

public Optional<Issue> findById(Long id) {
String sql = "SELECT id, milestone_id, user_id, title, content, created_at, is_closed FROM issues WHERE id = :id AND is_deleted = 0";
return Optional.ofNullable(
DataAccessUtils.singleResult(
jdbcTemplate.query(sql, Map.of("id", id), issueRowMapper)));
}

public Long modifyStatus(Long issueId, Boolean status) {
String sql = "UPDATE issues SET is_closed = :status where id = :id AND is_deleted = 0";
SqlParameterSource parameterSource = new MapSqlParameterSource()
.addValue("id", issueId)
.addValue("status", status);
jdbcTemplate.update(sql, parameterSource);
return issueId;
}

private final RowMapper<Issue> issueRowMapper = (rs, rowNum) -> Issue.builder()
.id(rs.getLong("id"))
.milestoneId(rs.getLong("milestone_id"))
.userId(rs.getLong("user_id"))
.title(rs.getString("title"))
.content(rs.getString("content"))
.createdAt(rs.getObject("created_at", LocalDateTime.class))
.isClosed(rs.getBoolean("is_closed"))
.build();

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import org.springframework.transaction.annotation.Transactional;

import codesquad.issueTracker.comment.service.CommentService;
import codesquad.issueTracker.global.common.Status;
import codesquad.issueTracker.global.exception.CustomException;
import codesquad.issueTracker.global.exception.ErrorCode;
import codesquad.issueTracker.issue.domain.Issue;
import codesquad.issueTracker.issue.dto.IssueWriteRequestDto;
import codesquad.issueTracker.issue.dto.ModifyIssueStatusRequestDto;
import codesquad.issueTracker.issue.repository.IssueRepository;
import codesquad.issueTracker.label.service.LabelService;
import codesquad.issueTracker.milestone.service.MilestoneService;
Expand Down Expand Up @@ -66,4 +68,20 @@ private void duplicatedId(List<Long> list) {
}
}
}

@Transactional
public List<Long> modifyIssueStatus(ModifyIssueStatusRequestDto request) {
List<Long> issueIds = request.getIssueIds();
Boolean status = Status.from(request.getStatus()).getStatus();
duplicatedId(issueIds);
validateExistIssue(issueIds);
return issueIds.stream()
.map(issueId -> issueRepository.modifyStatus(issueId, status)).collect(Collectors.toList());
}

private void validateExistIssue(List<Long> issuesIds) {
for (Long issueId : issuesIds) {
issueRepository.findById(issueId).orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_ISSUES));
}
}
}

0 comments on commit db405cc

Please sign in to comment.