Skip to content

Commit

Permalink
abstract delete command
Browse files Browse the repository at this point in the history
  • Loading branch information
csc530 committed Jul 31, 2024
1 parent f294844 commit a9a1983
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 170 deletions.
68 changes: 68 additions & 0 deletions Resumer/cli/commands/delete/DeleteCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using Microsoft.EntityFrameworkCore;
using Resumer.models;
using Spectre.Console;
using Spectre.Console.Cli;

namespace Resumer.cli.commands.delete;

public abstract class
DeleteCommand<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>: Command<DeleteCommandSettings>
where T : class
{
private string TypeName { get; } = typeof(T).Name.ToLower();
protected ResumeContext Db { get; set; } = new();
protected abstract DbSet<T> DbSet { get; }

public sealed override int Execute(CommandContext context, DeleteCommandSettings settings)
{
if(!DbSet.Any())
return CommandOutput.Error(ExitCode.Canceled, $"No {TypeName} found");

var confirmDelete = !settings.NoConfirm;
string prompt;
List<T> selected;

if(settings.DeleteAll)
{
prompt = $"Are you sure you want to delete all {DbSet.Count()} {TypeName}?";
if(confirmDelete)
{
AnsiConsole.WriteLine(DbSet.Print());
if(!AnsiConsole.Confirm(prompt, false))
return CommandOutput.Error(ExitCode.Canceled);
}

selected = DbSet.ToList();
}
else
{
selected = AnsiConsole.Prompt(
new MultiSelectionPrompt<T>()
.Title($"Select {TypeName}s to delete")
.PageSize(10).AddChoices(DbSet)
);
prompt = selected.Count == 1
? $"Are you sure you want to delete this {TypeName} - {selected[0]}?"
: $"Are you sure you want to delete these {selected.Count} {TypeName}s?";
if(confirmDelete && !AnsiConsole.Confirm(prompt, false))
return CommandOutput.Error(ExitCode.Canceled);
}

DbSet.RemoveRange(selected);
var deleted = Db.SaveChanges();
return CommandOutput.Success(deleted == 1 ? "1 job deleted." : $"{deleted} jobs deleted.");
}
}

public class DeleteCommandSettings: CommandSettings
{
[CommandOption("-n|--no-confirm")]
[Description("Do not ask for confirmation before deleting")]
public bool NoConfirm { get; set; }

[CommandOption("-a|--all")]
[Description("Delete all entries")]
public bool DeleteAll { get; set; }
}
16 changes: 0 additions & 16 deletions Resumer/cli/commands/delete/DeleteCommandSettings.cs

This file was deleted.

36 changes: 3 additions & 33 deletions Resumer/cli/commands/delete/DeleteJobCommand.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,11 @@
using Microsoft.EntityFrameworkCore;
using Resumer.models;
using Spectre.Console;
using Spectre.Console.Cli;

namespace Resumer.cli.commands.delete;

public class DeleteJobCommand: Command<DeleteCommandSettings>
public class DeleteJobCommand: DeleteCommand<Job>
{
public override int Execute(CommandContext context, DeleteCommandSettings settings)
{
var db = new ResumeContext();
var jobs = db.Jobs;

if(!jobs.Any())
return CommandOutput.Success("No jobs found.");
else if(settings.DeleteAll)
{
if(!settings.NoConfirm && !AnsiConsole.Confirm("Are you sure you want to delete all jobs?", false))
return CommandOutput.Error(ExitCode.Canceled);
jobs.RemoveRange(jobs);
}
else
{
var selected =
AnsiConsole.Prompt(new MultiSelectionPrompt<Job>().Title("Select job(s) to delete").AddChoices(jobs));

AnsiConsole.WriteLine($"Deleting {selected.Count} jobs...");
selected.ForEach(job => AnsiConsole.MarkupLine($"- {job}"));

var prompt = selected.Count == 1
? $"Are you sure you want to delete {selected[0]}?"
: $"Are you sure you want to delete these {selected.Count} jobs?";
if(!settings.NoConfirm && !AnsiConsole.Confirm(prompt))
return CommandOutput.Error(ExitCode.Canceled);
jobs.RemoveRange(selected);
}

var deleted = db.SaveChanges();
return CommandOutput.Success(deleted == 1 ? "1 job deleted." : $"{deleted} jobs deleted.");
}
protected override DbSet<Job> DbSet => Db.Jobs;
}
35 changes: 3 additions & 32 deletions Resumer/cli/commands/delete/DeleteProfileCommand.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,13 @@
using Microsoft.EntityFrameworkCore;
using Resumer.models;
using Spectre.Console;
using Spectre.Console.Cli;
using Profile = Resumer.models.Profile;

