delete command
This commit is contained in:
parent
4166efceca
commit
a1238ff685
|
@ -0,0 +1,49 @@
|
||||||
|
import * as program from 'commander';
|
||||||
|
|
||||||
|
import { CipherService } from 'jslib/abstractions/cipher.service';
|
||||||
|
import { FolderService } from 'jslib/abstractions/folder.service';
|
||||||
|
|
||||||
|
import { Response } from '../models/response';
|
||||||
|
|
||||||
|
export class DeleteCommand {
|
||||||
|
constructor(private cipherService: CipherService, private folderService: FolderService) { }
|
||||||
|
|
||||||
|
async run(object: string, id: string, cmd: program.Command): Promise<Response> {
|
||||||
|
switch (object) {
|
||||||
|
case 'item':
|
||||||
|
return await this.deleteCipher(id);
|
||||||
|
case 'folder':
|
||||||
|
return await this.deleteFolder(id);
|
||||||
|
default:
|
||||||
|
return Response.badRequest('Unknown object.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async deleteCipher(id: string) {
|
||||||
|
const cipher = await this.cipherService.get(id);
|
||||||
|
if (cipher == null) {
|
||||||
|
return Response.notFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await this.cipherService.deleteWithServer(id);
|
||||||
|
return Response.success();
|
||||||
|
} catch (e) {
|
||||||
|
return Response.error(e.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async deleteFolder(id: string) {
|
||||||
|
const folder = await this.folderService.get(id);
|
||||||
|
if (folder == null) {
|
||||||
|
return Response.notFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await this.folderService.deleteWithServer(id);
|
||||||
|
return Response.success();
|
||||||
|
} catch (e) {
|
||||||
|
return Response.error(e.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,6 +10,7 @@ import { Main } from './main';
|
||||||
import { Response } from './models/response';
|
import { Response } from './models/response';
|
||||||
import { ListResponse } from './models/response/listResponse';
|
import { ListResponse } from './models/response/listResponse';
|
||||||
import { StringResponse } from './models/response/stringResponse';
|
import { StringResponse } from './models/response/stringResponse';
|
||||||
|
import { DeleteCommand } from './commands/delete.command';
|
||||||
|
|
||||||
export class Program {
|
export class Program {
|
||||||
constructor(private main: Main) { }
|
constructor(private main: Main) { }
|
||||||
|
@ -79,10 +80,10 @@ export class Program {
|
||||||
program
|
program
|
||||||
.command('delete <object> <id>')
|
.command('delete <object> <id>')
|
||||||
.description('Delete an object.')
|
.description('Delete an object.')
|
||||||
.action((object, id, cmd) => {
|
.action(async (object, id, cmd) => {
|
||||||
console.log('Deleting...');
|
const command = new DeleteCommand(this.main.cipherService, this.main.folderService);
|
||||||
console.log(object);
|
const response = await command.run(object, id, cmd);
|
||||||
console.log(id);
|
this.processResponse(response);
|
||||||
});
|
});
|
||||||
|
|
||||||
program
|
program
|
||||||
|
|
Loading…
Reference in New Issue