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

220 lines
9.7 KiB
C#
Raw Normal View History

2020-05-24 02:12:37 +01:00
using System.Linq;
2019-06-01 22:59:18 +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-06-01 22:59:18 +01:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
2020-02-10 02:20:48 +00:00
namespace Marechai.Areas.Admin.Controllers
2019-06-01 22:59:18 +01:00
{
2020-02-10 22:44:18 +00:00
[Area("Admin"), Authorize]
2019-06-01 22:59:18 +01:00
public class ScreensByMachineController : Controller
{
2020-02-10 02:47:39 +00:00
readonly MarechaiContext _context;
2019-06-01 22:59:18 +01:00
2020-02-10 22:44:18 +00:00
public ScreensByMachineController(MarechaiContext context) => _context = context;
2019-06-01 22:59:18 +01:00
// GET: ScreensByMachine
public async Task<IActionResult> Index()
{
2020-02-10 02:47:39 +00:00
IIncludableQueryable<ScreensByMachine, Screen> marechaiContext =
2019-06-01 22:59:18 +01:00
_context.ScreensByMachine.Include(s => s.Machine).Include(s => s.Screen);
2020-02-10 22:44:18 +00:00
2020-02-10 02:47:39 +00:00
return View(await marechaiContext.Select(s => new ScreensByMachineViewModel
{
Id = s.Id,
Screen = s.Screen.NativeResolution != null
? $"{s.Screen.Diagonal}\" {s.Screen.Type} with {s.Screen.NativeResolution}"
: $"{s.Screen.Diagonal}\" {s.Screen}",
Machine = $"{s.Machine.Company.Name} {s.Machine.Name}"
}).OrderBy(s => s.Machine).ThenBy(s => s.Screen).ToListAsync());
2019-06-01 22:59:18 +01:00
}
// GET: ScreensByMachine/Details/5
public async Task<IActionResult> Details(long? id)
{
2020-02-10 22:44:18 +00:00
if(id == null)
return NotFound();
ScreensByMachineViewModel screensByMachine =
await _context.ScreensByMachine.Include(s => s.Machine).Include(s => s.Screen).
Select(s => new ScreensByMachineViewModel
{
Id = s.Id,
Screen = s.Screen.NativeResolution != null
? $"{s.Screen.Diagonal}\" {s.Screen.Type} with {s.Screen.NativeResolution}"
: $"{s.Screen.Diagonal}\" {s.Screen}",
Machine = $"{s.Machine.Company.Name} {s.Machine.Name}"
}).FirstOrDefaultAsync(m => m.Id == id);
if(screensByMachine == null)
return NotFound();
2019-06-01 22:59:18 +01:00
return View(screensByMachine);
}
// GET: ScreensByMachine/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["ScreenId"] = new SelectList(_context.Screens.Select(s => new
{
s.Id,
Name = s.NativeResolution != null ? $"{s.Diagonal}\" {s.Type} with {s.NativeResolution}"
: $"{s.Diagonal}\" {s.Type}"
}).OrderBy(s => s.Name), "Id", "Name");
2019-06-01 22:59:18 +01:00
return View();
}
// POST: ScreensByMachine/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]
2019-06-01 22:59:18 +01:00
public async Task<IActionResult> Create([Bind("ScreenId,MachineId,Id")] ScreensByMachine screensByMachine)
{
if(ModelState.IsValid)
{
_context.Add(screensByMachine);
await _context.SaveChangesAsync();
2020-02-10 22:44:18 +00:00
2019-06-01 22:59:18 +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", screensByMachine.MachineId);
ViewData["ScreenId"] = new SelectList(_context.Screens.Select(s => new
{
s.Id,
Name = s.NativeResolution != null ? $"{s.Diagonal}\" {s.Type} with {s.NativeResolution}"
: $"{s.Diagonal}\" {s.Type}"
}).OrderBy(s => s.Name), "Id", "Name", screensByMachine.ScreenId);
2019-06-01 22:59:18 +01:00
return View(screensByMachine);
}
// GET: ScreensByMachine/Edit/5
public async Task<IActionResult> Edit(long? id)
{
2020-02-10 22:44:18 +00:00
if(id == null)
return NotFound();
2019-06-01 22:59:18 +01:00
ScreensByMachine screensByMachine = await _context.ScreensByMachine.FindAsync(id);
2020-02-10 22:44:18 +00:00
if(screensByMachine == 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", screensByMachine.MachineId);
ViewData["ScreenId"] = new SelectList(_context.Screens.Select(s => new
{
s.Id,
Name = s.NativeResolution != null ? $"{s.Diagonal}\" {s.Type} with {s.NativeResolution}"
: $"{s.Diagonal}\" {s.Type}"
}).OrderBy(s => s.Name), "Id", "Name", screensByMachine.ScreenId);
2019-06-01 22:59:18 +01:00
return View(screensByMachine);
}
// POST: ScreensByMachine/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]
2019-06-01 22:59:18 +01:00
public async Task<IActionResult> Edit(
long id, [Bind("ScreenId,MachineId,Id")] ScreensByMachine screensByMachine)
{
2020-02-10 22:44:18 +00:00
if(id != screensByMachine.Id)
return NotFound();
2019-06-01 22:59:18 +01:00
if(ModelState.IsValid)
{
try
{
_context.Update(screensByMachine);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
2020-02-10 22:44:18 +00:00
if(!ScreensByMachineExists(screensByMachine.Id))
return NotFound();
2019-06-01 22:59:18 +01:00
throw;
}
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", screensByMachine.MachineId);
ViewData["ScreenId"] = new SelectList(_context.Screens.Select(s => new
{
s.Id,
Name = s.NativeResolution != null ? $"{s.Diagonal}\" {s.Type} with {s.NativeResolution}"
: $"{s.Diagonal}\" {s.Type}"
}).OrderBy(s => s.Name), "Id", "Name", screensByMachine.ScreenId);
2019-06-01 22:59:18 +01:00
return View(screensByMachine);
}
// GET: ScreensByMachine/Delete/5
public async Task<IActionResult> Delete(long? id)
{
2020-02-10 22:44:18 +00:00
if(id == null)
return NotFound();
ScreensByMachineViewModel screensByMachine =
await _context.ScreensByMachine.Include(s => s.Machine).Include(s => s.Screen).
Select(s => new ScreensByMachineViewModel
{
Id = s.Id,
Screen = s.Screen.NativeResolution != null
? $"{s.Screen.Diagonal}\" {s.Screen.Type} with {s.Screen.NativeResolution}"
: $"{s.Screen.Diagonal}\" {s.Screen}",
Machine = $"{s.Machine.Company.Name} {s.Machine.Name}"
}).FirstOrDefaultAsync(m => m.Id == id);
if(screensByMachine == null)
return NotFound();
2019-06-01 22:59:18 +01:00
return View(screensByMachine);
}
// POST: ScreensByMachine/Delete/5
2020-02-10 22:44:18 +00:00
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
2019-06-01 22:59:18 +01:00
public async Task<IActionResult> DeleteConfirmed(long id)
{
ScreensByMachine screensByMachine = await _context.ScreensByMachine.FindAsync(id);
_context.ScreensByMachine.Remove(screensByMachine);
await _context.SaveChangesAsync();
2020-02-10 22:44:18 +00:00
2019-06-01 22:59:18 +01:00
return RedirectToAction(nameof(Index));
}
2020-02-10 22:44:18 +00:00
bool ScreensByMachineExists(long id) => _context.ScreensByMachine.Any(e => e.Id == id);
[AcceptVerbs("Get", "Post")]
2020-02-10 22:44:18 +00:00
public async Task<IActionResult> VerifyUnique(int screenId, int machineId) =>
await _context.ScreensByMachine.FirstOrDefaultAsync(i => i.ScreenId == screenId && i.MachineId == machineId)
is null ? Json(true) : Json("The selected machine already has the selected screen.");
2019-06-01 22:59:18 +01:00
}
}