Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ListCommand.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Luticate2.Auth.Auth.Business;
  6. using Luticate2.Auth.Auth.Dbo;
  7. using Luticate2.Auth.Utils.Business.Fields;
  8. using Luticate2.Auth.Utils.Business.Pagination;
  9. using Luticate2.Auth.Utils.Business.Serializers.PartialJson;
  10. using Luticate2.Auth.Utils.Business.Utils;
  11. using Luticate2.Auth.Utils.Dbo.Pagination;
  12. using Microsoft.Extensions.DependencyInjection;
  13. using Microsoft.Extensions.Options;
  14. using NClap.Metadata;
  15. using Newtonsoft.Json;
  16. namespace Luticate2.Auth.ConsoleSample.Commands
  17. {
  18. public class ListCommand : Command
  19. {
  20. [PositionalArgument(ArgumentFlags.Optional, Position = 0, Description = "Items type to list")]
  21. public string ItemType { get; set; }
  22. [NamedArgument(DefaultValue = "*")]
  23. public string PartialResponse { get; set; }
  24. [NamedArgument(DefaultValue = "")]
  25. public string Filter { get; set; }
  26. [NamedArgument(ArgumentFlags.Multiple)]
  27. public string[] OrderBy { get; set; }
  28. [NamedArgument(DefaultValue = "0")]
  29. public int Page { get; set; }
  30. [NamedArgument(DefaultValue = "10")]
  31. public int PerPage { get; set; }
  32. [NamedArgument(DefaultValue = "specific")]
  33. public string OutputType { get; set; }
  34. public override Task<CommandResult> ExecuteAsync(CancellationToken cancel)
  35. {
  36. if (ItemType == null)
  37. {
  38. Console.WriteLine("groups");
  39. }
  40. else
  41. {
  42. var parserOptions = Program.ServiceProvider.GetRequiredService<IOptions<LuExpressionUtils.LuExpressionParserOptions>>();
  43. var partialResponseResult = LuPartialFieldsParser.Parse(PartialResponse);
  44. if (!partialResponseResult)
  45. {
  46. partialResponseResult.WriteLineError();
  47. return Task.FromResult(CommandResult.UsageError);
  48. }
  49. var partialResponse = partialResponseResult.Data;
  50. var filterResult = LuFilterParser.Parse<LuGroupDbo>(Filter, parserOptions.Value);
  51. if (!filterResult)
  52. {
  53. filterResult.WriteLineError();
  54. return Task.FromResult(CommandResult.UsageError);
  55. }
  56. var filter = filterResult.Data;
  57. var orderByResult = LuOrderByParser.Parse<LuGroupDbo>(OrderBy, parserOptions.Value);
  58. if (!orderByResult)
  59. {
  60. orderByResult.WriteLineError();
  61. return Task.FromResult(CommandResult.UsageError);
  62. }
  63. var orderBy = orderByResult.Data;
  64. var business = Program.ServiceProvider.GetService<LuGroupsBusiness>();
  65. var stopWatch = Stopwatch.StartNew();
  66. var results = business.Read(partialResponse, new LuPaginatedParamsDbo
  67. {
  68. Filter = filter,
  69. OrderBy = orderBy,
  70. Page = Page,
  71. PerPage = PerPage
  72. });
  73. var elapsed = stopWatch.ElapsedMilliseconds;
  74. if (results)
  75. {
  76. var jsonSettings = new JsonSerializerSettings
  77. {
  78. Formatting = Formatting.None
  79. };
  80. Console.WriteLine($"Displaying {Page * PerPage + 1}-{(Page + 1) * PerPage} of {results.Data.Count} ({elapsed} ms)");
  81. foreach (var item in results.Data.Data)
  82. {
  83. if (OutputType == "specific")
  84. {
  85. Console.WriteLine($"{item.Metadata.Id} {item.Name ?? "??"}");
  86. }
  87. else if (OutputType == "json-full")
  88. {
  89. Console.WriteLine(JsonConvert.SerializeObject(item, jsonSettings));
  90. }
  91. else if (OutputType == "json-partial")
  92. {
  93. Console.WriteLine(LuPartialJsonWriter.SerializeObject(item, partialResponse, jsonSettings));
  94. }
  95. }
  96. }
  97. else
  98. {
  99. results.WriteLineError();
  100. return Task.FromResult(CommandResult.RuntimeFailure);
  101. }
  102. }
  103. return Task.FromResult(CommandResult.Success);
  104. }
  105. }
  106. }