123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- using System;
- using System.Diagnostics;
- using System.Threading;
- using System.Threading.Tasks;
- using Luticate2.Auth.Business.Auth;
- using Luticate2.Auth.Business.Pagination;
- using Luticate2.Auth.Business.Fields;
- using Luticate2.Auth.Business.Serializers.PartialJson;
- using Luticate2.Auth.DataAccess.Models;
- using Luticate2.Auth.Dbo.Auth;
- using Luticate2.Auth.Dbo.Pagination;
- using Microsoft.Extensions.DependencyInjection;
- using NClap.Metadata;
- using Newtonsoft.Json;
-
- namespace Luticate2.Auth.ConsoleSample.Commands
- {
- public class ListCommand : Command
- {
- [PositionalArgument(ArgumentFlags.Optional, Position = 0, Description = "Items type to list")]
- public string ItemType { get; set; }
-
- [NamedArgument(DefaultValue = "*")]
- public string PartialResponse { get; set; }
-
- [NamedArgument(DefaultValue = "")]
- public string Filter { get; set; }
-
- [NamedArgument(DefaultValue = "id:asc")]
- public string OrderBy { get; set; }
-
- [NamedArgument(DefaultValue = "0")]
- public int Page { get; set; }
-
- [NamedArgument(DefaultValue = "10")]
- public int PerPage { get; set; }
-
- [NamedArgument(DefaultValue = "specific")]
- public string OutputType { get; set; }
-
- public override Task<CommandResult> ExecuteAsync(CancellationToken cancel)
- {
- if (ItemType == null)
- {
- Console.WriteLine("groups");
- }
- else
- {
- var partialResponseResult = LuPartialFieldsParser.Parse(PartialResponse);
- if (!partialResponseResult)
- {
- partialResponseResult.WriteLineError();
- return Task.FromResult(CommandResult.UsageError);
- }
- var partialResponse = partialResponseResult.Data;
-
- var filterResult = LuFilterParser.Parse<LuGroupDbo>(Filter);
- if (!filterResult)
- {
- filterResult.WriteLineError();
- return Task.FromResult(CommandResult.UsageError);
- }
- var filter = filterResult.Data;
-
- var orderByResult = LuOrderByParser.Parse(OrderBy);
- if (!orderByResult)
- {
- orderByResult.WriteLineError();
- return Task.FromResult(CommandResult.UsageError);
- }
- var orderBy = orderByResult.Data;
-
- var business = Program.ServiceProvider.GetService<LuGroupsBusiness>();
- var stopWatch = Stopwatch.StartNew();
- var results = business.Read(partialResponse, new LuPaginatedParamsDbo
- {
- Filter = filter,
- OrderBy = orderBy,
- Page = Page,
- PerPage = PerPage
- });
- var elapsed = stopWatch.ElapsedMilliseconds;
- if (results)
- {
- var jsonSettings = new JsonSerializerSettings
- {
- Formatting = Formatting.None
- };
- Console.WriteLine($"Displaying {Page * PerPage + 1}-{(Page + 1) * PerPage} of {results.Data.Count} ({elapsed} ms)");
- foreach (var item in results.Data.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 Task.FromResult(CommandResult.Success);
- }
- }
- }
|