123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- using System;
- using System.Collections.Generic;
- using System.Threading;
- using System.Threading.Tasks;
- using Luticate2.Auth.Business.Auth;
- using Luticate2.Auth.Business.Fields;
- using Luticate2.Auth.Business.Serializers.PartialJson;
- using Luticate2.Auth.Dbo.Auth;
- using Microsoft.Extensions.DependencyInjection;
- using NClap.Metadata;
- using Newtonsoft.Json;
-
- namespace Luticate2.Auth.ConsoleSample.Commands
- {
- public class CreateCommand : Command
- {
- [PositionalArgument(ArgumentFlags.Required, Position = 0, Description = "Items type to create")]
- public string ItemType { get; set; }
-
- [PositionalArgument(ArgumentFlags.Required, Position = 1, Description = "Items to create")]
- public string Items { get; set; }
-
- [NamedArgument(DefaultValue = "*")]
- public string PartialResponse { get; set; }
-
- [NamedArgument(DefaultValue = "*")]
- public string PartialInput { get; set; }
-
- [NamedArgument(DefaultValue = "specific")]
- public string OutputType { get; set; }
-
- public override Task<CommandResult> ExecuteAsync(CancellationToken cancel)
- {
- var partialResponseResult = LuPartialFieldsParser.Parse(PartialResponse);
- if (!partialResponseResult)
- {
- partialResponseResult.WriteLineError();
- return Task.FromResult(CommandResult.UsageError);
- }
- var partialResponse = partialResponseResult.Data;
-
- var partialInputResult = LuPartialFieldsParser.Parse(PartialInput);
- if (!partialInputResult)
- {
- partialInputResult.WriteLineError();
- return Task.FromResult(CommandResult.UsageError);
- }
- var partialInput = partialInputResult.Data;
-
- var business = Program.ServiceProvider.GetService<LuGroupsBusiness>();
-
- var items = JsonConvert.DeserializeObject<IList<LuGroupDbo>>(Items);
-
- var results = business.Create(partialResponse, partialInput, items);
-
- if (results)
- {
- var jsonSettings = new JsonSerializerSettings
- {
- Formatting = Formatting.None
- };
- foreach (var item in results.Data)
- {
- if (OutputType == "specific")
- {
- Console.WriteLine($"{item.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 base.ExecuteAsync(cancel);
- }
- }
- }
|