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

218 lines
9.7 KiB
C#
Raw Normal View History

2020-05-24 02:12:37 +01:00
using System.Linq;
2019-05-29 23:09:20 +01:00
using System.Threading.Tasks;
2020-02-10 02:20:48 +00:00
using Marechai.Areas.Admin.Models;
2020-02-10 22:44:18 +00:00
using Marechai.Database.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
{
2020-02-10 22:44:18 +00:00
[Area("Admin"), Authorize]
2019-05-29 23:09:20 +01:00
public class OwnedMachineController : Controller
{
2020-02-10 02:47:39 +00:00
readonly MarechaiContext _context;
2019-05-29 23:09:20 +01:00
2020-02-10 22:44:18 +00:00
public OwnedMachineController(MarechaiContext context) => _context = context;
2019-05-29 23:09:20 +01:00
// GET: OwnedMachine
public async Task<IActionResult> Index()
{
2020-02-10 22:44:18 +00:00
IQueryable<OwnedMachineViewModel> marechaiContext = _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
});
2020-02-10 02:47:39 +00:00
return View(await marechaiContext.ToListAsync());
2019-05-29 23:09:20 +01:00
}
// GET: OwnedMachine/Details/5
public async Task<IActionResult> Details(long? id)
{
2020-02-10 22:44:18 +00:00
if(id == null)
return NotFound();
2019-05-29 23:09:20 +01:00
2020-02-10 22:44:18 +00: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()
{
2020-02-10 22:44:18 +00: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).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.
2020-02-10 22:44:18 +00:00
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Create(
2020-02-10 22:44:18 +00:00
[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();
2020-02-10 22:44:18 +00:00
2019-05-29 23:09:20 +01:00
return RedirectToAction(nameof(Index));
}
2020-02-10 22:44:18 +00: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).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)
{
2020-02-10 22:44:18 +00:00
if(id == null)
return NotFound();
OwnedMachine ownedMachine = await _context.OwnedMachines.FindAsync(id);
2020-02-10 22:44:18 +00:00
if(ownedMachine == null)
return NotFound();
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.
2020-02-10 22:44:18 +00:00
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(
2020-02-10 22:44:18 +00:00
long id,
[Bind("AcquisitionDate,LostDate,Status,LastStatusDate,Trade,Boxed,Manuals,SerialNumber,SerialNumberVisible,MachineId,Id")]
OwnedMachine ownedMachine)
2019-05-29 23:09:20 +01:00
{
2020-02-10 22:44:18 +00: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
{
2020-02-10 22:44:18 +00: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));
}
2020-02-10 22:44:18 +00: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);
}
// GET: OwnedMachine/Delete/5
public async Task<IActionResult> Delete(long? id)
{
2020-02-10 22:44:18 +00:00
if(id == null)
return NotFound();
2019-05-29 23:09:20 +01:00
2020-02-10 22:44:18 +00: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
2020-02-10 22:44:18 +00:00
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
2019-05-29 23:09:20 +01:00
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();
2020-02-10 22:44:18 +00:00
2019-05-29 23:09:20 +01:00
return RedirectToAction(nameof(Index));
}
2020-02-10 22:44:18 +00:00
bool OwnedMachineExists(long id) => _context.OwnedMachines.Any(e => e.Id == id);
2019-05-29 23:09:20 +01:00
}
}