Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add search functionality #18

Merged
merged 1 commit into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/add_search_functinality.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
default: minor
---

# Add search functionality
113 changes: 113 additions & 0 deletions QuickCapacities/CapacitiesApiService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Spectre.Console;

namespace QuickCapacities;
Expand Down Expand Up @@ -61,4 +62,116 @@ public static async Task<int> SendWeblink(string link, string spacesId)
return 1;
}
}

public static async Task<int> Search(string phrase, string spacesId)
{
const string url = "https://api.capacities.io/search";

var data = new
{
mode = "fullText",
spaceIds = (List<string>) [spacesId],
searchTerm = phrase
};

string json = JsonSerializer.Serialize(data);
using HttpClient client = new();
try
{
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + ConfigurationService.GetApiKey());
HttpContent content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(url, content);
response.EnsureSuccessStatusCode();

// AnsiConsole.WriteLine(PrettyJson(response.Content.ReadAsStringAsync().Result));
SearchResponse? responseData =
JsonSerializer.Deserialize<SearchResponse>(response.Content.ReadAsStringAsync().Result);
if (responseData is null)
{
return 1;
}

responseData.Print();
return 0;
}
catch (HttpRequestException e)
{
AnsiConsole.WriteLine($"Request error: {e.Message}");
return 1;
}
}

private static string PrettyJson(string unPrettyJson)
{
JsonSerializerOptions options = new()
{
WriteIndented = true
};

JsonElement jsonElement = JsonSerializer.Deserialize<JsonElement>(unPrettyJson);

return JsonSerializer.Serialize(jsonElement, options);
}
}

public class SearchResult
{
[JsonPropertyName("id")] public required string Id { get; set; }
[JsonPropertyName("spaceId")] public required string SpaceId { get; set; }
[JsonPropertyName("structureId")] public required string StructureId { get; set; }
[JsonPropertyName("title")] public required string Title { get; set; }
[JsonPropertyName("highlights")] public required List<Highlights> Highlights { get; set; }

public void Print()
{
foreach (Highlights highlight in Highlights)
{
AnsiConsole.MarkupLine($"[bold]{Title}[/]");
highlight.Print(Id, SpaceId);
}
}
}

public class Highlights
{
[JsonPropertyName("context")] public required ResponseContext ResponseContext { get; set; }
[JsonPropertyName("snippets")] public required List<string> Snippets { get; set; }

public void Print(string id, string spaceId)
{
foreach (string snippet in Snippets)
{
AnsiConsole.WriteLine("\t" + snippet);
}

string link = "\thttps://app.capacities.io/";
link += spaceId;
link += "/";
link += id;
if (ResponseContext.BlockId is not null)
{
link += $"?bid={ResponseContext.BlockId}";
}

AnsiConsole.WriteLine(link);
}
}

public class ResponseContext
{
[JsonPropertyName("field")] public required string Field { get; set; }
[JsonPropertyName("blockId")] public string? BlockId { get; set; }
}

public class SearchResponse
{
[JsonPropertyName("results")] public required List<SearchResult> Results { get; init; }

public void Print()
{
foreach (SearchResult result in Results)
{
result.Print();
}
}
}
1 change: 1 addition & 0 deletions QuickCapacities/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static int Main(string[] args)
config.AddCommand<AddWeblinkCommand>("weblink");
config.AddCommand<ShowConfigCommand>("show-config");
config.AddCommand<SetConfigCommand>("set-config");
config.AddCommand<SearchCommand>("search");
});
return app.Run(args);
}
Expand Down
26 changes: 26 additions & 0 deletions QuickCapacities/SearchCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.ComponentModel;
using Spectre.Console;
using Spectre.Console.Cli;

namespace QuickCapacities;

public class SearchCommand : AsyncCommand<SearchCommand.Settings>
{
public sealed class Settings : CommandSettings
{
[Description("Phrase, enclose in quotes for phrase with spaces")]
[CommandArgument(0, "[phrase]")]
public required string? Phrase { get; init; }

[CommandOption("-s|--spaces-id")] public string? SpacesId { get; init; }
}

public override Task<int> ExecuteAsync(CommandContext context, Settings settings)
{
string phrase = "";
phrase = settings.Phrase ?? AnsiConsole.Prompt(new TextPrompt<string>("Phrase: "));

return CapacitiesApiService.Search(phrase,
settings.SpacesId ?? ConfigurationService.GetDefaultSpacesId());
}
}