From 6314a7a8e55263a464fdbaf886fea5e5bdfb58ac Mon Sep 17 00:00:00 2001 From: Ryan Maffit Date: Sun, 14 Apr 2024 17:49:44 -0400 Subject: [PATCH] Added `ClientIdOrName` parameter to allow editing/deleting clients by ID or name. --- .../HQ.Abstractions/Clients/DeleteClientV1.cs | 3 +- .../HQ.Abstractions/Clients/GetClientsV1.cs | 1 + .../Commands/Clients/CreateClientCommand.cs | 42 +++++++++++++++++++ .../Commands/Clients/DeleteClientCommand.cs | 6 +-- .../Commands/Clients/EditClientCommand.cs | 7 ++-- .../Commands/Clients/GetClientsCommand.cs | 6 +-- src/dotnet/HQ.CLI/Program.cs | 7 ++++ .../HQ.Server/API/Clients/ClientServiceV1.cs | 42 ++++++++++++++++++- 8 files changed, 103 insertions(+), 11 deletions(-) create mode 100644 src/dotnet/HQ.CLI/Commands/Clients/CreateClientCommand.cs diff --git a/src/dotnet/HQ.Abstractions/Clients/DeleteClientV1.cs b/src/dotnet/HQ.Abstractions/Clients/DeleteClientV1.cs index 2e387fa6..ea2559d3 100644 --- a/src/dotnet/HQ.Abstractions/Clients/DeleteClientV1.cs +++ b/src/dotnet/HQ.Abstractions/Clients/DeleteClientV1.cs @@ -11,7 +11,8 @@ public class DeleteClientV1 { public class Request { - public Guid ClientId { get; set; } + public Guid? ClientId { get; set; } + public string? ClientIdOrName { get; set; } } public class Response : NoContentResponseV1 diff --git a/src/dotnet/HQ.Abstractions/Clients/GetClientsV1.cs b/src/dotnet/HQ.Abstractions/Clients/GetClientsV1.cs index 7c2f6306..044142f1 100644 --- a/src/dotnet/HQ.Abstractions/Clients/GetClientsV1.cs +++ b/src/dotnet/HQ.Abstractions/Clients/GetClientsV1.cs @@ -13,6 +13,7 @@ public class Request : PagedRequestV1 { public string? Search { get; set; } public Guid? ClientId { get; set; } + public string? ClientIdOrName { get; set; } } public class Response : PagedResponseV1; diff --git a/src/dotnet/HQ.CLI/Commands/Clients/CreateClientCommand.cs b/src/dotnet/HQ.CLI/Commands/Clients/CreateClientCommand.cs new file mode 100644 index 00000000..61ab8ddd --- /dev/null +++ b/src/dotnet/HQ.CLI/Commands/Clients/CreateClientCommand.cs @@ -0,0 +1,42 @@ +using FluentResults; +using HQ.Abstractions.Clients; +using HQ.SDK; +using Spectre.Console; +using Spectre.Console.Cli; +using Spectre.Console.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; + +namespace HQ.CLI.Commands.Clients +{ + internal class CreateClientSettings : HQCommandSettings + { + } + + internal class CreateClientCommand : AsyncCommand + { + private readonly HQServiceV1 _hqService; + + public CreateClientCommand(HQServiceV1 hqService) + { + _hqService = hqService; + } + + public override async Task ExecuteAsync(CommandContext context, CreateClientSettings settings) + { + var model = new CreateClientV1.Request(); + + var Createor = new YAMLEditor(model, async (value) => + { + return await _hqService.CreateClientV1(value); + }); + + var rc = await Createor.Launch(); + return rc; + } + } +} diff --git a/src/dotnet/HQ.CLI/Commands/Clients/DeleteClientCommand.cs b/src/dotnet/HQ.CLI/Commands/Clients/DeleteClientCommand.cs index 67092bbd..c68a0629 100644 --- a/src/dotnet/HQ.CLI/Commands/Clients/DeleteClientCommand.cs +++ b/src/dotnet/HQ.CLI/Commands/Clients/DeleteClientCommand.cs @@ -14,8 +14,8 @@ namespace HQ.CLI.Commands.Clients { internal class DeleteClientSettings : HQCommandSettings { - [CommandArgument(0, "")] - public Guid ClientId { get; set; } + [CommandArgument(0, "")] + public string? ClientIdOrName { get; set; } } internal class DeleteClientCommand : AsyncCommand @@ -31,7 +31,7 @@ public override async Task ExecuteAsync(CommandContext context, DeleteClien { var result = await _hqService.DeleteClientV1(new() { - ClientId = settings.ClientId, + ClientIdOrName = settings.ClientIdOrName, }); if (!result.IsSuccess || result.Value == null) diff --git a/src/dotnet/HQ.CLI/Commands/Clients/EditClientCommand.cs b/src/dotnet/HQ.CLI/Commands/Clients/EditClientCommand.cs index 1ab77099..e0f9638c 100644 --- a/src/dotnet/HQ.CLI/Commands/Clients/EditClientCommand.cs +++ b/src/dotnet/HQ.CLI/Commands/Clients/EditClientCommand.cs @@ -15,8 +15,8 @@ namespace HQ.CLI.Commands.Clients { internal class EditClientSettings : HQCommandSettings { - [CommandArgument(0, "")] - public Guid ClientId { get; set; } + [CommandArgument(0, "")] + public string? ClientIdOrName { get; set; } } internal class EditClientCommand : AsyncCommand @@ -32,7 +32,7 @@ public override async Task ExecuteAsync(CommandContext context, EditClientS { var result = await _hqService.GetClientsV1(new() { - ClientId = settings.ClientId, + ClientIdOrName = settings.ClientIdOrName, }); if (!result.IsSuccess || result.Value == null) @@ -55,6 +55,7 @@ public override async Task ExecuteAsync(CommandContext context, EditClientS var editor = new YAMLEditor(model, async (value) => { + value.ClientId = record.ClientId; return await _hqService.UpdateClientV1(value); }); diff --git a/src/dotnet/HQ.CLI/Commands/Clients/GetClientsCommand.cs b/src/dotnet/HQ.CLI/Commands/Clients/GetClientsCommand.cs index e2e817b1..0e69cd5d 100644 --- a/src/dotnet/HQ.CLI/Commands/Clients/GetClientsCommand.cs +++ b/src/dotnet/HQ.CLI/Commands/Clients/GetClientsCommand.cs @@ -14,8 +14,8 @@ namespace HQ.CLI.Commands.Clients { internal class GetClientsSettings : HQCommandSettings { - [CommandArgument(0, "[clientId]")] - public Guid? ClientId { get; set; } + [CommandArgument(0, "[clientIdOrName]")] + public string? ClientIdOrName { get; set; } [CommandOption("--search|-s")] public string? Search { get; set; } @@ -35,7 +35,7 @@ public override async Task ExecuteAsync(CommandContext context, GetClientsS var result = await _hqService.GetClientsV1(new() { Search = settings.Search, - ClientId = settings.ClientId, + ClientIdOrName = settings.ClientIdOrName, }); if (!result.IsSuccess || result.Value == null) diff --git a/src/dotnet/HQ.CLI/Program.cs b/src/dotnet/HQ.CLI/Program.cs index 467fb41b..552b2bb0 100644 --- a/src/dotnet/HQ.CLI/Program.cs +++ b/src/dotnet/HQ.CLI/Program.cs @@ -79,6 +79,13 @@ .WithAlias("clients") .WithAlias("cl"); }); + + config.AddBranch("create", branch => + { + branch.AddCommand("client") + .WithAlias("clients") + .WithAlias("cl"); + }); }); var rc = await app.RunAsync(args); diff --git a/src/dotnet/HQ.Server/API/Clients/ClientServiceV1.cs b/src/dotnet/HQ.Server/API/Clients/ClientServiceV1.cs index d0af727e..c23c47e1 100644 --- a/src/dotnet/HQ.Server/API/Clients/ClientServiceV1.cs +++ b/src/dotnet/HQ.Server/API/Clients/ClientServiceV1.cs @@ -15,6 +15,20 @@ public ClientServiceV1(HQDbContext context) _context = context; } + private IQueryable FilterClientIdOrName(IQueryable records, string clientIdOrName) + { + if (Guid.TryParse(clientIdOrName, out Guid clientId)) + { + records = records.Where(t => t.Id == clientId); + } + else + { + records = records.Where(t => t.Name.ToLower() == clientIdOrName.ToLower()); + } + + return records; + } + public async Task> CreateClientV1(CreateClientV1.Request request, CancellationToken ct = default) { var validationResult = Result.Merge( @@ -72,7 +86,28 @@ public ClientServiceV1(HQDbContext context) public async Task> DeleteClientV1(DeleteClientV1.Request request, CancellationToken ct = default) { - var client = await _context.Clients.FindAsync(request.ClientId, ct); + var validationResult = Result.Merge( + Result.FailIf(String.IsNullOrEmpty(request.ClientIdOrName) && !request.ClientId.HasValue, "ClientIdOrName or ClientId must be specified.") + ); + + if (validationResult.IsFailed) + { + return validationResult; + } + + var records = _context.Clients.AsQueryable(); + + if (!String.IsNullOrEmpty(request.ClientIdOrName)) + { + records = FilterClientIdOrName(records, request.ClientIdOrName); + } + + if(request.ClientId.HasValue) + { + records = records.Where(t => t.Id == request.ClientId.Value); + } + + var client = await records.SingleOrDefaultAsync(ct); if (client == null) { return Result.Ok(null); @@ -102,6 +137,11 @@ public ClientServiceV1(HQDbContext context) var total = await records.CountAsync(ct); + if(!String.IsNullOrEmpty(request.ClientIdOrName)) + { + records = FilterClientIdOrName(records, request.ClientIdOrName); + } + if (!string.IsNullOrEmpty(request.Search)) { records = records.Where(t =>