Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.shell:spring-shell-starter:3.3.0'
implementation 'org.jline:jline-terminal-jansi:3.23.0'
implementation 'org.jline:jline-terminal-jna:3.23.0'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.preponderous.parpt.command;

import com.preponderous.parpt.domain.Project;
import com.preponderous.parpt.service.ProjectService;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;

import java.util.List;

@ShellComponent
public class ListProjectsCommand {

private final ProjectService projectService;

public ListProjectsCommand(ProjectService projectService) {
this.projectService = projectService;
}

@ShellMethod(key = "list", value = "Lists all projects.")
public String execute() {
List<Project> projects = projectService.getProjects();

if (projects.isEmpty()) {
return "No projects found.";
}

StringBuilder result = new StringBuilder("Projects:\n");
for (Project project : projects) {
result.append(String.format("- %s: %s\n", project.getName(), project.getDescription()));
}

return result.toString();
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/preponderous/parpt/repo/ProjectRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.preponderous.parpt.repo;

import com.preponderous.parpt.domain.Project;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class ProjectRepository {
private final List<Project> projects = new ArrayList<>();

public List<Project> findAll() {
return new ArrayList<>(projects);
}

public void clear() {
projects.clear();
}

public void add(Project project) {
if (project == null) {
throw new IllegalArgumentException("Project cannot be null");
}
if (projects.stream().anyMatch(p -> p.getName().equals(project.getName()))) {
throw new IllegalArgumentException("Project with the same name already exists");
}
projects.add(project);
}
}
33 changes: 33 additions & 0 deletions src/main/java/com/preponderous/parpt/service/ProjectService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.preponderous.parpt.service;

import com.preponderous.parpt.domain.Project;
import com.preponderous.parpt.repo.ProjectRepository;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProjectService {
private final ProjectRepository projectRepository;

public ProjectService(ProjectRepository projectRepository) {
this.projectRepository = projectRepository;
}

public void createProject(String name, String description, int impact, int confidence, int ease, int reach, int effort) {
Project project = Project.builder()
.name(name)
.description(description)
.impact(impact)
.confidence(confidence)
.ease(ease)
.reach(reach)
.effort(effort)
.build();
projectRepository.add(project);
}

public List<Project> getProjects() {
return projectRepository.findAll();
}
}
5 changes: 5 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
spring:
application:
name: Parpt
shell:
interactive:
enabled: true
history:
enabled: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.preponderous.parpt.command;

import com.preponderous.parpt.repo.ProjectRepository;
import com.preponderous.parpt.service.ProjectService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.assertTrue;

@SpringBootTest
class ListProjectsCommandTest {

ListProjectsCommand listProjectsCommand;

@Autowired
ProjectService projectService;

@Autowired
ProjectRepository projectRepository;

@BeforeEach
void setUp() {
// Initialize the command with the project service
listProjectsCommand = new ListProjectsCommand(projectService);

// Clear any existing projects in the repository before each test
projectRepository.clear();
}

@Test
void shouldReturnEmptyListWhenNoProjectsExist() {
// Given no projects exist

// When the command is executed
var result = listProjectsCommand.execute();

// Then the result should be an empty list
assertTrue(result.contains("No projects found."));
}

@Test
void shouldReturnListOfProjectsWhenProjectsExist() {
// Given some projects exist
projectService.createProject("Project A", "Description A", 5, 4, 3, 2, 1);
projectService.createProject("Project B", "Description B", 4, 3, 2, 1, 5);

// When the command is executed
var result = listProjectsCommand.execute();

// Then the result should contain the projects
assertTrue(result.contains("Projects:"));
assertTrue(result.contains("Project A: Description A"));
assertTrue(result.contains("Project B: Description B"));
}
}