This repository has been archived on 2023-10-28. You can view files and clone it, but cannot push or open issues or pull requests.
pwt-0x01-ng/Areas/Admin/Controllers/OrdersController.cs
surtur 9bed70352e
All checks were successful
continuous-integration/drone/push Build is passing
{User_id,Price_total} in Order {controller,views}
2021-02-10 11:38:55 +01:00

156 lines
3.3 KiB
C#

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<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();
}
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();
}
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));
}
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);
}
}
}