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

156 lines
3.3 KiB
C#
Raw Normal View History

merge: feature-admin-views-spaces-to-tabs commit 43ceea436cf04ed259d5e094e3e003da630d3dce Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:57:48 2020 +0100 chore: spaces to tabs in admin views commit 0bc3345368826a6c6edae974ca4cb69f32134553 Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:41:12 2020 +0100 chore: always update the updated timestamp * instruct db to update the updated timestamp both on create and edit, fixing a little bug that was preventing creation of items of any kind (since this behaviour has been used throughout the entity configs) due to the updated timestamp not being set (while db knows it's a not-null) commit 34664fc9f3876605001b4c5294d3350a136f2997 Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:18:05 2020 +0100 chore: edit the scaffolded views to use our layout commit 6816006c09b5a3d79c489e3c27d7081b83e6195e Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:14:17 2020 +0100 chore: update Makefile "run" target+added targets * pgdba - start a dev db container in background * pgdbz - stop the dev db image * docker run db img args are configured via variables as seen fit * run is now more of a "watch run" commit f1d87345ac2d7d9ce854a6c8bb37731ff0f8805c Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:12:41 2020 +0100 chore: add {OrderItem,Order} links to navbar commit c214cf4fe6625ad312275432a8883a01e37f9cf0 Author: surtur <a_mirre@utb.cz> Date: Wed Dec 30 13:37:34 2020 +0100 chore: add netcore-scaffolded controllers+views * Templates folder of the vs.web.codegenerators pkg (v3.1.4) had to be temporarily copied to the project directory for the scaffolding to work; unnecessary template folders had to be removed prior to running scaffold command * restored pkgs and clean built, then scaffolded using (var set for trace output of the command): make restore clean build; codegen_trace=1 dotnet-aspnet-codegenerator controller -p "." -actions --force -name OrdersController -dc pwt_0x01_ng.Models.Database.DBContext -async -m pwt_0x01_ng.Models.Order -namespace pwt_0x01_ng.Areas.Admin.Controllers -outDir Areas/Admin/Controllers --no-build make restore clean build; codegen_trace=1 dotnet-aspnet-codegenerator controller -p "." -actions --force -name OrderItemController -dc pwt_0x01_ng.Models.Database.DBContext -async -m pwt_0x01_ng.Models.OrderItem -namespace pwt_0x01_ng.Areas.Admin.Controllers -outDir Areas/Admin/Controllers --no-build
2020-12-31 02:11:14 +01:00
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authorization;
merge: feature-admin-views-spaces-to-tabs commit 43ceea436cf04ed259d5e094e3e003da630d3dce Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:57:48 2020 +0100 chore: spaces to tabs in admin views commit 0bc3345368826a6c6edae974ca4cb69f32134553 Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:41:12 2020 +0100 chore: always update the updated timestamp * instruct db to update the updated timestamp both on create and edit, fixing a little bug that was preventing creation of items of any kind (since this behaviour has been used throughout the entity configs) due to the updated timestamp not being set (while db knows it's a not-null) commit 34664fc9f3876605001b4c5294d3350a136f2997 Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:18:05 2020 +0100 chore: edit the scaffolded views to use our layout commit 6816006c09b5a3d79c489e3c27d7081b83e6195e Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:14:17 2020 +0100 chore: update Makefile "run" target+added targets * pgdba - start a dev db container in background * pgdbz - stop the dev db image * docker run db img args are configured via variables as seen fit * run is now more of a "watch run" commit f1d87345ac2d7d9ce854a6c8bb37731ff0f8805c Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:12:41 2020 +0100 chore: add {OrderItem,Order} links to navbar commit c214cf4fe6625ad312275432a8883a01e37f9cf0 Author: surtur <a_mirre@utb.cz> Date: Wed Dec 30 13:37:34 2020 +0100 chore: add netcore-scaffolded controllers+views * Templates folder of the vs.web.codegenerators pkg (v3.1.4) had to be temporarily copied to the project directory for the scaffolding to work; unnecessary template folders had to be removed prior to running scaffold command * restored pkgs and clean built, then scaffolded using (var set for trace output of the command): make restore clean build; codegen_trace=1 dotnet-aspnet-codegenerator controller -p "." -actions --force -name OrdersController -dc pwt_0x01_ng.Models.Database.DBContext -async -m pwt_0x01_ng.Models.Order -namespace pwt_0x01_ng.Areas.Admin.Controllers -outDir Areas/Admin/Controllers --no-build make restore clean build; codegen_trace=1 dotnet-aspnet-codegenerator controller -p "." -actions --force -name OrderItemController -dc pwt_0x01_ng.Models.Database.DBContext -async -m pwt_0x01_ng.Models.OrderItem -namespace pwt_0x01_ng.Areas.Admin.Controllers -outDir Areas/Admin/Controllers --no-build
2020-12-31 02:11:14 +01:00
using pwt_0x01_ng.Models;
using pwt_0x01_ng.Models.Database;
using pwt_0x01_ng.Models.Identity;
merge: feature-admin-views-spaces-to-tabs commit 43ceea436cf04ed259d5e094e3e003da630d3dce Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:57:48 2020 +0100 chore: spaces to tabs in admin views commit 0bc3345368826a6c6edae974ca4cb69f32134553 Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:41:12 2020 +0100 chore: always update the updated timestamp * instruct db to update the updated timestamp both on create and edit, fixing a little bug that was preventing creation of items of any kind (since this behaviour has been used throughout the entity configs) due to the updated timestamp not being set (while db knows it's a not-null) commit 34664fc9f3876605001b4c5294d3350a136f2997 Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:18:05 2020 +0100 chore: edit the scaffolded views to use our layout commit 6816006c09b5a3d79c489e3c27d7081b83e6195e Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:14:17 2020 +0100 chore: update Makefile "run" target+added targets * pgdba - start a dev db container in background * pgdbz - stop the dev db image * docker run db img args are configured via variables as seen fit * run is now more of a "watch run" commit f1d87345ac2d7d9ce854a6c8bb37731ff0f8805c Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:12:41 2020 +0100 chore: add {OrderItem,Order} links to navbar commit c214cf4fe6625ad312275432a8883a01e37f9cf0 Author: surtur <a_mirre@utb.cz> Date: Wed Dec 30 13:37:34 2020 +0100 chore: add netcore-scaffolded controllers+views * Templates folder of the vs.web.codegenerators pkg (v3.1.4) had to be temporarily copied to the project directory for the scaffolding to work; unnecessary template folders had to be removed prior to running scaffold command * restored pkgs and clean built, then scaffolded using (var set for trace output of the command): make restore clean build; codegen_trace=1 dotnet-aspnet-codegenerator controller -p "." -actions --force -name OrdersController -dc pwt_0x01_ng.Models.Database.DBContext -async -m pwt_0x01_ng.Models.Order -namespace pwt_0x01_ng.Areas.Admin.Controllers -outDir Areas/Admin/Controllers --no-build make restore clean build; codegen_trace=1 dotnet-aspnet-codegenerator controller -p "." -actions --force -name OrderItemController -dc pwt_0x01_ng.Models.Database.DBContext -async -m pwt_0x01_ng.Models.OrderItem -namespace pwt_0x01_ng.Areas.Admin.Controllers -outDir Areas/Admin/Controllers --no-build
2020-12-31 02:11:14 +01:00
namespace pwt_0x01_ng.Areas.Admin.Controllers
{
[Area("Admin")]
[Authorize(Roles = nameof(Roles.Admin) + "," + nameof(Roles.Manager))]
merge: feature-admin-views-spaces-to-tabs commit 43ceea436cf04ed259d5e094e3e003da630d3dce Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:57:48 2020 +0100 chore: spaces to tabs in admin views commit 0bc3345368826a6c6edae974ca4cb69f32134553 Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:41:12 2020 +0100 chore: always update the updated timestamp * instruct db to update the updated timestamp both on create and edit, fixing a little bug that was preventing creation of items of any kind (since this behaviour has been used throughout the entity configs) due to the updated timestamp not being set (while db knows it's a not-null) commit 34664fc9f3876605001b4c5294d3350a136f2997 Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:18:05 2020 +0100 chore: edit the scaffolded views to use our layout commit 6816006c09b5a3d79c489e3c27d7081b83e6195e Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:14:17 2020 +0100 chore: update Makefile "run" target+added targets * pgdba - start a dev db container in background * pgdbz - stop the dev db image * docker run db img args are configured via variables as seen fit * run is now more of a "watch run" commit f1d87345ac2d7d9ce854a6c8bb37731ff0f8805c Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:12:41 2020 +0100 chore: add {OrderItem,Order} links to navbar commit c214cf4fe6625ad312275432a8883a01e37f9cf0 Author: surtur <a_mirre@utb.cz> Date: Wed Dec 30 13:37:34 2020 +0100 chore: add netcore-scaffolded controllers+views * Templates folder of the vs.web.codegenerators pkg (v3.1.4) had to be temporarily copied to the project directory for the scaffolding to work; unnecessary template folders had to be removed prior to running scaffold command * restored pkgs and clean built, then scaffolded using (var set for trace output of the command): make restore clean build; codegen_trace=1 dotnet-aspnet-codegenerator controller -p "." -actions --force -name OrdersController -dc pwt_0x01_ng.Models.Database.DBContext -async -m pwt_0x01_ng.Models.Order -namespace pwt_0x01_ng.Areas.Admin.Controllers -outDir Areas/Admin/Controllers --no-build make restore clean build; codegen_trace=1 dotnet-aspnet-codegenerator controller -p "." -actions --force -name OrderItemController -dc pwt_0x01_ng.Models.Database.DBContext -async -m pwt_0x01_ng.Models.OrderItem -namespace pwt_0x01_ng.Areas.Admin.Controllers -outDir Areas/Admin/Controllers --no-build
2020-12-31 02:11:14 +01:00
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)
merge: feature-admin-views-spaces-to-tabs commit 43ceea436cf04ed259d5e094e3e003da630d3dce Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:57:48 2020 +0100 chore: spaces to tabs in admin views commit 0bc3345368826a6c6edae974ca4cb69f32134553 Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:41:12 2020 +0100 chore: always update the updated timestamp * instruct db to update the updated timestamp both on create and edit, fixing a little bug that was preventing creation of items of any kind (since this behaviour has been used throughout the entity configs) due to the updated timestamp not being set (while db knows it's a not-null) commit 34664fc9f3876605001b4c5294d3350a136f2997 Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:18:05 2020 +0100 chore: edit the scaffolded views to use our layout commit 6816006c09b5a3d79c489e3c27d7081b83e6195e Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:14:17 2020 +0100 chore: update Makefile "run" target+added targets * pgdba - start a dev db container in background * pgdbz - stop the dev db image * docker run db img args are configured via variables as seen fit * run is now more of a "watch run" commit f1d87345ac2d7d9ce854a6c8bb37731ff0f8805c Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:12:41 2020 +0100 chore: add {OrderItem,Order} links to navbar commit c214cf4fe6625ad312275432a8883a01e37f9cf0 Author: surtur <a_mirre@utb.cz> Date: Wed Dec 30 13:37:34 2020 +0100 chore: add netcore-scaffolded controllers+views * Templates folder of the vs.web.codegenerators pkg (v3.1.4) had to be temporarily copied to the project directory for the scaffolding to work; unnecessary template folders had to be removed prior to running scaffold command * restored pkgs and clean built, then scaffolded using (var set for trace output of the command): make restore clean build; codegen_trace=1 dotnet-aspnet-codegenerator controller -p "." -actions --force -name OrdersController -dc pwt_0x01_ng.Models.Database.DBContext -async -m pwt_0x01_ng.Models.Order -namespace pwt_0x01_ng.Areas.Admin.Controllers -outDir Areas/Admin/Controllers --no-build make restore clean build; codegen_trace=1 dotnet-aspnet-codegenerator controller -p "." -actions --force -name OrderItemController -dc pwt_0x01_ng.Models.Database.DBContext -async -m pwt_0x01_ng.Models.OrderItem -namespace pwt_0x01_ng.Areas.Admin.Controllers -outDir Areas/Admin/Controllers --no-build
2020-12-31 02:11:14 +01:00
{
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)
merge: feature-admin-views-spaces-to-tabs commit 43ceea436cf04ed259d5e094e3e003da630d3dce Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:57:48 2020 +0100 chore: spaces to tabs in admin views commit 0bc3345368826a6c6edae974ca4cb69f32134553 Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:41:12 2020 +0100 chore: always update the updated timestamp * instruct db to update the updated timestamp both on create and edit, fixing a little bug that was preventing creation of items of any kind (since this behaviour has been used throughout the entity configs) due to the updated timestamp not being set (while db knows it's a not-null) commit 34664fc9f3876605001b4c5294d3350a136f2997 Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:18:05 2020 +0100 chore: edit the scaffolded views to use our layout commit 6816006c09b5a3d79c489e3c27d7081b83e6195e Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:14:17 2020 +0100 chore: update Makefile "run" target+added targets * pgdba - start a dev db container in background * pgdbz - stop the dev db image * docker run db img args are configured via variables as seen fit * run is now more of a "watch run" commit f1d87345ac2d7d9ce854a6c8bb37731ff0f8805c Author: surtur <a_mirre@utb.cz> Date: Thu Dec 31 01:12:41 2020 +0100 chore: add {OrderItem,Order} links to navbar commit c214cf4fe6625ad312275432a8883a01e37f9cf0 Author: surtur <a_mirre@utb.cz> Date: Wed Dec 30 13:37:34 2020 +0100 chore: add netcore-scaffolded controllers+views * Templates folder of the vs.web.codegenerators pkg (v3.1.4) had to be temporarily copied to the project directory for the scaffolding to work; unnecessary template folders had to be removed prior to running scaffold command * restored pkgs and clean built, then scaffolded using (var set for trace output of the command): make restore clean build; codegen_trace=1 dotnet-aspnet-codegenerator controller -p "." -actions --force -name OrdersController -dc pwt_0x01_ng.Models.Database.DBContext -async -m pwt_0x01_ng.Models.Order -namespace pwt_0x01_ng.Areas.Admin.Controllers -outDir Areas/Admin/Controllers --no-build make restore clean build; codegen_trace=1 dotnet-aspnet-codegenerator controller -p "." -actions --force -name OrderItemController -dc pwt_0x01_ng.Models.Database.DBContext -async -m pwt_0x01_ng.Models.OrderItem -namespace pwt_0x01_ng.Areas.Admin.Controllers -outDir Areas/Admin/Controllers --no-build
2020-12-31 02:11:14 +01:00
{
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);
}
}
}