aboutsummaryrefslogtreecommitdiff
path: root/docker/crupest-api/CrupestApi/Services/TodoService.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-12-01 19:19:47 +0800
committercrupest <crupest@outlook.com>2022-12-02 13:35:35 +0800
commitb44f957e6d886387e9f275dfb623540ae0acd737 (patch)
treedee523de3ff29067fa0202135a322079b295df4c /docker/crupest-api/CrupestApi/Services/TodoService.cs
parent462ae43246799469f0c6cdac5e45e1a9d0f5da2e (diff)
downloadcrupest-b44f957e6d886387e9f275dfb623540ae0acd737.tar.gz
crupest-b44f957e6d886387e9f275dfb623540ae0acd737.tar.bz2
crupest-b44f957e6d886387e9f275dfb623540ae0acd737.zip
Restructure crupest-api.
Diffstat (limited to 'docker/crupest-api/CrupestApi/Services/TodoService.cs')
-rw-r--r--docker/crupest-api/CrupestApi/Services/TodoService.cs146
1 files changed, 0 insertions, 146 deletions
diff --git a/docker/crupest-api/CrupestApi/Services/TodoService.cs b/docker/crupest-api/CrupestApi/Services/TodoService.cs
deleted file mode 100644
index eb4fef6..0000000
--- a/docker/crupest-api/CrupestApi/Services/TodoService.cs
+++ /dev/null
@@ -1,146 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Net.Http;
-using System.Net.Http.Headers;
-using System.Net.Mime;
-using System.Text;
-using System.Text.Json;
-using System.Threading.Tasks;
-using CrupestApi.Config;
-using Microsoft.Extensions.Logging;
-using Microsoft.Extensions.Options;
-
-namespace CrupestApi.Services
-{
- public class TodoItem
- {
- public string Status { get; set; } = default!;
- public string Title { get; set; } = default!;
- public bool Closed { get; set; }
- public string color { get; set; } = default!;
- }
-
- public class TodoService
- {
- private readonly IOptionsSnapshot<TodoConfiguration> _options;
- private readonly ILogger<TodoService> _logger;
-
- public TodoService(IOptionsSnapshot<TodoConfiguration> options, ILogger<TodoService> logger)
- {
- _options = options;
- _logger = logger;
- }
-
- private static string CreateGraphQLQuery(TodoConfiguration todoConfiguration)
- {
- return $$"""
-{
- user(login: "{{todoConfiguration.Username}}") {
- projectV2(number: {{todoConfiguration.ProjectNumber}}) {
- items(last: {{todoConfiguration.Count}}) {
- nodes {
- __typename
- content {
- __typename
- ... on Issue {
- title
- closed
- }
- ... on PullRequest {
- title
- closed
- }
- ... on DraftIssue {
- title
- }
- }
- }
- }
- }
- }
-}
-""";
- }
-
-
- public async Task<List<TodoItem>> GetTodosAsync()
- {
- var todoOptions = _options.Value;
- if (todoOptions is null)
- {
- throw new Exception("Fail to get todos configuration.");
- }
-
- _logger.LogInformation("Username: {}; ProjectNumber: {}; Count: {}", todoOptions.Username, todoOptions.ProjectNumber, todoOptions.Count);
- _logger.LogInformation("Getting todos from GitHub GraphQL API...");
-
- using var httpClient = new HttpClient();
-
- using var requestContent = new StringContent(JsonSerializer.Serialize(new
- {
- query = CreateGraphQLQuery(todoOptions)
- }));
- requestContent.Headers.ContentType = new MediaTypeHeaderValue(MediaTypeNames.Application.Json, Encoding.UTF8.WebName);
-
- using var request = new HttpRequestMessage(HttpMethod.Post, "https://api.github.com/graphql");
- request.Content = requestContent;
- request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", todoOptions.Token);
- request.Headers.TryAddWithoutValidation("User-Agent", todoOptions.Username);
-
- using var response = await httpClient.SendAsync(request);
- var responseBody = await response.Content.ReadAsStringAsync();
-
- _logger.LogInformation("GitHub server returned status code: {}", response.StatusCode);
- _logger.LogInformation("GitHub server returned body: {}", responseBody);
-
- if (response.IsSuccessStatusCode)
- {
- using var responseJson = JsonSerializer.Deserialize<JsonDocument>(responseBody);
- if (responseJson is null)
- {
- throw new Exception("Fail to deserialize response body.");
- }
-
- var nodes = responseJson.RootElement.GetProperty("data").GetProperty("user").GetProperty("projectV2").GetProperty("items").GetProperty("nodes").EnumerateArray();
-
- var result = new List<TodoItem>();
-
- foreach (var node in nodes)
- {
- var content = node.GetProperty("content");
- var title = content.GetProperty("title").GetString();
- if (title is null)
- {
- throw new Exception("Fail to get title.");
- }
- JsonElement closedElement;
- bool closed;
- if (content.TryGetProperty("closed", out closedElement))
- {
- closed = closedElement.GetBoolean();
- }
- else
- {
- closed = false;
- }
-
- result.Add(new TodoItem
- {
- Title = title,
- Status = closed ? "Done" : "Todo",
- Closed = closed,
- color = closed ? "green" : "blue"
- });
- }
-
- return result;
- }
- else
- {
- const string message = "Fail to get todos from GitHub.";
- _logger.LogError(message);
- throw new Exception(message);
- }
- }
- }
-} \ No newline at end of file