UTB.Eshop_2021/UTB.Eshop.Web/Areas/Admin/Controllers/OrdersController.cs

165 lines
5.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using UTB.Eshop.Web.Models.Database;
using UTB.Eshop.Web.Models.Entity;
using UTB.Eshop.Web.Models.Identity;
namespace UTB.Eshop.Web.Areas.Admin.Controllers
{
[Area("Admin")]
[Authorize(Roles = nameof(Roles.Admin) + ", " + nameof(Roles.Manager))]
public class OrdersController : Controller
{
private readonly EshopDbContext _context;
public OrdersController(EshopDbContext context)
{
_context = context;
}
// GET: Admin/Orders
public async Task<IActionResult> Index()
{
var eshopDbContext = _context.Orders.Include(o => o.User);
return View(await eshopDbContext.ToListAsync());
}
// GET: Admin/Orders/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var order = await _context.Orders
.Include(o => o.User)
.FirstOrDefaultAsync(m => m.ID == id);
if (order == null)
{
return NotFound();
}
return View(order);
}
// GET: Admin/Orders/Create
public IActionResult Create()
{
ViewData["UserId"] = new SelectList(_context.Users, "Id", "Id");
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<IActionResult> Create([Bind("ID,DateTimeCreated,OrderNumber,TotalPrice,UserId")] Order order)
{
if (ModelState.IsValid)
{
_context.Add(order);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["UserId"] = new SelectList(_context.Users, "Id", "Id", order.UserId);
return View(order);
}
// GET: Admin/Orders/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var order = await _context.Orders.FindAsync(id);
if (order == null)
{
return NotFound();
}
ViewData["UserId"] = new SelectList(_context.Users, "Id", "Id", order.UserId);
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<IActionResult> Edit(int id, [Bind("ID,DateTimeCreated,OrderNumber,TotalPrice,UserId")] 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));
}
ViewData["UserId"] = new SelectList(_context.Users, "Id", "Id", order.UserId);
return View(order);
}
// GET: Admin/Orders/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var order = await _context.Orders
.Include(o => o.User)
.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<IActionResult> DeleteConfirmed(int id)
{
var order = await _context.Orders.FindAsync(id);
_context.Orders.Remove(order);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool OrderExists(int id)
{
return _context.Orders.Any(e => e.ID == id);
}
}
}