aboutsummaryrefslogtreecommitdiffstats
path: root/ui-console/src/main/java/market/guess/ui/console/command/LoadFileMenuCommand.java
blob: c36df90a1c60709f47de29d18baab4c11f631f88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package market.guess.ui.console.command;

import market.guess.api.CatalogContext;
import market.guess.ui.console.io.InputProcessor;

public final class LoadFileMenuCommand implements MenuCommand {
  private final CatalogContext catalog;
  private final InputProcessor io;

  public LoadFileMenuCommand(CatalogContext catalog, InputProcessor io) {
    this.catalog = catalog;
    this.io = io;
  }

  @Override
  public int getIndex() {
    return 1;
  }

  @Override
  public String getName() {
    return "Load events from file";
  }

  @Override
  public void execute() {
    var path = io.readPath("Please enter the full file path to the XML file: ");
    var result = catalog.loadEvents(path);

    if (result.isSuccess()) {
      io.println(
          "Successfully loaded %d event(s) from %s."
              .formatted(result.getData().eventsLoaded(), result.getData().source()));
      return;
    }

    var errorMsg =
        result.getDetails() != null && !result.getDetails().isBlank()
            ? result.getDetails()
            : result.getMessage();
    io.println("The file was not loaded: %s".formatted(errorMsg));
  }
}