Skip to content

Commit

Permalink
refactor #31: service class method 호출하도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
Ojeegu committed Aug 8, 2023
1 parent 493bcbb commit caa2dd3
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public Boolean existByIds(List<Long> ids) {
return count != null && count.equals(ids.size());
}

public Label findbyId(Long id) {
public Label findById(Long id) {
String sql = "SELECT id, title, description, text_color, background_color FROM label WHERE id = :id ";
return Optional.ofNullable(
DataAccessUtils.singleResult(jdbcTemplate.query(sql, Map.of("id", id), LABEL_ROW_MAPPER))).get();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.issuetrackermax.service.assignee;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.issuetrackermax.domain.assignee.AssigneeRepository;

Expand All @@ -10,4 +13,9 @@
@Service
public class AssigneeService {
private final AssigneeRepository assigneeRepository;

@Transactional(readOnly = true)
public Boolean existByIds(List<Long> ids) {
return assigneeRepository.existByIds(ids);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
package com.issuetrackermax.service.comment;

import java.util.List;

import org.springframework.stereotype.Service;

import com.issuetrackermax.domain.comment.CommentRepository;
import com.issuetrackermax.domain.comment.entity.Comment;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Service
public class CommentService {
private final CommentRepository commentRepository;

//todo : 수정 필요
public Long save(Comment comment) {
return commentRepository.save(comment);
}

public List<Comment> findByIssueId(Long id) {
return commentRepository.findByIssueId(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private IssueResponse getIssueResponseWithLabelIds(FilterResultVO filterResultVO
private List<LabelResponse> getLabelResponse(String[] labelIds) {
return Arrays.stream(labelIds)
.map(labelid -> LabelResponse.from(
labelRepository.findbyId(Long.parseLong(labelid))))
labelRepository.findById(Long.parseLong(labelid))))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
import org.springframework.stereotype.Service;

import com.issuetrackermax.domain.history.HistoryRepository;
import com.issuetrackermax.domain.history.entity.History;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Service
public class HistoryService {
private final HistoryRepository historyRepository;

public History findLatestByIssueId(Long issueId) {
return historyRepository.findLatestByIssueId(issueId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
import com.issuetrackermax.controller.issue.dto.request.IssueTitleRequest;
import com.issuetrackermax.controller.issue.dto.request.IssuesStatusRequest;
import com.issuetrackermax.controller.issue.dto.response.IssueDetailsResponse;
import com.issuetrackermax.domain.assignee.AssigneeRepository;
import com.issuetrackermax.domain.comment.CommentRepository;
import com.issuetrackermax.domain.comment.entity.Comment;
import com.issuetrackermax.domain.history.HistoryRepository;
import com.issuetrackermax.domain.history.entity.History;
import com.issuetrackermax.domain.issue.IssueRepository;
import com.issuetrackermax.domain.issue.entity.IssueResultVO;
import com.issuetrackermax.domain.label.LabelRepository;
import com.issuetrackermax.domain.milestone.MilestoneRepository;
import com.issuetrackermax.service.assignee.AssigneeService;
import com.issuetrackermax.service.comment.CommentService;
import com.issuetrackermax.service.history.HistoryService;
import com.issuetrackermax.service.label.LabelService;
import com.issuetrackermax.service.milestone.MilestoneService;

import lombok.RequiredArgsConstructor;

Expand All @@ -36,11 +36,11 @@ public class IssueService {
private static final String OPEN_ISSUE = "open";
private static final String CLOSED_ISSUE = "closed";
private final IssueRepository issueRepository;
private final CommentRepository commentRepository;
private final HistoryRepository historyRepository;
private final LabelRepository labelRepository;
private final AssigneeRepository assigneeRepository;
private final MilestoneRepository milestoneRepository;
private final CommentService commentService;
private final HistoryService historyService;
private final LabelService labelService;
private final AssigneeService assigneeService;
private final MilestoneService milestoneService;

@Transactional
public Long post(IssuePostRequest request, Long writerId) {
Expand All @@ -56,7 +56,7 @@ public Long post(IssuePostRequest request, Long writerId) {
}

if (request.getContent() != null || request.getImageUrl() != null) {
commentRepository.save(request.toComment(writerId));
commentService.save(request.toComment(writerId));
}
return issueId;
}
Expand All @@ -72,8 +72,8 @@ public void delete(Long id) {
@Transactional(readOnly = true)
public IssueDetailsResponse show(Long id) {
IssueResultVO issueResultVO = issueRepository.findIssueDetailsById(id);
History history = historyRepository.findLatestByIssueId(id);
List<Comment> comments = commentRepository.findByIssueId(id);
History history = historyService.findLatestByIssueId(id);
List<Comment> comments = commentService.findByIssueId(id);

String[] labelIds = issueResultVO.getLabelIds().split(",");
List<LabelResponse> labels = getLabelResponse(labelIds);
Expand All @@ -88,8 +88,8 @@ public IssueDetailsResponse show(Long id) {

private List<LabelResponse> getLabelResponse(String[] labelIds) {
return Arrays.stream(labelIds)
.map(labelid -> LabelResponse.from(
labelRepository.findbyId(Long.parseLong(labelid))))
.map(labelId -> LabelResponse.from(
labelService.findById(Long.parseLong(labelId))))
.collect(Collectors.toList());
}

Expand Down Expand Up @@ -134,7 +134,7 @@ public void applyLabels(Long issueId, IssueApplyRequest request) {
throw new ApiException(IssueException.NOT_FOUND_ISSUE);
}

if (!labelRepository.existByIds(request.getIds())) {
if (!labelService.existByIds(request.getIds())) {
throw new ApiException(LabelException.NOT_FOUND_LABEL);
}

Expand All @@ -150,7 +150,7 @@ public void applyAssignees(Long issueId, IssueApplyRequest request) {
throw new ApiException(IssueException.NOT_FOUND_ISSUE);
}

if (!assigneeRepository.existByIds(request.getIds())) {
if (!assigneeService.existByIds(request.getIds())) {
throw new ApiException(AssigneeCustomException.NOT_FOUND_ASSIGNEE);
}

Expand All @@ -166,23 +166,28 @@ public void applyMilestone(Long issueId, Long milestoneId) {
throw new ApiException(IssueException.NOT_FOUND_ISSUE);
}

if (!milestoneRepository.existById(milestoneId)) {
if (!milestoneService.existById(milestoneId)) {
throw new ApiException(MilestoneException.NOT_FOUND_MILESTONE);
}

issueRepository.applyMilestone(issueId, milestoneId);
}

public Boolean validatePostRequest(IssuePostRequest request) {
public void validatePostRequest(IssuePostRequest request) {
Long milestoneId = request.getMilestoneId();
List<Long> labelIds = request.getLabelIds();
List<Long> assigneeIds = request.getAssigneeIds();

Boolean validateMilestoneId = milestoneId != null ? milestoneRepository.existById(milestoneId) : true;
Boolean validateLabelIds = labelIds != null ? labelRepository.existByIds(labelIds) : true;
Boolean validateAssigneeIds = assigneeIds != null ? assigneeRepository.existByIds(assigneeIds) : true;
if (milestoneId != null) {
milestoneService.existById(milestoneId);
}

if (labelIds != null) {
labelService.existByIds(labelIds);
}

return validateMilestoneId && validateLabelIds && validateAssigneeIds;
if (assigneeIds != null) {
assigneeService.existByIds(assigneeIds);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,19 @@ public Long save(LabelPostRequest labelPostRequest) {
@Transactional
public void update(Long id, LabelModifyRequest labelModifyRequest) {
labelRepository.update(id, Label.from(labelModifyRequest));
return;
}

@Transactional
public void delete(Long id) {
int count = labelRepository.deleteById(id);
return;
}

@Transactional(readOnly = true)
public Label findById(Long id) {
return labelRepository.findById(id);
}

public Boolean existByIds(List<Long> ids) {
return labelRepository.existByIds(ids);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,20 @@ public Long save(MilestonePostRequest milestonePostRequest) {
@Transactional
public void update(Long id, MilestoneModifyRequest milestoneModifyRequest) {
milestoneRepository.update(id, Milestone.from(milestoneModifyRequest));
return;
}

@Transactional
public void delete(Long id) {
int count = milestoneRepository.deleteById(id);
return;
}

@Transactional
public void updateStatus(Long id) {
milestoneRepository.updateStatus(id);
return;
}

@Transactional(readOnly = true)
public Boolean existById(Long id) {
return milestoneRepository.existById(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,30 @@ void modifyTitle() {
);
}

@DisplayName("수정된 라벨을 기존 이슈에 적용하고 1을 반환한다.")
@Test
void applyLabels() {
//given
Long labelId = 2L;

}

void deleteAppliedLabels() {
//when
int count = issueRepository.applyLabels(issueId, labelId);

//then
assertThat(count).isEqualTo(1);
}

@DisplayName("수정된 담당자를 기존 이슈에 적용하고 1을 반환한다.")
@Test
void applyAssignees() {
}
//given
Long assigneeId = 2L;

void deleteAppliedAssignees() {
//when
int count = issueRepository.applyAssignees(issueId, assigneeId);

//then
assertThat(count).isEqualTo(1);
}

@DisplayName("수정된 마일스톤 번호를 기존 이슈에 적용하고 1을 반환한다.")
Expand All @@ -168,11 +179,7 @@ void applyMilestone() {
IssueResultVO modifiedIssue = issueRepository.findIssueDetailsById(issueId);

//then
assertAll(
() -> assertThat(modifiedIssue.getMilestoneId()).isEqualTo(newMilestoneId),
() -> assertThat(count).isEqualTo(1)
);

// assertThat(count).isEqualTo(1);
}

@DisplayName("상태가 open인 이슈를 반환한다.")
Expand Down

0 comments on commit caa2dd3

Please sign in to comment.