Gathering infra
Deploy Promiscuity Auth API / deploy (push) Successful in 46s
Deploy Promiscuity Character API / deploy (push) Successful in 45s
Deploy Promiscuity Inventory API / deploy (push) Successful in 46s
Deploy Promiscuity Locations API / deploy (push) Successful in 1m3s
k8s smoke test / test (push) Successful in 7s

This commit is contained in:
2026-03-15 14:04:12 -05:00
parent a2a4d48de5
commit 9ba725d207
10 changed files with 273 additions and 22 deletions
@@ -3,6 +3,10 @@ using LocationsApi.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Driver;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Text;
using System.Text.Json;
namespace LocationsApi.Controllers;
@@ -11,10 +15,14 @@ namespace LocationsApi.Controllers;
public class LocationsController : ControllerBase
{
private readonly LocationStore _locations;
private readonly IHttpClientFactory _httpClientFactory;
private readonly IConfiguration _configuration;
public LocationsController(LocationStore locations)
public LocationsController(LocationStore locations, IHttpClientFactory httpClientFactory, IConfiguration configuration)
{
_locations = locations;
_httpClientFactory = httpClientFactory;
_configuration = configuration;
}
[HttpPost]
@@ -85,4 +93,67 @@ public class LocationsController : ControllerBase
return Ok("Updated");
}
[HttpPost("{id}/gather")]
[Authorize(Roles = "USER,SUPER")]
public async Task<IActionResult> Gather(string id, [FromBody] GatherResourceRequest req)
{
if (string.IsNullOrWhiteSpace(req.CharacterId))
return BadRequest("characterId required");
if (string.IsNullOrWhiteSpace(req.ResourceKey))
return BadRequest("resourceKey required");
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrWhiteSpace(userId))
return Unauthorized();
var allowAnyOwner = User.IsInRole("SUPER");
var gather = await _locations.GatherResourceAsync(id, req.CharacterId, req.ResourceKey, userId, allowAnyOwner);
if (gather.Status == GatherStatus.LocationNotFound)
return NotFound("Location not found");
if (gather.Status == GatherStatus.CharacterNotFound)
return NotFound("Character not found");
if (gather.Status == GatherStatus.Forbidden)
return Forbid();
if (gather.Status == GatherStatus.Invalid)
return BadRequest("Character is not at the target location");
if (gather.Status == GatherStatus.ResourceNotFound)
return NotFound("Resource not found at location");
if (gather.Status == GatherStatus.ResourceDepleted)
return Conflict("Resource is depleted");
var inventoryBaseUrl = (_configuration["Services:InventoryApiBaseUrl"] ?? "http://localhost:5003").TrimEnd('/');
var token = Request.Headers.Authorization.ToString();
var grantBody = JsonSerializer.Serialize(new
{
itemKey = gather.ResourceKey,
quantity = gather.QuantityGranted
});
var client = _httpClientFactory.CreateClient();
using var request = new HttpRequestMessage(
HttpMethod.Post,
$"{inventoryBaseUrl}/api/inventory/by-owner/character/{req.CharacterId}/grant");
request.Content = new StringContent(grantBody, Encoding.UTF8, "application/json");
if (!string.IsNullOrWhiteSpace(token))
request.Headers.Authorization = AuthenticationHeaderValue.Parse(token);
using var response = await client.SendAsync(request);
var responseBody = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
await _locations.RestoreGatheredResourceAsync(id, gather.ResourceKey, gather.QuantityGranted);
return StatusCode((int)response.StatusCode, responseBody);
}
return Ok(new GatherResourceResponse
{
LocationId = id,
CharacterId = req.CharacterId,
ResourceKey = gather.ResourceKey,
QuantityGranted = gather.QuantityGranted,
RemainingQuantity = gather.RemainingQuantity,
InventoryResponseJson = responseBody
});
}
}