Skip to content

Commit

Permalink
Merge pull request codesquad-issue-team-05#92 from DOEKYONG/be
Browse files Browse the repository at this point in the history
codesquad-issue-team-05#80 fix : merge conflict 해결
  • Loading branch information
DOEKYONG authored Aug 14, 2023
2 parents 99373d6 + 8a159a5 commit 3ec9f59
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@

import static codesquad.issueTracker.global.exception.SuccessCode.*;

import codesquad.issueTracker.issue.dto.IssueLabelResponseDto;
import codesquad.issueTracker.issue.dto.IssueMilestoneResponseDto;
import codesquad.issueTracker.issue.dto.IssueOptionResponseDto;
import codesquad.issueTracker.issue.dto.IssueResponseDto;
import codesquad.issueTracker.issue.dto.IssueUserResponseDto;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -21,6 +17,11 @@
import org.springframework.web.bind.annotation.RestController;

import codesquad.issueTracker.global.common.ApiResponse;
import codesquad.issueTracker.issue.dto.IssueLabelResponseDto;
import codesquad.issueTracker.issue.dto.IssueMilestoneResponseDto;
import codesquad.issueTracker.issue.dto.IssueOptionResponseDto;
import codesquad.issueTracker.issue.dto.IssueResponseDto;
import codesquad.issueTracker.issue.dto.IssueUserResponseDto;
import codesquad.issueTracker.issue.dto.IssueWriteRequestDto;
import codesquad.issueTracker.issue.dto.ModifyAssigneeRequestDto;
import codesquad.issueTracker.issue.dto.ModifyIssueContentRequestDto;
Expand All @@ -42,13 +43,12 @@ public class IssueController {

@PostMapping("/issues")
public ApiResponse<String> postIssues(@Valid @RequestBody IssueWriteRequestDto request,
HttpServletRequest httpServletRequest) {
HttpServletRequest httpServletRequest) {
Long id = Long.parseLong(String.valueOf(httpServletRequest.getAttribute("userId")));
issueService.save(request, id);
return ApiResponse.success(SUCCESS.getStatus(), SUCCESS.getMessage());
}


@GetMapping("/issues/labels")
public ApiResponse<List<IssueLabelResponseDto>> getIssueLabels() {
List<IssueLabelResponseDto> labels = issueService.getIssueLabels();
Expand Down Expand Up @@ -77,6 +77,7 @@ public ApiResponse<IssueResponseDto> getIssue(@PathVariable Long issueId) {
public ApiResponse<IssueOptionResponseDto> getIssueOptions(@PathVariable Long issueId) {
IssueOptionResponseDto issueOptionResponseDto = issueService.getIssueOptions(issueId);
return ApiResponse.success(SUCCESS.getStatus(), issueOptionResponseDto);
}

@PatchMapping("/issues/status")
public ApiResponse<String> patchStatus(@RequestBody ModifyIssueStatusRequestDto request) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package codesquad.issueTracker.issue.repository;


import codesquad.issueTracker.issue.vo.AssigneeVo;
import codesquad.issueTracker.issue.vo.IssueMilestoneVo;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.time.LocalDateTime;



import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.JdbcTemplate;
Expand All @@ -21,6 +16,8 @@
import org.springframework.stereotype.Repository;

import codesquad.issueTracker.issue.domain.Issue;
import codesquad.issueTracker.issue.vo.AssigneeVo;
import codesquad.issueTracker.issue.vo.IssueMilestoneVo;

@Repository
public class IssueRepository {
Expand Down Expand Up @@ -62,60 +59,37 @@ public Long insertAssignees(Long issueId, Long userId) {
return keyHolder.getKey().longValue();
}


public Optional<Issue> findActiveIssueById(Long issueId) {
String sql = "SELECT * FROM issues WHERE id = :issueId AND is_deleted = false";
return Optional.ofNullable(
DataAccessUtils.singleResult(
jdbcTemplate.query(sql, Map.of("issueId", issueId), issueRowMapper)));
}

private final RowMapper<Issue> issueRowMapper = ((rs, rowNum) -> Issue.builder()
.id(rs.getLong("id"))
.title(rs.getString("title"))
.content(rs.getString("content"))
.createdAt(rs.getTimestamp("created_at").toLocalDateTime())
.isClosed(rs.isClosed())
.build());

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

public List<AssigneeVo> findAssigneesById(Long issueId) {
String sql = "select u.id, u.name, u.profile_img "
+ "from assignees a "
+ " join users u on a.user_id = u.id "
+ " join issues i on a.issue_id = i.user_id "
+ "where i.id = :issueId "
+ "AND i.is_deleted = false";
return jdbcTemplate.query(sql, Map.of("issueId",issueId), assigneeVoRowMapper);
+ "from assignees a "
+ " join users u on a.user_id = u.id "
+ " join issues i on a.issue_id = i.user_id "
+ "where i.id = :issueId "
+ "AND i.is_deleted = false";
return jdbcTemplate.query(sql, Map.of("issueId", issueId), assigneeVoRowMapper);
}

private final RowMapper<AssigneeVo> assigneeVoRowMapper = ((rs, rowNum) -> AssigneeVo.builder()
.id(rs.getLong("id"))
.name(rs.getString("name"))
.imgUrl(rs.getString("profile_img"))
.build());


public int findCountByStatusAndMilestone(boolean status, IssueMilestoneVo milestone) {
String sql = "select COUNT(i.id) as count "
+ "from issues i "
+ " join milestones m on m.id = i.milestone_id "
+ "where m.id = :milestoneId "
+ "and i.is_deleted = false "
+ "and i.is_closed = :status";
+ "from issues i "
+ " join milestones m on m.id = i.milestone_id "
+ "where m.id = :milestoneId "
+ "and i.is_deleted = false "
+ "and i.is_closed = :status";
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("milestoneId", milestone.getId());
params.addValue("status", status);
return jdbcTemplate.queryForObject(sql, params, Integer.class);
}
public Optional<Issue> findById(Long id) {
}

public Optional<Issue> findActiveIssueById(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(
Expand All @@ -131,16 +105,6 @@ public Long modifyStatus(Long issueId, Boolean status) {
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();

public Long updateContent(Long id, String modifiedContent) {
String sql = "UPDATE issues SET content = :modifiedContent WHERE Id = :id";
SqlParameterSource parameterSource = new MapSqlParameterSource()
Expand Down Expand Up @@ -183,5 +147,21 @@ public Long updateMilestone(Long issueId, Long milestoneId) {
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();

private final RowMapper<AssigneeVo> assigneeVoRowMapper = ((rs, rowNum) -> AssigneeVo.builder()
.id(rs.getLong("id"))
.name(rs.getString("name"))
.imgUrl(rs.getString("profile_img"))
.build());

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
package codesquad.issueTracker.issue.service;

import codesquad.issueTracker.global.common.Status;
import codesquad.issueTracker.issue.dto.IssueOptionResponseDto;
import codesquad.issueTracker.issue.dto.IssueLabelResponseDto;
import codesquad.issueTracker.issue.dto.IssueMilestoneResponseDto;
import codesquad.issueTracker.issue.vo.IssueLabelVo;
import codesquad.issueTracker.issue.vo.IssueMilestoneVo;
import codesquad.issueTracker.issue.dto.IssueResponseDto;
import codesquad.issueTracker.issue.dto.IssueUserResponseDto;
import codesquad.issueTracker.issue.vo.AssigneeVo;
import codesquad.issueTracker.label.dto.LabelResponseDto;

import codesquad.issueTracker.milestone.vo.MilestoneVo;
import codesquad.issueTracker.user.domain.User;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -21,12 +8,15 @@
import org.springframework.stereotype.Service;
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.IssueLabelResponseDto;
import codesquad.issueTracker.issue.dto.IssueMilestoneResponseDto;
import codesquad.issueTracker.issue.dto.IssueOptionResponseDto;
import codesquad.issueTracker.issue.dto.IssueResponseDto;
import codesquad.issueTracker.issue.dto.IssueUserResponseDto;
import codesquad.issueTracker.issue.dto.IssueWriteRequestDto;
import codesquad.issueTracker.issue.dto.ModifyAssigneeRequestDto;
import codesquad.issueTracker.issue.dto.ModifyIssueContentRequestDto;
Expand All @@ -37,8 +27,14 @@
import codesquad.issueTracker.issue.dto.ModifyIssueTitleResponse;
import codesquad.issueTracker.issue.dto.ModifyLabelRequestDto;
import codesquad.issueTracker.issue.repository.IssueRepository;
import codesquad.issueTracker.issue.vo.AssigneeVo;
import codesquad.issueTracker.issue.vo.IssueLabelVo;
import codesquad.issueTracker.issue.vo.IssueMilestoneVo;
import codesquad.issueTracker.label.dto.LabelResponseDto;
import codesquad.issueTracker.label.service.LabelService;
import codesquad.issueTracker.milestone.service.MilestoneService;
import codesquad.issueTracker.milestone.vo.MilestoneVo;
import codesquad.issueTracker.user.domain.User;
import codesquad.issueTracker.user.service.UserService;
import lombok.RequiredArgsConstructor;

Expand Down Expand Up @@ -89,26 +85,25 @@ private void duplicatedId(List<Long> list) {
}
}


public List<IssueLabelResponseDto> getIssueLabels() {
LabelResponseDto allLabels = labelService.findAll();
return allLabels.getLabels().stream()
.map(IssueLabelResponseDto::from)
.collect(Collectors.toList());
.map(IssueLabelResponseDto::from)
.collect(Collectors.toList());
}

public List<IssueMilestoneResponseDto> getIssueMilestones() {
List<MilestoneVo> milestones = milestoneService.findMilestonesByStatus(Status.OPEN.getStatus());
return milestones.stream()
.map(IssueMilestoneResponseDto::from)
.collect(Collectors.toList());
.map(IssueMilestoneResponseDto::from)
.collect(Collectors.toList());
}

public List<IssueUserResponseDto> getIssueUsers() {
List<User> users = userService.getUsers();
return users.stream()
.map(IssueUserResponseDto::from)
.collect(Collectors.toList());
.map(IssueUserResponseDto::from)
.collect(Collectors.toList());
}

public IssueResponseDto getIssueById(Long issueId) {
Expand All @@ -118,14 +113,9 @@ public IssueResponseDto getIssueById(Long issueId) {
return IssueResponseDto.from(issue);
}

private void validateExistIssue(Long issueId) {
issueRepository.findById(issueId)
.orElseThrow(() -> new CustomException(ErrorCode.NOT_EXIST_ISSUE));
}

private Issue validateActiveIssueById(Long issueId) {
return issueRepository.findActiveIssueById(issueId)
.orElseThrow(() -> new CustomException(ErrorCode.ALREADY_DELETED_ISSUE));
.orElseThrow(() -> new CustomException(ErrorCode.ALREADY_DELETED_ISSUE));
}

public IssueOptionResponseDto getIssueOptions(Long issueId) {
Expand All @@ -143,7 +133,7 @@ public IssueOptionResponseDto getIssueOptions(Long issueId) {
}

return IssueOptionResponseDto.of(assignees, labels, milestone);
}
}

@Transactional
public List<Long> modifyIssueStatus(ModifyIssueStatusRequestDto request) {
Expand All @@ -152,7 +142,7 @@ public List<Long> modifyIssueStatus(ModifyIssueStatusRequestDto request) {
if (issueIds != null) {
duplicatedId(issueIds);
issueIds.stream()
.map(issueId -> validateExistIssue(issueId))
.map(issueId -> validateExistActiveIssue(issueId))
.map(existIssue -> issueRepository.modifyStatus(existIssue.getId(), status))
.collect(Collectors.toList());
}
Expand All @@ -162,41 +152,47 @@ public List<Long> modifyIssueStatus(ModifyIssueStatusRequestDto request) {
@Transactional
public Long modifyIssueStatusInDetail(Long id, ModifyIssueStatusRequestDto request) {
Boolean status = Status.from(request.getStatus()).getStatus();
validateExistIssue(id);
validateExistActiveIssue(id);
return issueRepository.modifyStatus(id, status);
}

private Issue validateExistIssue(Long issuesIds) {
return issueRepository.findById(issuesIds).orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_ISSUES));
private Issue validateExistActiveIssue(Long issuesIds) {
return issueRepository.findActiveIssueById(issuesIds)
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_ISSUES));
}

private void validateExistIssue(Long issueId) {
issueRepository.findById(issueId)
.orElseThrow(() -> new CustomException(ErrorCode.NOT_EXIST_ISSUE));
}

@Transactional
public ModifyIssueContentResponseDto modifyIssueContent(Long id, ModifyIssueContentRequestDto request) {
validateExistIssue(id);
validateExistActiveIssue(id);
String modifiedContent = request.getContent();
issueRepository.updateContent(id, modifiedContent);
return new ModifyIssueContentResponseDto(modifiedContent);
}

@Transactional
public ModifyIssueTitleResponse modifyIssueTitle(Long id, ModifyIssueTitleRequest request) {
validateExistIssue(id);
validateExistActiveIssue(id);
String modifiedTitle = request.getTitle();
issueRepository.updateTitle(id, modifiedTitle);
return new ModifyIssueTitleResponse(modifiedTitle);
}

@Transactional
public Long delete(Long id) {
validateExistIssue(id);
validateExistActiveIssue(id);
Long deletedId = issueRepository.delete(id);
return deletedId;

}

@Transactional
public Long modifyAssignees(Long id, ModifyAssigneeRequestDto request) {
validateExistIssue(id);
validateExistActiveIssue(id);
List<Long> assignees = request.getAssignees();

if (assignees != null) {
Expand All @@ -216,7 +212,7 @@ public Long modifyAssignees(Long id, ModifyAssigneeRequestDto request) {

@Transactional
public Long modifyLabels(Long id, ModifyLabelRequestDto request) {
validateExistIssue(id);
validateExistActiveIssue(id);
List<Long> labels = request.getLabels();

if (labels != null) {
Expand All @@ -236,7 +232,7 @@ public Long modifyLabels(Long id, ModifyLabelRequestDto request) {

@Transactional
public Long modifyMilestone(Long id, ModifyIssueMilestoneDto request) {
validateExistIssue(id);
validateExistActiveIssue(id);
Long milestoneId = request.getMilestone();
if (milestoneId != null) {
milestoneService.isExistMilestone(milestoneId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package codesquad.issueTracker.user.service;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
Expand Down

0 comments on commit 3ec9f59

Please sign in to comment.