namespace Groceries.Stores; using Groceries.Data; using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; [Route("/stores")] public class StoresController : Controller { private readonly AppDbContext dbContext; public StoresController(AppDbContext dbContext) { this.dbContext = dbContext; } [HttpGet] public IResult Index() { return new RazorComponentResult(); } [HttpGet("new")] public IResult NewStore() { return Request.IsTurboFrameRequest("modal") ? new RazorComponentResult() : new RazorComponentResult(); } [HttpPost("new")] public async Task NewStore(Guid retailerId, string name, string? address) { var store = new Store(retailerId, name, address); dbContext.Stores.Add(store); await dbContext.SaveChangesAsync(HttpContext.RequestAborted); return Request.IsTurboFrameRequest("modal") ? Results.NoContent() : Results.LocalRedirect("/stores?page=1"); } [HttpGet("edit/{id}")] public async Task EditStore(Guid id) { var store = await dbContext.Stores .SingleOrDefaultAsync(store => store.Id == id, HttpContext.RequestAborted); if (store == null) { return Results.NotFound(); } return Request.IsTurboFrameRequest("modal") ? new RazorComponentResult(new { Store = store }) : new RazorComponentResult(new { Store = store }); } [HttpPost("edit/{id}")] public async Task EditStore(Guid id, Guid retailerId, string name, string? address, string? returnUrl) { var store = new Store(id, retailerId, name, address); dbContext.Stores.Update(store); await dbContext.SaveChangesAsync(HttpContext.RequestAborted); return Request.IsTurboFrameRequest("modal") ? Results.NoContent() : Results.LocalRedirect($"/stores/edit/{id}"); } }