Skip to content

Commit 3cd8663

Browse files
authored
[PR] Merge pull request #97 from CSC207-2022F-UofT/ListAllInteractorUpdate
List all interactor update
2 parents aaef6f4 + 08c0957 commit 3cd8663

File tree

7 files changed

+126
-29
lines changed

7 files changed

+126
-29
lines changed
Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package org.hydev.mcpm.client.commands.controllers;
22

3+
import org.hydev.mcpm.client.display.presenters.ListPresenter;
34
import org.hydev.mcpm.client.list.ListAllBoundary;
45
import org.hydev.mcpm.client.list.ListType;
6+
import org.hydev.mcpm.client.list.ListResult;
57

6-
import java.util.List;
78
import java.util.function.Consumer;
89

9-
import static org.hydev.mcpm.client.display.presenters.Table.tabulate;
10-
1110
/**
1211
* Controller class for the ListAll use case.
1312
*
@@ -16,6 +15,7 @@
1615
public class ListController {
1716
private final ListAllBoundary listAllBoundary;
1817

18+
1919
/**
2020
* Constructor for ListAllController.
2121
*
@@ -29,28 +29,37 @@ public ListController(ListAllBoundary listAllBoundary) {
2929
* Executes the ListAll use case.
3030
*
3131
* @param parameter The parameter for the ListAll use case.
32-
* @param log Logger
3332
*/
3433
public void listAll(String parameter, Consumer<String> log) {
34+
ListPresenter listPresenter = new ListPresenter(log);
3535
ListType listType;
36-
switch (parameter) {
36+
switch (parameter.toLowerCase()) {
3737
case "all" -> listType = ListType.ALL;
3838
case "manual" -> listType = ListType.MANUAL;
3939
case "automatic" -> listType = ListType.AUTOMATIC;
4040
case "outdated" -> listType = ListType.OUTDATED;
4141
default -> {
42-
log.accept("Invalid parameter. Please use 'all', 'manual', 'automatic', or 'outdated'.");
42+
ListResult queryResult = new ListResult(null, ListResult.Type.SEARCH_INVALID_INPUT, null);
43+
listPresenter.displayResult(queryResult);
4344
return;
4445
}
4546
}
4647

47-
var list = listAllBoundary.listAll(listType);
48+
try {
49+
var list = listAllBoundary.listAll(listType);
4850

49-
// Tabulate result
50-
var table = tabulate(
51-
list.stream().map(p -> List.of("&a" + p.name(), "&e" + p.getFirstAuthor(), p.version())).toList(),
52-
List.of(":Name", "Author", "Version:"));
51+
// if list is empty
52+
ListResult queryResult;
53+
if (list.isEmpty()) {
54+
queryResult = new ListResult(list, ListResult.Type.SUCCESS_RETRIEVING_BUT_NO_MATCHES, listType);
55+
} else {
56+
queryResult = new ListResult(list, ListResult.Type.SUCCESS_RETRIEVING_LOCAL_AND_UPDATABLE, listType);
57+
}
58+
listPresenter.displayResult(queryResult);
59+
} catch (Exception e) {
60+
ListResult queryResult = new ListResult(null, ListResult.Type.SEARCH_FAILED_TO_FETCH_INSTALLED, listType);
61+
listPresenter.displayResult(queryResult);
62+
}
5363

54-
log.accept(table);
5564
}
5665
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.hydev.mcpm.client.commands.presenters;
2+
3+
import org.hydev.mcpm.client.list.ListResult;
4+
5+
public interface ListResultPresenter {
6+
/**
7+
* Display the associated output to the console
8+
*
9+
* @param listResult result of the list command
10+
*/
11+
void displayResult(ListResult listResult);
12+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.hydev.mcpm.client.display.presenters;
2+
3+
import org.hydev.mcpm.client.commands.presenters.ListResultPresenter;
4+
import org.hydev.mcpm.client.list.ListResult;
5+
6+
import java.util.List;
7+
import java.util.function.Consumer;
8+
9+
import static org.hydev.mcpm.client.display.presenters.Table.tabulate;
10+
11+
public class ListPresenter implements ListResultPresenter {
12+
private final Consumer<String> log;
13+
14+
/**
15+
* Instantiate Install Presenter
16+
*
17+
* @param log log string to the console
18+
*/
19+
20+
public ListPresenter(Consumer<String> log) {
21+
this.log = log;
22+
}
23+
24+
@Override
25+
public void displayResult(ListResult listResult) {
26+
var list = listResult.queryResult();
27+
if (listResult.type() == ListResult.Type.SEARCH_INVALID_INPUT) {
28+
log.accept(listResult.type().reason() + "\n" + "Invalid input. Please enter one of the following: " +
29+
"all, manual, automatic, outdated");
30+
} else if (listResult.type() == ListResult.Type.SEARCH_FAILED_TO_FETCH_INSTALLED) {
31+
log.accept(listResult.type().reason() + "\n" + "Unable to fetch result.");
32+
} else {
33+
var table = tabulate(
34+
list.stream().map(p -> List.of("&a" + p.name(), "&e" + p.getFirstAuthor(), p.version())).toList(),
35+
List.of(":Name", "Author", "Version:"));
36+
37+
this.log.accept(listResult.type().reason() + " (Parameter " + listResult.listType() + ")\n" + table);
38+
39+
}
40+
}
41+
}

src/main/java/org/hydev/mcpm/client/list/ListAllInteractor.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@
77
import java.util.List;
88

99
/**
10-
* Implementation to the ListAll functionality
10+
* Implementation for the ListAll functionality
1111
*/
12-
public record ListAllInteractor(
13-
PluginTracker pluginTracker,
14-
CheckForUpdatesBoundary updates
15-
) implements ListAllBoundary {
12+
public record ListAllInteractor(PluginTracker pluginTracker, CheckForUpdatesBoundary checkForUpdatesBoundary) implements ListAllBoundary {
1613
/**
1714
* listAllInteractor interacts with the LocalPluginTracker to get the list of
1815
* plugins, according to a specified
1916
* parameter
2017
*
18+
*
2119
* @param parameter The parameter for the ListAll use case. 'All' denotes a
2220
* request to list all manually
2321
* installed plugins, 'manual' denotes a request to list all
@@ -40,8 +38,8 @@ public List<PluginYml> listAll(ListType parameter) {
4038
return installed.stream().filter(it -> !manual.contains(it.name())).toList();
4139
}
4240
case OUTDATED -> {
43-
ListUpdateableBoundary listUpdateableBoundary = new ListUpdateableHelper();
44-
var outdated = listUpdateableBoundary.listUpdateable(pluginTracker, updates);
41+
ListUpdatableBoundary listUpdatableBoundary = new ListUpdatableHelper();
42+
var outdated = listUpdatableBoundary.listUpdatable(pluginTracker, checkForUpdatesBoundary);
4543
return installed.stream().filter(it -> outdated.contains(it.name())).toList();
4644
}
4745
default -> {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.hydev.mcpm.client.list;
2+
3+
4+
import org.hydev.mcpm.client.models.PluginYml;
5+
6+
import java.util.List;
7+
8+
9+
/**
10+
* Exception during installation of a plugin
11+
*/
12+
public record ListResult(List<PluginYml> queryResult, Type type, ListType listType) {
13+
/**
14+
* Type of list failure
15+
*/
16+
public enum Type {
17+
SEARCH_INVALID_INPUT("Invalid search input"),
18+
SEARCH_FAILED_TO_FETCH_INSTALLED("Failed to fetch local files."),
19+
SUCCESS_RETRIEVING_LOCAL_BUT_FAIL_UPDATABLE("Successfully retrieved local files," +
20+
" but failed to retrieve updatable files."),
21+
SUCCESS_RETRIEVING_LOCAL_AND_UPDATABLE("Successfully retrieved local files."),
22+
SUCCESS_RETRIEVING_BUT_NO_MATCHES("Successfully retrieved local files, but no matches found.");
23+
24+
private final String reason;
25+
26+
Type(String reason) {
27+
this.reason = reason;
28+
}
29+
30+
public String reason() {
31+
return reason;
32+
}
33+
}
34+
}

src/main/java/org/hydev/mcpm/client/list/ListUpdateableBoundary.java renamed to src/main/java/org/hydev/mcpm/client/list/ListUpdatableBoundary.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* Defines an interface for obtaining a list of plugin names that may be updated
99
*/
10-
public interface ListUpdateableBoundary {
11-
ArrayList<String> listUpdateable(PluginTracker superPluginTracker,
12-
CheckForUpdatesBoundary checkForUpdatesBoundary);
10+
public interface ListUpdatableBoundary {
11+
ArrayList<String> listUpdatable(PluginTracker superPluginTracker,
12+
CheckForUpdatesBoundary checkForUpdatesBoundary);
1313
}

src/main/java/org/hydev/mcpm/client/list/ListUpdateableHelper.java renamed to src/main/java/org/hydev/mcpm/client/list/ListUpdatableHelper.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
import java.util.OptionalLong;
1919

2020
/**
21-
* A helper class for ListUpdateableBoundary.
21+
* A helper class for ListUpdatableBoundary.
2222
*/
23-
public class ListUpdateableHelper implements ListUpdateableBoundary {
23+
public class ListUpdatableHelper implements ListUpdatableBoundary {
2424

2525
/**
2626
* Returns a list of plugins names that belong to outdated plugins
@@ -29,7 +29,7 @@ public class ListUpdateableHelper implements ListUpdateableBoundary {
2929
* @param checkForUpdatesBoundary The boundary to check for updates
3030
* @return A list of plugins that are outdated.
3131
*/
32-
public ArrayList<String> listUpdateable(
32+
public ArrayList<String> listUpdatable(
3333
PluginTracker localPluginTracker, CheckForUpdatesBoundary checkForUpdatesBoundary) {
3434

3535
ArrayList<PluginVersionState> temp = new ArrayList<>();
@@ -47,20 +47,23 @@ public ArrayList<String> listUpdateable(
4747
PluginModelId pluginModelId = new PluginModelId(
4848
OptionalLong.of(installedModel.getPluginId()), installedModel.getName(), null);
4949

50-
temp.add(new PluginVersionState(pluginModelId, pluginVersionId));
50+
try {
51+
temp.add(new PluginVersionState(pluginModelId, pluginVersionId));
52+
} catch (Exception e) {
53+
break;
54+
}
5155
}
5256

5357
CheckForUpdatesInput input = new CheckForUpdatesInput(temp, false);
54-
5558
CheckForUpdatesResult rawResult = checkForUpdatesBoundary.updates(input);
5659

5760
// Read the list of installedModels and create a CheckForUpdatesInput object
5861
// with state equal
59-
// to the list of PluginTrackerModels's version
62+
// to the list of PluginTrackerModels' version
6063

6164
if (rawResult.state() == CheckForUpdatesResult.State.SUCCESS) {
6265

63-
// get the ids of the plugins that are outdated from result
66+
// get the names of the plugins that are outdated from result
6467
ArrayList<String> outdated = new ArrayList<>();
6568
for (PluginModel pluginModel : rawResult.updatable().values()) {
6669
pluginModel.getLatestPluginVersion()

0 commit comments

Comments
 (0)