Files
marechai/Marechai/Areas/Admin/Controllers/OwnedMachineController.cs

216 lines
10 KiB
C#
Raw Normal View History

2019-05-29 23:09:20 +01:00
using System.Linq;
using System.Threading.Tasks;
2020-02-10 02:10:18 +00:00
using Marechai.Database.Models;
2020-02-10 02:20:48 +00:00
using Marechai.Areas.Admin.Models;
2019-05-29 23:09:20 +01:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
2020-02-10 02:20:48 +00:00
namespace Marechai.Areas.Admin.Controllers
2019-05-29 23:09:20 +01:00
{
[Area("Admin")]
[Authorize]
public class OwnedMachineController : Controller
{
readonly cicmContext _context;
2019-05-29 23:09:20 +01:00
public OwnedMachineController(cicmContext context)
{
_context = context;
}
// GET: OwnedMachine
public async Task<IActionResult> Index()
{
IQueryable<OwnedMachineViewModel> cicmContext = _context
.OwnedMachines.Include(o => o.Machine)
.OrderBy(o => o.Machine.Company.Name)
.ThenBy(o => o.Machine.Name).ThenBy(o => o.User.UserName)
.ThenBy(o => o.AcquisitionDate)
.Select(o => new OwnedMachineViewModel
{
AcquisitionDate = o.AcquisitionDate,
Id = o.Id,
Machine =
$"{o.Machine.Company.Name} {o.Machine.Name}",
Status = o.Status,
User = o.User.UserName
});
2019-05-29 23:09:20 +01:00
return View(await cicmContext.ToListAsync());
}
// GET: OwnedMachine/Details/5
public async Task<IActionResult> Details(long? id)
{
if(id == null) return NotFound();
2019-05-29 23:09:20 +01:00
OwnedMachineViewModel ownedMachine = await _context.OwnedMachines
.Include(o => o.Machine)
.Select(o => new OwnedMachineViewModel
{
AcquisitionDate = o.AcquisitionDate,
Boxed = o.Boxed,
LastStatusDate = o.LastStatusDate,
LostDate = o.LostDate,
Machine =
$"{o.Machine.Company.Name} {o.Machine.Name}",
Manuals = o.Manuals,
SerialNumber = o.SerialNumber,
SerialNumberVisible =
o.SerialNumberVisible,
Status = o.Status,
User = o.User.UserName,
Id = o.Id
}).FirstOrDefaultAsync(m => m.Id == id);
if(ownedMachine == null) return NotFound();
2019-05-29 23:09:20 +01:00
return View(ownedMachine);
}
// GET: OwnedMachine/Create
public IActionResult Create()
{
ViewData["MachineId"] =
new
SelectList(_context.Machines.OrderBy(m => m.Company.Name).ThenBy(m => m.Name).Select(m => new {m.Id, Name = $"{m.Company.Name} {m.Name}"}),
"Id", "Name");
ViewData["UserId"] =
new SelectList(_context.Users.OrderBy(u => u.UserName).Select(u => new {u.Id, u.UserName}), "Id",
"UserName");
2019-05-29 23:09:20 +01:00
return View();
}
// POST: OwnedMachine/Create
// To protect from overposting attacks, please 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(
"AcquisitionDate,LostDate,Status,LastStatusDate,Trade,Boxed,Manuals,SerialNumber,SerialNumberVisible,MachineId,UserId,Id")]
OwnedMachine ownedMachine)
2019-05-29 23:09:20 +01:00
{
if(ModelState.IsValid)
2019-05-29 23:09:20 +01:00
{
_context.Add(ownedMachine);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["MachineId"] =
new
SelectList(_context.Machines.OrderBy(m => m.Company.Name).ThenBy(m => m.Name).Select(m => new {m.Id, Name = $"{m.Company.Name} {m.Name}"}),
"Id", "Name");
ViewData["UserId"] =
new SelectList(_context.Users.OrderBy(u => u.UserName).Select(u => new {u.Id, u.UserName}), "Id",
"UserName");
2019-05-29 23:09:20 +01:00
return View(ownedMachine);
}
// GET: OwnedMachine/Edit/5
public async Task<IActionResult> Edit(long? id)
{
if(id == null) return NotFound();
OwnedMachine ownedMachine = await _context.OwnedMachines.FindAsync(id);
if(ownedMachine == null) return NotFound();
2019-05-29 23:09:20 +01:00
ViewData["MachineId"] =
new
SelectList(_context.Machines.OrderBy(m => m.Company.Name).ThenBy(m => m.Name).Select(m => new {m.Id, Name = $"{m.Company.Name} {m.Name}"}),
"Id", "Name");
ViewData["UserId"] =
new
SelectList(_context.Users.OrderBy(u => u.UserName).Where(u => u.Id == ownedMachine.UserId).Select(u => new {u.Id, u.UserName}),
"Id", "UserName");
2019-05-29 23:09:20 +01:00
return View(ownedMachine);
}
// POST: OwnedMachine/Edit/5
// To protect from overposting attacks, please 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(
long id, [Bind(
"AcquisitionDate,LostDate,Status,LastStatusDate,Trade,Boxed,Manuals,SerialNumber,SerialNumberVisible,MachineId,Id")]
OwnedMachine ownedMachine)
2019-05-29 23:09:20 +01:00
{
if(id != ownedMachine.Id) return NotFound();
2019-05-29 23:09:20 +01:00
if(ModelState.IsValid)
2019-05-29 23:09:20 +01:00
{
try
{
_context.Update(ownedMachine);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
2019-05-29 23:09:20 +01:00
{
if(!OwnedMachineExists(ownedMachine.Id)) return NotFound();
throw;
2019-05-29 23:09:20 +01:00
}
2019-05-29 23:09:20 +01:00
return RedirectToAction(nameof(Index));
}
ViewData["MachineId"] =
new
SelectList(_context.Machines.OrderBy(m => m.Company.Name).ThenBy(m => m.Name).Select(m => new {m.Id, Name = $"{m.Company.Name} {m.Name}"}),
"Id", "Name");
ViewData["UserId"] =
new
SelectList(_context.Users.OrderBy(u => u.UserName).Where(u => u.Id == ownedMachine.UserId).Select(u => new {u.Id, u.UserName}),
"Id", "UserName");
2019-05-29 23:09:20 +01:00
return View(ownedMachine);
}
// GET: OwnedMachine/Delete/5
public async Task<IActionResult> Delete(long? id)
{
if(id == null) return NotFound();
2019-05-29 23:09:20 +01:00
OwnedMachineViewModel ownedMachine = await _context.OwnedMachines
.Include(o => o.Machine)
.Select(o => new OwnedMachineViewModel
{
AcquisitionDate = o.AcquisitionDate,
Boxed = o.Boxed,
LastStatusDate = o.LastStatusDate,
LostDate = o.LostDate,
Machine =
$"{o.Machine.Company.Name} {o.Machine.Name}",
Manuals = o.Manuals,
SerialNumber = o.SerialNumber,
SerialNumberVisible =
o.SerialNumberVisible,
Status = o.Status,
User = o.User.UserName,
Id = o.Id
}).FirstOrDefaultAsync(m => m.Id == id);
if(ownedMachine == null) return NotFound();
2019-05-29 23:09:20 +01:00
return View(ownedMachine);
}
// POST: OwnedMachine/Delete/5
[HttpPost]
[ActionName("Delete")]
2019-05-29 23:09:20 +01:00
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(long id)
{
OwnedMachine ownedMachine = await _context.OwnedMachines.FindAsync(id);
2019-05-29 23:09:20 +01:00
_context.OwnedMachines.Remove(ownedMachine);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
bool OwnedMachineExists(long id)
2019-05-29 23:09:20 +01:00
{
return _context.OwnedMachines.Any(e => e.Id == id);
}
}
}