Files
FsToolbox/Cli/Tasks/StoreTasks.cs
2025-12-12 08:23:19 +01:00

113 lines
4.7 KiB
C#

using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Nodes;
using FsToolbox.Cli.Helper;
namespace FsToolbox.Cli.Tasks
{
public static partial class StoreTasks
{
#region Public Method GetPickupsAsync
/// <summary>
/// Retrieves all pickups for the specified store with authentication handled automatically.
/// </summary>
/// <param name="httpClient">The HTTP client used to send the request.</param>
/// <param name="storeId">The store identifier to query.</param>
/// <returns>A list of pickups, or an empty list when no data is available.</returns>
public static async Task<List<Pickup>> GetPickupsAsync(HttpClient httpClient, int storeId)
{
await AuthHelper.EnsureAuthenticationAsync(httpClient);
var uri = string.Format(Endpoints.StorePickups, storeId);
var response = await httpClient.GetAsync(uri);
var responseBody = await response.Content.ReadAsStringAsync();
// handle unsuccessful response
if (!response.IsSuccessStatusCode) await Console.Error.WriteLineAsync($"Pickup retrieval failed ({(int)response.StatusCode} {response.ReasonPhrase}): {responseBody}");
var root = JsonNode.Parse(responseBody);
if (root == null) return [];
// Deserialize JsonNode to List<Pickup>
var opts = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = null // <── WICHTIG
};
return root["pickups"].Deserialize<List<Pickup>>(opts) ?? [];
}
#endregion
#region Public Method GetStoreMembersAsync
/// <summary>
/// Retrieves the members of the specified store, requiring authentication before the request.
/// </summary>
/// <param name="httpClient">The HTTP client used to send the request.</param>
/// <param name="storeId">The store identifier to query.</param>
/// <returns>A list of store members, or an empty list when the call fails.</returns>
public static async Task<List<Member>> GetStoreMembersAsync(HttpClient httpClient, int storeId)
{
await AuthHelper.EnsureAuthenticationAsync(httpClient);
var uri = string.Format(Endpoints.StoreMembers, storeId);
var response = await httpClient.GetAsync(uri);
var responseBody = await response.Content.ReadAsStringAsync();
// handle unsuccessful response
if (!response.IsSuccessStatusCode)
{
await Console.Error.WriteLineAsync($"Store members retrieval failed ({(int)response.StatusCode} {response.ReasonPhrase}): {responseBody}");
return [];
}
var root = JsonNode.Parse(responseBody);
if (root == null) return [];
// Deserialize JsonNode to List<Pickup>
var opts = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = null // <── WICHTIG
};
return root.Deserialize<List<Member>>(opts) ?? [];
}
#endregion
#region Public Method PatchPickupAsync
/// <summary>
/// Marks a pickup slot as confirmed for the specified store, date, and user.
/// </summary>
/// <param name="httpClient">The HTTP client used to send the request.</param>
/// <param name="storeId">The store identifier containing the pickup.</param>
/// <param name="pickupDate">The date of the pickup to patch.</param>
/// <param name="fsId">The Foodsharing user identifier associated with the slot.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public static async Task PatchPickupAsync(HttpClient httpClient, int storeId, string pickupDate, int fsId)
{
await AuthHelper.EnsureAuthenticationAsync(httpClient);
var uri = string.Format(Endpoints.StorePickupsSlot, storeId, pickupDate, fsId);
var payload = new JsonObject
{
["isConfirmed"] = true
};
var response = await httpClient.PatchAsync(uri, JsonContent.Create(payload));
var responseBody = await response.Content.ReadAsStringAsync();
// handle unsuccessful response
if (!response.IsSuccessStatusCode)
await Console.Error.WriteLineAsync($"Pickup patch failed ({(int)response.StatusCode} {response.ReasonPhrase}): {responseBody}");
else
Console.WriteLine($"Pickup patch succeeded {fsId} on {pickupDate}");
}
#endregion
}
}