namespace Resumer.cli.commands.delete;

public class DeleteProfileCommand : Command<DeleteCommandSettings>
public class DeleteProfileCommand : DeleteCommand<Profile>
{
public override int Execute(CommandContext context, DeleteCommandSettings settings)
{
var db = new ResumeContext();
var profiles = db.Profiles;
if(!profiles.Any())
return CommandOutput.Success("No profiles found");
else if(settings.DeleteAll)
{
if(!settings.NoConfirm && !AnsiConsole.Confirm("Are you sure you want to delete all profiles?"))
return CommandOutput.Error(ExitCode.Canceled);
profiles.RemoveRange(profiles);
}
else
{
var selected = AnsiConsole.Prompt(new MultiSelectionPrompt<Profile>()
.Title("Select a profile to delete")
.AddChoices(profiles.AsEnumerable().OrderBy(p => p.ToString()).ToList()));
protected override DbSet<Profile> DbSet => Db.Profiles;

AnsiConsole.WriteLine($"Deleting {selected.Count} profiles...");
selected.ForEach(profile => AnsiConsole.MarkupLine($"- {profile}"));

var prompt = selected.Count == 1
? $"Are you sure you want to delete {selected[0]}?"
: $"Are you sure you want to delete these {selected.Count} profiles?";
if(!settings.NoConfirm && !AnsiConsole.Confirm(prompt,false))
return CommandOutput.Error(ExitCode.Canceled);
profiles.RemoveRange(selected);
}

var deleted = db.SaveChanges();
return CommandOutput.Success(deleted == 1 ? "Profile deleted" : $"{deleted} profiles deleted");
}
}
34 changes: 3 additions & 31 deletions Resumer/cli/commands/delete/DeleteProjectCommand.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,12 @@
using Microsoft.EntityFrameworkCore;
using Resumer.models;
using Spectre.Console;
using Spectre.Console.Cli;

namespace Resumer.cli.commands.delete;

public class DeleteProjectCommand: Command<DeleteCommandSettings>
public class DeleteProjectCommand: DeleteCommand<Project>
{
public override int Execute(CommandContext context, DeleteCommandSettings settings)
{
var db = new ResumeContext();
var projects = db.Projects;
if(!projects.Any())
return CommandOutput.Success("No projects found");
else if(settings.DeleteAll)
{
if(!settings.NoConfirm && !AnsiConsole.Confirm("Are you sure you want to delete all projects?"))
return CommandOutput.Error(ExitCode.Canceled);
projects.RemoveRange(projects);
var deleted = db.SaveChanges();
return CommandOutput.Success(deleted == 1 ? "project deleted" : $"{deleted} projects deleted");
}
else
{
var selectedProjects = AnsiConsole.Prompt(new MultiSelectionPrompt<Project>()
.Title("Select projects to delete")
.PageSize(10)
.AddChoices(projects));
protected override DbSet<Project> DbSet => Db.Projects;

var prompt = selectedProjects.Count == 1
? $"Are you sure you want to delete this project - {selectedProjects[0].Title}?"
: $"Are you sure you want to delete these {selectedProjects.Count} projects?";
if(!settings.NoConfirm && !AnsiConsole.Confirm(prompt, false))
return CommandOutput.Error(ExitCode.Canceled);
projects.RemoveRange(selectedProjects);
var deleted = db.SaveChanges();
return CommandOutput.Success(deleted == 1 ? "project deleted" : $"{deleted} projects deleted");
}
}
}
30 changes: 3 additions & 27 deletions Resumer/cli/commands/delete/DeleteSkillCommand.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,12 @@
using Microsoft.EntityFrameworkCore;
using Resumer.models;
using Spectre.Console;
using Spectre.Console.Cli;

