Files
Inboxintel/src/InboxIntel.Api/Controllers/WidgetLayoutController.cs
T
cesnimda 81fc05299d chore: fix pre-existing dotnet-format whitespace drift
Purely mechanical (object-initializer line-splitting per the project's format
rules) — no behavior change. This drift on develop was blocking the pre-commit
hook's solution-wide 'dotnet format --verify-no-changes' check for unrelated
commits. Verified: diff is cosmetic only (spot-checked), build + all 39 tests
still pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-01 21:02:32 +02:00

61 lines
2.2 KiB
C#

using InboxIntel.Application.Abstractions;
using InboxIntel.Domain.Entities;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace InboxIntel.Api.Controllers;
public record WidgetLayoutDto(string WidgetKey, int X, int Y, int W, int H, bool Visible, int SortOrder, string? SettingsJson);
/// <summary>Persists the user's draggable/resizable dashboard layout.</summary>
public class WidgetLayoutController : ApiControllerBase
{
private readonly IAppDbContext _db;
public WidgetLayoutController(IAppDbContext db) => _db = db;
[HttpGet]
public async Task<IActionResult> Get(CancellationToken ct)
{
var layouts = await _db.WidgetLayouts
.Where(w => w.UserId == UserId)
.OrderBy(w => w.SortOrder)
.Select(w => new WidgetLayoutDto(w.WidgetKey, w.X, w.Y, w.W, w.H, w.Visible, w.SortOrder, w.SettingsJson))
.ToListAsync(ct);
return Ok(layouts);
}
/// <summary>Upserts the full layout for the user (replace semantics).</summary>
[HttpPut]
public async Task<IActionResult> Save([FromBody] List<WidgetLayoutDto> layout, CancellationToken ct)
{
var existing = await _db.WidgetLayouts.Where(w => w.UserId == UserId).ToListAsync(ct);
var byKey = existing.ToDictionary(w => w.WidgetKey);
foreach (var dto in layout)
{
if (byKey.TryGetValue(dto.WidgetKey, out var w))
{
w.X = dto.X; w.Y = dto.Y; w.W = dto.W; w.H = dto.H;
w.Visible = dto.Visible; w.SortOrder = dto.SortOrder; w.SettingsJson = dto.SettingsJson;
}
else
{
_db.WidgetLayouts.Add(new WidgetLayout
{
UserId = UserId,
WidgetKey = dto.WidgetKey,
X = dto.X,
Y = dto.Y,
W = dto.W,
H = dto.H,
Visible = dto.Visible,
SortOrder = dto.SortOrder,
SettingsJson = dto.SettingsJson
});
}
}
await _db.SaveChangesAsync(ct);
return NoContent();
}
}