Files
mealplanner/backend/Controllers/MealPlanController.cs
Claude 1885abee64 fix: align frontend API calls with backend routes and types
- Backend: rename routes /mealplan→/mealplans, /shoppinglist→/shopping
- Backend: simplify swap/reroll to entry-centric endpoints (by entryId)
- Frontend: fix all interfaces to use string GUIDs instead of numbers
- Frontend: fix field names (weekStart, date, totalAmount) to match backend JSON
- Frontend: shopping toggle by itemName instead of non-existent id
- Frontend: handle 204 No Content on DELETE responses
- Docker-compose: use env vars for DB credentials

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 06:49:41 +00:00

82 lines
2.5 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MealPlanner.Services;
namespace MealPlanner.Controllers;
[ApiController]
[Route("api/mealplans")]
[Authorize]
public class MealPlanController(MealPlanService mealPlanService) : ControllerBase
{
private string UserId => User.FindFirst("sub")?.Value ?? throw new UnauthorizedAccessException();
public record GenerateRequest(string? WeekStart);
public record SwapRequest(Guid RecipeId);
[HttpPost("generate")]
public async Task<IActionResult> Generate([FromBody] GenerateRequest? body)
{
DateOnly weekStart;
if (body?.WeekStart is not null && DateOnly.TryParse(body.WeekStart, out var parsed))
{
weekStart = MealPlanService.GetWeekStart(parsed);
}
else
{
weekStart = MealPlanService.GetWeekStart(DateOnly.FromDateTime(DateTime.UtcNow));
}
var plan = await mealPlanService.GenerateWeekPlanAsync(UserId, weekStart);
return Ok(plan);
}
[HttpGet("current")]
public async Task<IActionResult> GetCurrent()
{
var plan = await mealPlanService.GetCurrentPlanAsync(UserId);
if (plan is null) return NotFound();
return Ok(plan);
}
[HttpGet("{weekStart}")]
public async Task<IActionResult> GetByWeek(string weekStart)
{
if (!DateOnly.TryParse(weekStart, out var date)) return BadRequest("Invalid date format.");
var plan = await mealPlanService.GetPlanAsync(UserId, date);
if (plan is null) return NotFound();
return Ok(plan);
}
[HttpPut("entries/{entryId:guid}/swap")]
public async Task<IActionResult> SwapEntry(Guid entryId, [FromBody] SwapRequest body)
{
try
{
var updated = await mealPlanService.SwapEntryAsync(entryId, body.RecipeId, UserId);
if (updated is null) return NotFound();
return Ok(updated);
}
catch (UnauthorizedAccessException)
{
return Forbid();
}
}
[HttpPost("entries/{entryId:guid}/reroll")]
public async Task<IActionResult> RerollEntry(Guid entryId)
{
try
{
var updated = await mealPlanService.RerollEntryAsync(entryId, UserId);
if (updated is null) return StatusCode(503, "Could not fetch a new recipe. Try again.");
return Ok(updated);
}
catch (UnauthorizedAccessException)
{
return Forbid();
}
}
}