refactor(cli): Обобщение CLI-команд для поддержки разных типов сущностей #59
@ -7,14 +7,15 @@ import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Optional;
|
||||
|
||||
import ru.kamask.pet.todo.service.TaskService;
|
||||
import ru.kamask.pet.todo.model.Identifiable;
|
||||
import ru.kamask.pet.todo.service.EntityService;
|
||||
|
||||
public class CliEngine {
|
||||
private HashMap<String, Command> registry = new HashMap<>();
|
||||
private TaskService service;
|
||||
private EntityService<? extends Identifiable> service;
|
||||
private BufferedReader reader;
|
||||
|
||||
public CliEngine(TaskService service) {
|
||||
public CliEngine(EntityService<? extends Identifiable> service) {
|
||||
this.service = service;
|
||||
reader = new BufferedReader(new InputStreamReader(System.in));
|
||||
initializeCommands();
|
||||
|
||||
@ -2,13 +2,14 @@ package ru.kamask.pet.todo.cli;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import ru.kamask.pet.todo.service.TaskService;
|
||||
import ru.kamask.pet.todo.model.Identifiable;
|
||||
import ru.kamask.pet.todo.service.EntityService;
|
||||
|
||||
public interface Command {
|
||||
String templateUsage = " %-30s // %s";
|
||||
String errorMessage = "Не корректно введена команда. Введите help для спарвки.";
|
||||
|
||||
Optional<String> handle(String[] args, TaskService service);
|
||||
Optional<String> handle(String[] args, EntityService<? extends Identifiable> service);
|
||||
|
||||
String name();
|
||||
|
||||
|
||||
@ -2,6 +2,8 @@ package ru.kamask.pet.todo.cli;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import ru.kamask.pet.todo.model.Identifiable;
|
||||
import ru.kamask.pet.todo.service.EntityService;
|
||||
import ru.kamask.pet.todo.service.TaskService;
|
||||
|
||||
public class CompleteCommand implements Command {
|
||||
@ -16,14 +18,16 @@ public class CompleteCommand implements Command {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> handle(String[] args, TaskService service) {
|
||||
public Optional<String> handle(String[] args, EntityService<? extends Identifiable> service) {
|
||||
if (args.length != 1)
|
||||
return Optional.of(Command.errorMessage);
|
||||
|
||||
TaskService taskService = (TaskService) service;
|
||||
|
||||
try {
|
||||
int id = Integer.parseInt(args[0]);
|
||||
|
||||
return Optional.of(service.complete(id)
|
||||
return Optional.of(taskService.complete(id)
|
||||
? "Задача ID-%d выполнена.".formatted(id)
|
||||
: "Задача ID-%d не найдена.".formatted(id));
|
||||
|
||||
|
||||
@ -2,6 +2,8 @@ package ru.kamask.pet.todo.cli;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import ru.kamask.pet.todo.model.Identifiable;
|
||||
import ru.kamask.pet.todo.service.EntityService;
|
||||
import ru.kamask.pet.todo.service.TaskService;
|
||||
|
||||
public class CreateCommand implements Command{
|
||||
@ -11,12 +13,13 @@ public class CreateCommand implements Command {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> handle(String[] args, TaskService service) {
|
||||
public Optional<String> handle(String[] args, EntityService<? extends Identifiable> service) {
|
||||
if (args.length > 0) {
|
||||
var title = String.join(" ", args);
|
||||
if (title.length() > 30)
|
||||
return Optional.of("Ошибка: максимальная длинна названия задачи 30 символов.");
|
||||
service.create(title);
|
||||
TaskService taskService = (TaskService) service;
|
||||
taskService.create(title);
|
||||
return Optional.of(String.format("Задача \"%s\" успешно добавлена!", title));
|
||||
}
|
||||
return Optional.of(Command.errorMessage);
|
||||
|
||||
@ -2,7 +2,8 @@ package ru.kamask.pet.todo.cli;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import ru.kamask.pet.todo.service.TaskService;
|
||||
import ru.kamask.pet.todo.model.Identifiable;
|
||||
import ru.kamask.pet.todo.service.EntityService;
|
||||
|
||||
public class DeleteCommand implements Command{
|
||||
@Override
|
||||
@ -16,7 +17,7 @@ public class DeleteCommand implements Command {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> handle(String[] args, TaskService service) {
|
||||
public Optional<String> handle(String[] args, EntityService<? extends Identifiable> service) {
|
||||
if (args.length != 1)
|
||||
return Optional.of(Command.errorMessage);
|
||||
|
||||
|
||||
@ -3,7 +3,9 @@ package ru.kamask.pet.todo.cli;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import ru.kamask.pet.todo.model.Identifiable;
|
||||
import ru.kamask.pet.todo.model.SimpleTask;
|
||||
import ru.kamask.pet.todo.service.EntityService;
|
||||
import ru.kamask.pet.todo.service.TaskService;
|
||||
|
||||
public class ListCommand implements Command {
|
||||
@ -13,11 +15,12 @@ public class ListCommand implements Command {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> handle(String[] args, TaskService service) {
|
||||
public Optional<String> handle(String[] args, EntityService<? extends Identifiable> service) {
|
||||
if (args.length > 0)
|
||||
return Optional.of(Command.errorMessage);
|
||||
|
||||
var res = formatWithTable(service.getAll(), "Список задач пуст.");
|
||||
TaskService taskService = (TaskService) service;
|
||||
List<SimpleTask> allTasks = taskService.getAll();
|
||||
var res = formatWithTable(allTasks, "Список задач пуст.");
|
||||
|
||||
return Optional.of(res);
|
||||
}
|
||||
|
||||
@ -2,6 +2,8 @@ package ru.kamask.pet.todo.cli;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import ru.kamask.pet.todo.model.Identifiable;
|
||||
import ru.kamask.pet.todo.service.EntityService;
|
||||
import ru.kamask.pet.todo.service.TaskService;
|
||||
|
||||
public class SearchCommand implements Command {
|
||||
@ -16,13 +18,15 @@ public class SearchCommand implements Command {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> handle(String[] args, TaskService service) {
|
||||
public Optional<String> handle(String[] args, EntityService<? extends Identifiable> service) {
|
||||
if (args.length != 1)
|
||||
return Optional.of(Command.errorMessage);
|
||||
if (args[0].length() < 3)
|
||||
return Optional.of("Длина запроса должна быть не менее 3 символов.");
|
||||
|
||||
var matchTask = service.search(args[0]);
|
||||
TaskService taskService = (TaskService) service;
|
||||
|
||||
var matchTask = taskService.search(args[0]);
|
||||
var res = new ListCommand().formatWithTable(matchTask, "Не найдено задач, соответствующих запросу.");
|
||||
|
||||
return Optional.of(res);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user