123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- using System;
- using System.Diagnostics;
- using System.Threading;
- using System.Threading.Tasks;
- using Luticate2.Auth.Auth.Business;
- using Luticate2.Auth.Auth.Dbo;
- using Luticate2.Auth.Utils.Business.Fields;
- using Luticate2.Auth.Utils.Business.Pagination;
- using Luticate2.Auth.Utils.Business.Serializers.PartialJson;
- using Luticate2.Auth.Utils.Business.Utils;
- using Luticate2.Auth.Utils.Dbo.Pagination;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Options;
- using NClap.Metadata;
- using Newtonsoft.Json;
-
- namespace Luticate2.Auth.ConsoleSample.Commands
- {
- public class ListCommand : Command
- {
- [PositionalArgument(ArgumentFlags.Optional, Position = 0, Description = "Items type to list")]
- public string ItemType { get; set; }
-
- [NamedArgument(DefaultValue = "*")]
- public string PartialResponse { get; set; }
-
- [NamedArgument(DefaultValue = "")]
- public string Filter { get; set; }
-
- [NamedArgument(ArgumentFlags.Multiple)]
- public string[] OrderBy { get; set; }
-
- [NamedArgument(DefaultValue = "0")]
- public int Page { get; set; }
-
- [NamedArgument(DefaultValue = "10")]
- public int PerPage { get; set; }
-
- [NamedArgument(DefaultValue = "specific")]
- public string OutputType { get; set; }
-
- public override Task<CommandResult> ExecuteAsync(CancellationToken cancel)
- {
- if (ItemType == null)
- {
- Console.WriteLine("groups");
- }
- else
- {
- var parserOptions = Program.ServiceProvider.GetRequiredService<IOptions<LuExpressionUtils.LuExpressionParserOptions>>();
-
- var partialResponseResult = LuPartialFieldsParser.Parse(PartialResponse);
- if (!partialResponseResult)
- {
- partialResponseResult.WriteLineError();
- return Task.FromResult(CommandResult.UsageError);
- }
- var partialResponse = partialResponseResult.Data;
-
- var filterResult = LuFilterParser.Parse<LuGroupDbo>(Filter, parserOptions.Value);
- if (!filterResult)
- {
- filterResult.WriteLineError();
- return Task.FromResult(CommandResult.UsageError);
- }
- var filter = filterResult.Data;
-
- var orderByResult = LuOrderByParser.Parse<LuGroupDbo>(OrderBy, parserOptions.Value);
- if (!orderByResult)
- {
- orderByResult.WriteLineError();
- return Task.FromResult(CommandResult.UsageError);
- }
- var orderBy = orderByResult.Data;
-
- var business = Program.ServiceProvider.GetService<LuGroupsBusiness>();
- var stopWatch = Stopwatch.StartNew();
- var results = business.Read(partialResponse, new LuPaginatedParamsDbo
- {
- Filter = filter,
- OrderBy = orderBy,
- Page = Page,
- PerPage = PerPage
- });
- var elapsed = stopWatch.ElapsedMilliseconds;
- if (results)
- {
- var jsonSettings = new JsonSerializerSettings
- {
- Formatting = Formatting.None
- };
- Console.WriteLine($"Displaying {Page * PerPage + 1}-{(Page + 1) * PerPage} of {results.Data.Count} ({elapsed} ms)");
- foreach (var item in results.Data.Data)
- {
- if (OutputType == "specific")
- {
- Console.WriteLine($"{item.Metadata.Id} {item.Name ?? "??"}");
- }
- else if (OutputType == "json-full")
- {
- Console.WriteLine(JsonConvert.SerializeObject(item, jsonSettings));
- }
- else if (OutputType == "json-partial")
- {
- Console.WriteLine(LuPartialJsonWriter.SerializeObject(item, partialResponse, jsonSettings));
- }
- }
- }
- else
- {
- results.WriteLineError();
- return Task.FromResult(CommandResult.RuntimeFailure);
- }
- }
-
- return Task.FromResult(CommandResult.Success);
- }
- }
- }
|