Files
TBDel/Commands/DeleteCommand.cs

66 lines
2.3 KiB
C#

using TBDel.Models;
using TBDel.Services;
namespace TBDel.Commands
{
public class DeleteCommand
{
public static async Task DeleteEntry(string[] args)
{
var dbService = new DbService();
if (args.Length > 1 && uint.TryParse(args[1], out uint id))
{
var filePath = await dbService.GetEntryPath(id);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write($"Are you sure you want to permanently delete entry with ID {id}? (y/N) ");
Console.ResetColor();
var input = Console.ReadLine();
if (input != "y")
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Operation cancelled.");
Console.ResetColor();
return;
}
if (File.Exists(filePath))
{
File.Delete(filePath);
if (await dbService.RemoveFileEntryAsync(id))
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("File deleted successfully.");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Something went wrong while deleting the file.");
Console.ResetColor();
}
}
else if (Directory.Exists(filePath))
{
Directory.Delete(filePath);
if (await dbService.RemoveFolderEntryAsync(id))
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Directory deleted successfully.");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Something went wrong while deleting the directory.");
Console.ResetColor();
}
}
}
}
}
}