您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ListCommand.cs 4.2KB

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