Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CreateCommand.cs 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Luticate2.Auth.Business.Auth;
  6. using Luticate2.Auth.Business.Fields;
  7. using Luticate2.Auth.Business.Serializers.PartialJson;
  8. using Luticate2.Auth.Dbo.Auth;
  9. using Microsoft.Extensions.DependencyInjection;
  10. using NClap.Metadata;
  11. using Newtonsoft.Json;
  12. namespace Luticate2.Auth.ConsoleSample.Commands
  13. {
  14. public class CreateCommand : Command
  15. {
  16. [PositionalArgument(ArgumentFlags.Required, Position = 0, Description = "Items type to create")]
  17. public string ItemType { get; set; }
  18. [PositionalArgument(ArgumentFlags.Required, Position = 1, Description = "Items to create")]
  19. public string Items { get; set; }
  20. [NamedArgument(DefaultValue = "*")]
  21. public string PartialResponse { get; set; }
  22. [NamedArgument(DefaultValue = "*")]
  23. public string PartialInput { get; set; }
  24. [NamedArgument(DefaultValue = "specific")]
  25. public string OutputType { get; set; }
  26. public override Task<CommandResult> ExecuteAsync(CancellationToken cancel)
  27. {
  28. var partialResponseResult = LuPartialFieldsParser.Parse(PartialResponse);
  29. if (!partialResponseResult)
  30. {
  31. partialResponseResult.WriteLineError();
  32. return Task.FromResult(CommandResult.UsageError);
  33. }
  34. var partialResponse = partialResponseResult.Data;
  35. var partialInputResult = LuPartialFieldsParser.Parse(PartialInput);
  36. if (!partialInputResult)
  37. {
  38. partialInputResult.WriteLineError();
  39. return Task.FromResult(CommandResult.UsageError);
  40. }
  41. var partialInput = partialInputResult.Data;
  42. var business = Program.ServiceProvider.GetService<LuGroupsBusiness>();
  43. var items = JsonConvert.DeserializeObject<IList<LuGroupDbo>>(Items);
  44. var results = business.Create(partialResponse, partialInput, items);
  45. if (results)
  46. {
  47. var jsonSettings = new JsonSerializerSettings
  48. {
  49. Formatting = Formatting.None
  50. };
  51. foreach (var item in results.Data)
  52. {
  53. if (OutputType == "specific")
  54. {
  55. Console.WriteLine($"{item.Id} {item.Name ?? "??"}");
  56. }
  57. else if (OutputType == "json-full")
  58. {
  59. Console.WriteLine(JsonConvert.SerializeObject(item, jsonSettings));
  60. }
  61. else if (OutputType == "json-partial")
  62. {
  63. Console.WriteLine(LuPartialJsonWriter.SerializeObject(item, partialResponse, jsonSettings));
  64. }
  65. }
  66. }
  67. else
  68. {
  69. results.WriteLineError();
  70. return Task.FromResult(CommandResult.RuntimeFailure);
  71. }
  72. return base.ExecuteAsync(cancel);
  73. }
  74. }
  75. }