Skip to content

Commit

Permalink
Merge pull request #77 from Louie-03/BE-feature-74
Browse files Browse the repository at this point in the history
[BE] 마일스톤 기능 구현
  • Loading branch information
rkolx authored Jul 11, 2022
2 parents 0e80f58 + 6f15c5d commit 9e59efa
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 1 deletion.
7 changes: 7 additions & 0 deletions BE/src/main/java/louie/hanse/issuetracker/domain/Issue.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,11 @@ public boolean isClosed() {
return status.isClosed();
}

public boolean isOpened() {
return status.isOpened();
}

public void deleteMilestone() {
this.milestone = null;
}
}
21 changes: 21 additions & 0 deletions BE/src/main/java/louie/hanse/issuetracker/domain/Milestone.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package louie.hanse.issuetracker.domain;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.time.LocalDate;
Expand All @@ -11,6 +13,7 @@

@Getter
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Milestone {

@Id @GeneratedValue(strategy = IDENTITY)
Expand All @@ -28,4 +31,22 @@ public class Milestone {
public void addIssue(Issue issue) {
this.issues.add(issue);
}

public Milestone(String title, String description, LocalDate completedDate) {
this.title = title;
this.description = description;
this.completedDate = completedDate;
}

public void updateTitle(String title) {
this.title = title;
}

public void updateDescription(String description) {
this.description = description;
}

public void updateCompletedDate(LocalDate completedDate) {
this.completedDate = completedDate;
}
}
6 changes: 5 additions & 1 deletion BE/src/main/java/louie/hanse/issuetracker/domain/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public Status reverse() {
}

public boolean isClosed() {
return this.equals(Status.CLOSE);
return this.equals(CLOSE);
}

public boolean isOpened() {
return this.equals(OPEN);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

import louie.hanse.issuetracker.domain.Milestone;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDate;
import java.util.List;

public interface MilestoneRepository extends JpaRepository<Milestone, Long> {

@Query("select m from Milestone as m where m.completedDate < :currentDate")
List<Milestone> findClosedMilestone(@Param("currentDate") LocalDate currentDate);

@Query("select m from Milestone as m where m.completedDate > :currentDate")
List<Milestone> findOpenedMilestone(@Param("currentDate") LocalDate currentDate);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package louie.hanse.issuetracker.service;

import lombok.RequiredArgsConstructor;
import louie.hanse.issuetracker.domain.Issue;
import louie.hanse.issuetracker.domain.Milestone;
import louie.hanse.issuetracker.domain.Status;
import louie.hanse.issuetracker.repository.MilestoneRepository;
import louie.hanse.issuetracker.web.dto.milestone.MilestoneListResponse;
import louie.hanse.issuetracker.web.dto.milestone.MilestoneRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class MileStoneService {

private final MilestoneRepository milestoneRepository;

@Transactional
public void register(MilestoneRequest request) {
Milestone milestone = request.toEntity();
milestoneRepository.save(milestone);
}

public MilestoneListResponse getMilestoneList(Status status) {
List<Milestone> closedMilestone = milestoneRepository.findClosedMilestone(LocalDate.now());
List<Milestone> openedMilestone = milestoneRepository.findOpenedMilestone(LocalDate.now());
if (status.isOpened()) {
return new MilestoneListResponse(openedMilestone.size(), closedMilestone.size(), openedMilestone);
}
return new MilestoneListResponse(openedMilestone.size(), closedMilestone.size(), closedMilestone);
}

@Transactional
public void edit(Long id, MilestoneRequest request) {
Milestone milestone = milestoneRepository.findById(id).orElseThrow(IllegalStateException::new);

milestone.updateTitle(request.getTitle());
milestone.updateDescription(request.getDescription());
milestone.updateCompletedDate(request.getCompletedDate());
}

@Transactional
public void delete(Long id) {
Milestone milestone = milestoneRepository.findById(id).orElseThrow(IllegalStateException::new);
List<Issue> issues = milestone.getIssues();
for (Issue issue : issues) {
issue.deleteMilestone();
}
milestoneRepository.delete(milestone);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package louie.hanse.issuetracker.web.controller;

import lombok.RequiredArgsConstructor;
import louie.hanse.issuetracker.domain.Status;
import louie.hanse.issuetracker.service.MileStoneService;
import louie.hanse.issuetracker.web.dto.milestone.MilestoneListResponse;
import louie.hanse.issuetracker.web.dto.milestone.MilestoneRequest;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/milestone")
@RequiredArgsConstructor
public class MileStoneController {

private final MileStoneService mileStoneService;

@PostMapping
public void registerMilestone(@RequestBody MilestoneRequest request) {
mileStoneService.register(request);
}

@GetMapping
public MilestoneListResponse getMilestoneList(@RequestParam("status") Status status) {
return mileStoneService.getMilestoneList(status);
}

@PutMapping("/{id}")
public void editMilestone(@PathVariable Long id, @RequestBody MilestoneRequest request) {
mileStoneService.edit(id, request);
}

@DeleteMapping("/{id}")
public void deleteMilestone(@PathVariable Long id) {
mileStoneService.delete(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package louie.hanse.issuetracker.web.dto.milestone;

import lombok.Getter;
import louie.hanse.issuetracker.domain.Issue;
import louie.hanse.issuetracker.domain.Milestone;

import java.time.LocalDate;

@Getter
public class MilestoneDetailResponse {
private Long id;
private String title;
private String description;
private LocalDate completedDate;
private long openedIssueCount;
private long closedIssueCount;

public MilestoneDetailResponse(Milestone milestone) {
this.id = milestone.getId();
this.title = milestone.getTitle();
this.description = milestone.getDescription();
this.completedDate = milestone.getCompletedDate();
this.openedIssueCount = milestone.getIssues().stream()
.filter(Issue::isOpened)
.count();
this.closedIssueCount = milestone.getIssues().stream()
.filter(Issue::isClosed)
.count();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package louie.hanse.issuetracker.web.dto.milestone;

import lombok.Getter;
import louie.hanse.issuetracker.domain.Milestone;

import java.util.List;
import java.util.stream.Collectors;

@Getter
public class MilestoneListResponse {
private int openedMileStoneCount;
private int closedMileStoneCount;
private List<MilestoneDetailResponse> milestones;

public MilestoneListResponse(int openedMileStoneCount, int closedMileStoneCount, List<Milestone> milestones) {
this.openedMileStoneCount = openedMileStoneCount;
this.closedMileStoneCount = closedMileStoneCount;
this.milestones = milestones.stream()
.map(MilestoneDetailResponse::new)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package louie.hanse.issuetracker.web.dto.milestone;

import lombok.Getter;
import louie.hanse.issuetracker.domain.Milestone;

import javax.validation.constraints.NotEmpty;
import java.time.LocalDate;

@Getter
public class MilestoneRequest {
@NotEmpty
private String title;
private String description;
private LocalDate completedDate;

public Milestone toEntity() {
return new Milestone(title, description, completedDate);
}
}

0 comments on commit 9e59efa

Please sign in to comment.