-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCourseCommands.cs
75 lines (65 loc) · 2.53 KB
/
CourseCommands.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using Ookii.CommandLine;
using Ookii.CommandLine.Commands;
using Ookii.CommandLine.Validation;
using System.ComponentModel;
namespace NestedCommands;
// This is the top-level "course" command. It has no functionality since everything is done in the
// ParentCommand class.
[Command("course")]
[Description("Add or remove a course.")]
internal class CourseCommand : ParentCommand
{
}
// Command to add courses. Since it inherits from BaseCommand, it has a Path argument in addition
// to the arguments created here.
[GeneratedParser]
[Command("add")]
[ParentCommand(typeof(CourseCommand))]
[Description("Adds a course to the database.")]
internal partial class AddCourseCommand : BaseCommand
{
[CommandLineArgument(IsPositional = true)]
[Description("The name of the course.")]
[ValidateNotWhiteSpace]
public required string Name { get; set; }
[CommandLineArgument(IsPositional = true)]
[Description("The name of the teacher of the course.")]
[ValidateNotWhiteSpace]
public required string Teacher { get; set; }
protected override async Task<int> RunAsync(Database db, CancellationToken cancellationToken)
{
int id = db.Courses.Any() ? db.Courses.Keys.Max() + 1 : 1;
db.Courses.Add(id, new Course(Name, Teacher));
await db.Save(Path, cancellationToken);
Console.WriteLine($"Added a course with ID {id}.");
return (int)ExitCode.Success;
}
}
// Command to remove courses. Since it inherits from BaseCommand, it has a Path argument in addition
// to the arguments created here.
[GeneratedParser]
[Command("remove")]
[ParentCommand(typeof(CourseCommand))]
[Description("Removes a course from the database.")]
internal partial class RemoveCourseCommand : BaseCommand
{
[CommandLineArgument(IsPositional = true)]
[Description("The ID of the course to remove.")]
public required int Id { get; set; }
protected override async Task<int> RunAsync(Database db, CancellationToken cancellationToken)
{
if (db.Students.Any(s => s.Value.Courses.Any(c => c.CourseId == Id)))
{
Console.WriteLine("Can't remove a course referenced by a student.");
return (int)ExitCode.IdError;
}
if (!db.Courses.Remove(Id))
{
Console.WriteLine("No such course");
return (int)ExitCode.IdError;
}
await db.Save(Path, cancellationToken);
Console.WriteLine("Course removed.");
return (int)ExitCode.Success;
}
}