Skip to content

Commit

Permalink
DEAR-133: add complete sprint
Browse files Browse the repository at this point in the history
  • Loading branch information
baurnick committed Aug 5, 2024
1 parent 7ca00df commit d025f46
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.fhnw.deardevbackend.controller;

import ch.fhnw.deardevbackend.dto.ActiveSprintsDTO;
import ch.fhnw.deardevbackend.dto.CreateSprintDTO;
import ch.fhnw.deardevbackend.dto.SprintIdAndTeamIdDTO;
import ch.fhnw.deardevbackend.dto.SprintsAndTeamsDTO;
Expand Down Expand Up @@ -39,6 +40,13 @@ public ResponseEntity<SprintsAndTeamsDTO> getSprintsAndTeams() {
return ResponseEntity.ok().body(sprintsAndTeams);
}

@GetMapping("/active")
public ResponseEntity<List<ActiveSprintsDTO>> getActiveSprints() {
Integer userId = getCurrentUserFromContext().getId();
List<ActiveSprintsDTO> activeSprints = sprintConfigService.getActiveSprints(userId);
return ResponseEntity.ok().body(activeSprints);
}

@PostMapping("/create")
public ResponseEntity<SprintConfig> createSprint(@RequestBody CreateSprintDTO request) {
Integer userId = getCurrentUserFromContext().getId();
Expand All @@ -52,6 +60,16 @@ public ResponseEntity<SprintConfig> updateSprintConfig(@PathVariable Integer spr
return ResponseEntity.ok().body(updatedSprint);
}

@PutMapping("/complete/{sprintId}")
public ResponseEntity<String> completeSprint(@PathVariable Integer sprintId) {
try {
sprintConfigService.completeSprint(sprintId);
return ResponseEntity.ok().body("Sprint completed successfully");
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
}

@PostMapping("/start")
public ResponseEntity<String> startSprint(@RequestBody SprintIdAndTeamIdDTO request) {
try {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/ch/fhnw/deardevbackend/dto/ActiveSprintsDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ch.fhnw.deardevbackend.dto;

import ch.fhnw.deardevbackend.entities.SprintConfig;
import lombok.Data;

@Data
public class ActiveSprintsDTO {
private String teamName;
private SprintConfig sprint;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class SprintConfig {
@Column(name = "sprint_goal")
private String sprintGoal;

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "team_id", referencedColumnName = "id")
private Team team;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
@Repository
public interface SprintConfigRepository extends JpaRepository<SprintConfig, Integer> {

List<SprintConfig> findByTeamIdAndStatus(Integer teamId, SprintStatus status);

List<SprintConfig> findAllByCreatedBy(Integer createdBy);

List<SprintConfig> findByCreatedByAndStatusAndStartDateAfter(Integer createdBy, SprintStatus status, LocalDate startDate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,15 @@ public interface TeamRepository extends JpaRepository<Team, Integer> {

@Query("SELECT t from Team t JOIN TeamMember tm ON t.id = tm.teamId WHERE tm.userId = :userId AND t.active = TRUE")
List<Team> findActiveTeamsByUserId(Integer userId);

@Query("""
SELECT t\s
FROM Team t\s
LEFT JOIN SprintConfig sc ON t.currentSprintId = sc.id
JOIN TeamMember tm ON t.id = tm.teamId\s
WHERE tm.userId = :userId\s
AND t.active = TRUE\s
AND (t.currentSprintId IS NULL OR sc.status = 'COMPLETED')
\s""")
List<Team> findActiveTeamsWithNoActiveSprintByUserId(Integer userId);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.fhnw.deardevbackend.services;

import ch.fhnw.deardevbackend.controller.exceptions.YappiException;
import ch.fhnw.deardevbackend.dto.ActiveSprintsDTO;
import ch.fhnw.deardevbackend.dto.CreateSprintDTO;
import ch.fhnw.deardevbackend.dto.SprintsAndTeamsDTO;
import ch.fhnw.deardevbackend.entities.SprintConfig;
Expand All @@ -15,6 +16,7 @@

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

Expand All @@ -37,13 +39,28 @@ public SprintConfig getSprintById(Integer sprintId) {
}

public SprintsAndTeamsDTO getSprintsAndTeams(Integer userId) {
List<Team> teams = teamRepository.findActiveTeamsByUserId(userId);
List<Team> teams = teamRepository.findActiveTeamsWithNoActiveSprintByUserId(userId);
LocalDate today = LocalDate.now();
List<SprintConfig> sprints = sprintConfigRepository.findByCreatedByAndStatusAndStartDateAfter(userId, SprintStatus.OPEN, today.minusDays(1));

return new SprintsAndTeamsDTO(teams, sprints);
}

public List<ActiveSprintsDTO> getActiveSprints(Integer userId) {
List<Team> teams = teamRepository.findActiveTeamsByUserId(userId);
List<ActiveSprintsDTO> activeSprints = new ArrayList<>();
for (Team team : teams) {
List<SprintConfig> sprints = sprintConfigRepository.findByTeamIdAndStatus(team.getId(), SprintStatus.IN_PROGRESS);
for (SprintConfig sprint : sprints) {
ActiveSprintsDTO dto = new ActiveSprintsDTO();
dto.setTeamName(team.getName());
dto.setSprint(sprint);
activeSprints.add(dto);
}
}
return activeSprints;
}

public SprintConfig createSprint(CreateSprintDTO dto, Integer userId) {

SprintConfig sprint = SprintConfig.builder()
Expand Down Expand Up @@ -80,6 +97,14 @@ public void startSprint(Integer teamId, Integer sprintId) {

SprintConfig sprint = sprintConfigRepository.findById(sprintId).orElseThrow(() -> new YappiException("Sprint not found with id: " + sprintId));
sprint.setStatus(SprintStatus.IN_PROGRESS);
sprint.setTeam(team);
sprintConfigRepository.save(sprint);
}

@Transactional
public void completeSprint(Integer sprintId) {
SprintConfig sprint = sprintConfigRepository.findById(sprintId).orElseThrow(() -> new YappiException("Sprint not found with id: " + sprintId));
sprint.setStatus(SprintStatus.COMPLETED);
sprintConfigRepository.save(sprint);
}
}

0 comments on commit d025f46

Please sign in to comment.