using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using WebVentaCoche.DataBase; using WebVentaCoche.Models; namespace WebVentaCoche.Controllers { [Authorize] // Requiere autenticación para acceder al controlador public class OrderController : Controller { private readonly ApplicationDbContext _context; public OrderController(ApplicationDbContext context) { _context = context; } //GET:Order/Index public async Task Index() { var orders = await _context.Orders .Include(o => o.User) // Cargar la relación con el usuario .ToListAsync(); return View(orders); } //GET:Order/Details/5 public async Task Details(int? id) { if (id == null) { return NotFound(); } var order = await _context.Orders .Include(o => o.User) // Cargar la relación con el usuario .FirstOrDefaultAsync(o => o.Id == id); if (order == null) { return NotFound(); } return View(order); } //GET:Order/Edit/{id} public async Task Edit(int? id) { if (id == null) { return NotFound(); } var order = await _context.Orders.FindAsync(id); if (order == null) { return NotFound(); } return View(order); } //POST:Order/Edit/{id} [HttpPost] [ValidateAntiForgeryToken] public async Task Edit(int id, Order order) { if (id != order.Id) { return NotFound(); } if (ModelState.IsValid) { try { _context.Update(order); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!OrderExists(order.Id)) { return NotFound(); } else { throw; } } return RedirectToAction(nameof(Index)); } return View(order); } private bool OrderExists(int id) { return _context.Orders.Any(e => e.Id == id); } } }