using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Nodes;
using FsTool;
using FsTool.Helpers;
namespace FsTool.Tasks
{
public static partial class StoreTasks
{
///
/// Retrieves the members of the specified store, requiring authentication before the request.
///
/// The HTTP client used to send the request.
/// The store identifier to query.
/// A list of store members, or an empty list when the call fails.
public static async Task> 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
var opts = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = null // <── WICHTIG
};
return root.Deserialize>(opts) ?? [];
}
///
/// Retrieves all pickups for the specified store with authentication handled automatically.
///
/// The HTTP client used to send the request.
/// The store identifier to query.
/// A list of pickups, or an empty list when no data is available.
public static async Task> 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
var opts = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = null // <── WICHTIG
};
return root["pickups"].Deserialize>(opts) ?? [];
}
///
/// Marks a pickup slot as confirmed for the specified store, date, and user.
///
/// The HTTP client used to send the request.
/// The store identifier containing the pickup.
/// The date of the pickup to patch.
/// The Foodsharing user identifier associated with the slot.
/// A task representing the asynchronous operation.
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}");
}
}
}