Skip to content

Commit

Permalink
Added ClientIdOrName parameter to allow editing/deleting clients by…
Browse files Browse the repository at this point in the history
… ID or name.
  • Loading branch information
rmaffitsancsoft committed Apr 14, 2024
1 parent 36ee5d9 commit 6314a7a
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/dotnet/HQ.Abstractions/Clients/DeleteClientV1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/dotnet/HQ.Abstractions/Clients/GetClientsV1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record>;
Expand Down
42 changes: 42 additions & 0 deletions src/dotnet/HQ.CLI/Commands/Clients/CreateClientCommand.cs
Original file line number Diff line number Diff line change
@@ -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<CreateClientSettings>
{
private readonly HQServiceV1 _hqService;

public CreateClientCommand(HQServiceV1 hqService)
{
_hqService = hqService;
}

public override async Task<int> ExecuteAsync(CommandContext context, CreateClientSettings settings)
{
var model = new CreateClientV1.Request();

var Createor = new YAMLEditor<CreateClientV1.Request>(model, async (value) =>
{
return await _hqService.CreateClientV1(value);
});

var rc = await Createor.Launch();
return rc;
}
}
}
6 changes: 3 additions & 3 deletions src/dotnet/HQ.CLI/Commands/Clients/DeleteClientCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace HQ.CLI.Commands.Clients
{
internal class DeleteClientSettings : HQCommandSettings
{
[CommandArgument(0, "<clientId>")]
public Guid ClientId { get; set; }
[CommandArgument(0, "<clientIdOrName>")]
public string? ClientIdOrName { get; set; }
}

internal class DeleteClientCommand : AsyncCommand<DeleteClientSettings>
Expand All @@ -31,7 +31,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, DeleteClien
{
var result = await _hqService.DeleteClientV1(new()
{
ClientId = settings.ClientId,
ClientIdOrName = settings.ClientIdOrName,
});

if (!result.IsSuccess || result.Value == null)
Expand Down
7 changes: 4 additions & 3 deletions src/dotnet/HQ.CLI/Commands/Clients/EditClientCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace HQ.CLI.Commands.Clients
{
internal class EditClientSettings : HQCommandSettings
{
[CommandArgument(0, "<clientId>")]
public Guid ClientId { get; set; }
[CommandArgument(0, "<clientIdOrName>")]
public string? ClientIdOrName { get; set; }
}

internal class EditClientCommand : AsyncCommand<EditClientSettings>
Expand All @@ -32,7 +32,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, EditClientS
{
var result = await _hqService.GetClientsV1(new()
{
ClientId = settings.ClientId,
ClientIdOrName = settings.ClientIdOrName,
});

if (!result.IsSuccess || result.Value == null)
Expand All @@ -55,6 +55,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, EditClientS

var editor = new YAMLEditor<UpdateClientV1.Request>(model, async (value) =>
{
value.ClientId = record.ClientId;
return await _hqService.UpdateClientV1(value);
});

Expand Down
6 changes: 3 additions & 3 deletions src/dotnet/HQ.CLI/Commands/Clients/GetClientsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -35,7 +35,7 @@ public override async Task<int> 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)
Expand Down
7 changes: 7 additions & 0 deletions src/dotnet/HQ.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@
.WithAlias("clients")
.WithAlias("cl");
});

config.AddBranch("create", branch =>
{
branch.AddCommand<CreateClientCommand>("client")
.WithAlias("clients")
.WithAlias("cl");
});
});

var rc = await app.RunAsync(args);
Expand Down
42 changes: 41 additions & 1 deletion src/dotnet/HQ.Server/API/Clients/ClientServiceV1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ public ClientServiceV1(HQDbContext context)
_context = context;
}

private IQueryable<Client> FilterClientIdOrName(IQueryable<Client> 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<Result<CreateClientV1.Response>> CreateClientV1(CreateClientV1.Request request, CancellationToken ct = default)
{
var validationResult = Result.Merge(
Expand Down Expand Up @@ -72,7 +86,28 @@ public ClientServiceV1(HQDbContext context)

public async Task<Result<DeleteClientV1.Response?>> 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<DeleteClientV1.Response?>(null);
Expand Down Expand Up @@ -102,6 +137,11 @@ public ClientServiceV1(HQDbContext context)

var total = await records.CountAsync(ct);

if(!String.IsNullOrEmpty(request.ClientIdOrName))
{
records = FilterClientIdOrName(records, request.ClientIdOrName);

Check failure on line 142 in src/dotnet/HQ.Server/API/Clients/ClientServiceV1.cs

View workflow job for this annotation

GitHub Actions / build-dotnet

Argument 1: cannot convert from 'System.Linq.IQueryable<HQ.Abstractions.Clients.GetClientsV1.Record>' to 'System.Linq.IQueryable<HQ.Server.Data.Models.Client>'

Check failure on line 142 in src/dotnet/HQ.Server/API/Clients/ClientServiceV1.cs

View workflow job for this annotation

GitHub Actions / build-dotnet

Argument 1: cannot convert from 'System.Linq.IQueryable<HQ.Abstractions.Clients.GetClientsV1.Record>' to 'System.Linq.IQueryable<HQ.Server.Data.Models.Client>'
}

if (!string.IsNullOrEmpty(request.Search))
{
records = records.Where(t =>
Expand Down

0 comments on commit 6314a7a

Please sign in to comment.