using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Authorization; using pwt_0x01_ng.Models; using pwt_0x01_ng.Models.Database; using pwt_0x01_ng.Models.Identity; namespace pwt_0x01_ng.Areas.Admin.Controllers { [Area("Admin")] [Authorize(Roles = nameof(Roles.Admin) + "," + nameof(Roles.Manager))] public class OrdersController : Controller { private readonly DBContext _context; public OrdersController(DBContext context) { _context = context; } // GET: Admin/Orders public async Task Index() { return View(await _context.Order.ToListAsync()); } // GET: Admin/Orders/Details/5 public async Task Details(int? id) { if (id == null) { return NotFound(); } var order = await _context.Order .FirstOrDefaultAsync(m => m.id == id); if (order == null) { return NotFound(); } return View(order); } // GET: Admin/Orders/Create public IActionResult Create() { return View(); } // POST: Admin/Orders/Create // To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public async Task Create([Bind("Order_Number,id,User_id,Price_total,Created,Updated")] Order order) { if (ModelState.IsValid) { _context.Add(order); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } return View(order); } // GET: Admin/Orders/Edit/5 public async Task Edit(int? id) { if (id == null) { return NotFound(); } var order = await _context.Order.FindAsync(id); if (order == null) { return NotFound(); } return View(order); } // POST: Admin/Orders/Edit/5 // To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public async Task Edit(int id, [Bind("Order_Number,id,User_id,Price_total,new.Created,new.Updated")] 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); } // GET: Admin/Orders/Delete/5 public async Task Delete(int? id) { if (id == null) { return NotFound(); } var order = await _context.Order .FirstOrDefaultAsync(m => m.id == id); if (order == null) { return NotFound(); } return View(order); } // POST: Admin/Orders/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public async Task DeleteConfirmed(int id) { var order = await _context.Order.FindAsync(id); _context.Order.Remove(order); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } private bool OrderExists(int id) { return _context.Order.Any(e => e.id == id); } } }