You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ListCommand.cs 4.2KB

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