namespace Resumer.cli.commands.delete;

public class DeleteSkillCommand: Command<DeleteCommandSettings>
public class DeleteSkillCommand: DeleteCommand<Skill>
{
public override int Execute(CommandContext context, DeleteCommandSettings settings)
{
var db = new ResumeContext();
var skills = db.Skills;
if(!skills.Any())
return CommandOutput.Success("No skills found");
else if(settings.DeleteAll)
{
if(!settings.NoConfirm && !AnsiConsole.Confirm($"Are you sure you want to delete all {skills.Count()} skills?", false))
return CommandOutput.Error(ExitCode.Canceled);
skills.RemoveRange(skills);
}
else
{
var selected = AnsiConsole.Prompt(new MultiSelectionPrompt<Skill>().Title("Select skills to delete")
.PageSize(10).AddChoices(skills));
var prompt = selected.Count == 1
? $"Are you sure you want to delete this skill - {selected[0]}?"
: $"Are you sure you want to delete these {selected.Count} skills?";
if(!settings.NoConfirm && !AnsiConsole.Confirm(prompt))
return CommandOutput.Error(ExitCode.Canceled);
skills.RemoveRange(selected);
}
protected override DbSet<Skill> DbSet => Db.Skills;

var deleted = db.SaveChanges();
return CommandOutput.Success(deleted == 1 ? "Skill deleted" : $"{deleted} skills deleted");
}
}
34 changes: 3 additions & 31 deletions Resumer/cli/commands/delete/DeleteTypstTemplateCommand.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,11 @@
using Microsoft.EntityFrameworkCore;
using Resumer.models;
using Spectre.Console;
using Spectre.Console.Cli;

namespace Resumer.cli.commands.delete;

public class DeleteTypstTemplateCommand: Command<DeleteCommandSettings>
public class DeleteTypstTemplateCommand: DeleteCommand<TypstTemplate>
{
public override int Execute(CommandContext context, DeleteCommandSettings settings)
{
var db = new ResumeContext();
var typstTemplates = db.Templates;
if(!typstTemplates.Any())
return CommandOutput.Success("No templates found");
else if(settings.DeleteAll)
{
if(!settings.NoConfirm && !AnsiConsole.Confirm("Are you sure you want to delete all templates?", false))
return CommandOutput.Error(ExitCode.Canceled);
typstTemplates.RemoveRange(typstTemplates);
}
else
{
var selectedTypstTemplates = AnsiConsole.Prompt(new MultiSelectionPrompt<TypstTemplate>()
.Title("Select templates to delete")
.PageSize(10)
.AddChoices(typstTemplates.OrderBy(template => template.Name)));

var prompt = selectedTypstTemplates.Count == 1
? $"Are you sure you want to delete this typst template - {selectedTypstTemplates[0].Name}?"
: $"Are you sure you want to delete these {selectedTypstTemplates.Count} templates?";
if(!settings.NoConfirm && !AnsiConsole.Confirm(prompt, false))
return CommandOutput.Error(ExitCode.Canceled);
typstTemplates.RemoveRange(selectedTypstTemplates);
}

var deleted = db.SaveChanges();
return CommandOutput.Success(deleted == 1 ? "template deleted" : $"{deleted} typst templates deleted");
}
protected override DbSet<TypstTemplate> DbSet => Db.Templates;
}

0 comments on commit a9a1983

Please sign in to comment.