forked from wanderer/pwt-0x01-ng
159 lines
3.6 KiB
C#
159 lines
3.6 KiB
C#
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
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<IActionResult> Index()
|
|
{
|
|
return View(await _context.Order.ToListAsync());
|
|
}
|
|
|
|
// GET: Admin/Orders/Details/5
|
|
public async Task<IActionResult> Details(int? id)
|
|
{
|
|
if (id == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
var order = await _context.Order
|
|
.FirstOrDefaultAsync(m => m.id == id);
|
|
if (order == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
ViewData["User_id"] = new SelectList(_context.Users, "Id", "Id", order.User_id);
|
|
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<IActionResult> 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<IActionResult> Edit(int? id)
|
|
{
|
|
if (id == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
var order = await _context.Order.FindAsync(id);
|
|
if (order == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
ViewData["User_id"] = new SelectList(_context.Users, "Id", "Id", order.User_id);
|
|
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("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));
|
|
}
|
|
ViewData["User_id"] = new SelectList(_context.Users, "Id", "Id", order.User_id);
|
|
return View(order);
|
|
}
|
|
|
|
// GET: Admin/Orders/Delete/5
|
|
public async Task<IActionResult> 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<IActionResult> 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);
|
|
}
|
|
}
|
|
}
|