Stop user from adding a db list with the same name (#1873)

This commit is contained in:
Charis Kyriakou 2022-12-15 09:21:54 +00:00 коммит произвёл GitHub
Родитель b1bf82d432
Коммит 091d793f13
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 31 добавлений и 3 удалений

Просмотреть файл

@ -99,16 +99,29 @@ export class DbConfigStore extends DisposableObject {
throw Error("Cannot add remote list if config is not loaded");
}
if (this.doesRemoteListExist(listName)) {
throw Error(`A remote list with the name '${listName}' already exists`);
}
const config: DbConfig = cloneDbConfig(this.config);
config.databases.remote.repositoryLists.push({
name: listName,
repositories: [],
});
// TODO: validate that the name doesn't already exist
await this.writeConfig(config);
}
public doesRemoteListExist(listName: string): boolean {
if (!this.config) {
throw Error("Cannot check remote list existence if config is not loaded");
}
return this.config.databases.remote.repositoryLists.some(
(l) => l.name === listName,
);
}
private async writeConfig(config: DbConfig): Promise<void> {
await writeJSON(this.configPath, config, {
spaces: 2,

Просмотреть файл

@ -76,6 +76,14 @@ export class DbManager {
}
public async addNewRemoteList(listName: string): Promise<void> {
if (this.dbConfigStore.doesRemoteListExist(listName)) {
throw Error(`A list with the name '${listName}' already exists`);
}
await this.dbConfigStore.addRemoteList(listName);
}
public doesRemoteListExist(listName: string): boolean {
return this.dbConfigStore.doesRemoteListExist(listName);
}
}

Просмотреть файл

@ -1,5 +1,6 @@
import { TreeViewExpansionEvent, window, workspace } from "vscode";
import { commandRunner } from "../../commandRunner";
import { showAndLogErrorMessage } from "../../helpers";
import { DisposableObject } from "../../pure/disposable-object";
import { DbManager } from "../db-manager";
import { DbTreeDataProvider } from "./db-tree-data-provider";
@ -58,7 +59,6 @@ export class DbPanel extends DisposableObject {
}
private async addNewRemoteList(): Promise<void> {
// TODO: check that config exists *before* showing the input box
const listName = await window.showInputBox({
prompt: "Enter a name for the new list",
placeHolder: "example-list",
@ -66,7 +66,14 @@ export class DbPanel extends DisposableObject {
if (listName === undefined) {
return;
}
await this.dbManager.addNewRemoteList(listName);
if (this.dbManager.doesRemoteListExist(listName)) {
void showAndLogErrorMessage(
`A list with the name '${listName}' already exists`,
);
} else {
await this.dbManager.addNewRemoteList(listName);
}
}
private async setSelectedItem(treeViewItem: DbTreeViewItem): Promise<void> {