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

197 lines
7.6 KiB
C#
Raw Normal View History

2019-06-01 22:59:18 +01:00
using System.Linq;
using System.Threading.Tasks;
2020-02-10 02:10: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 ResolutionsByScreenController : 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 ResolutionsByScreenController(MarechaiContext context) => _context = context;
2019-06-01 22:59:18 +01:00
// GET: ResolutionsByScreen
public async Task<IActionResult> Index()
{
2020-02-10 02:47:39 +00:00
IIncludableQueryable<ResolutionsByScreen, Screen> marechaiContext =
2019-06-01 22:59:18 +01:00
_context.ResolutionsByScreen.Include(r => r.Resolution).Include(r => r.Screen);
2020-02-10 22:44:18 +00:00
return View(await marechaiContext.OrderBy(r => r.Screen.ToString()).ThenBy(r => r.Resolution.ToString()).
ToListAsync());
2019-06-01 22:59:18 +01:00
}
// GET: ResolutionsByScreen/Details/5
public async Task<IActionResult> Details(long? id)
{
2020-02-10 22:44:18 +00:00
if(id == null)
return NotFound();
ResolutionsByScreen resolutionsByScreen =
await _context.ResolutionsByScreen.Include(r => r.Resolution).Include(r => r.Screen).
FirstOrDefaultAsync(m => m.Id == id);
2019-06-01 22:59:18 +01:00
2020-02-10 22:44:18 +00:00
if(resolutionsByScreen == null)
return NotFound();
2019-06-01 22:59:18 +01:00
return View(resolutionsByScreen);
}
// GET: ResolutionsByScreen/Create
public IActionResult Create()
{
2020-02-10 22:44:18 +00:00
ViewData["ResolutionId"] = new SelectList(_context.Resolutions.Select(r => new
{
r.Id, Name = r.ToString()
}).OrderBy(r => r.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: ResolutionsByScreen/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,ResolutionId,Id")] ResolutionsByScreen resolutionsByScreen)
{
if(ModelState.IsValid)
{
_context.Add(resolutionsByScreen);
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["ResolutionId"] = new SelectList(_context.Resolutions.Select(r => new
{
r.Id, Name = r.ToString()
}).OrderBy(r => r.Name), "Id", "Name", resolutionsByScreen.ResolutionId);
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", resolutionsByScreen.ScreenId);
2019-06-01 22:59:18 +01:00
return View(resolutionsByScreen);
}
// GET: ResolutionsByScreen/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
ResolutionsByScreen resolutionsByScreen = await _context.ResolutionsByScreen.FindAsync(id);
2020-02-10 22:44:18 +00:00
if(resolutionsByScreen == null)
return NotFound();
ViewData["ResolutionId"] = new SelectList(_context.Resolutions.Select(r => new
{
r.Id, Name = r.ToString()
}).OrderBy(r => r.Name), "Id", "Name", resolutionsByScreen.ResolutionId);
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", resolutionsByScreen.ScreenId);
2019-06-01 22:59:18 +01:00
return View(resolutionsByScreen);
}
// POST: ResolutionsByScreen/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,ResolutionId,Id")] ResolutionsByScreen resolutionsByScreen)
{
2020-02-10 22:44:18 +00:00
if(id != resolutionsByScreen.Id)
return NotFound();
2019-06-01 22:59:18 +01:00
if(ModelState.IsValid)
{
try
{
_context.Update(resolutionsByScreen);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
2020-02-10 22:44:18 +00:00
if(!ResolutionsByScreenExists(resolutionsByScreen.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["ResolutionId"] = new SelectList(_context.Resolutions.Select(r => new
{
r.Id, Name = r.ToString()
}).OrderBy(r => r.Name), "Id", "Name", resolutionsByScreen.ResolutionId);
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", resolutionsByScreen.ScreenId);
2019-06-01 22:59:18 +01:00
return View(resolutionsByScreen);
}
// GET: ResolutionsByScreen/Delete/5
public async Task<IActionResult> Delete(long? id)
{
2020-02-10 22:44:18 +00:00
if(id == null)
return NotFound();
ResolutionsByScreen resolutionsByScreen =
await _context.ResolutionsByScreen.Include(r => r.Resolution).Include(r => r.Screen).
FirstOrDefaultAsync(m => m.Id == id);
2019-06-01 22:59:18 +01:00
2020-02-10 22:44:18 +00:00
if(resolutionsByScreen == null)
return NotFound();
2019-06-01 22:59:18 +01:00
return View(resolutionsByScreen);
}
// POST: ResolutionsByScreen/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)
{
ResolutionsByScreen resolutionsByScreen = await _context.ResolutionsByScreen.FindAsync(id);
_context.ResolutionsByScreen.Remove(resolutionsByScreen);
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 ResolutionsByScreenExists(long id) => _context.ResolutionsByScreen.Any(e => e.Id == id);
[AcceptVerbs("Get", "Post")]
2020-02-10 22:44:18 +00:00
public async Task<IActionResult> VerifyUnique(int screenId, int resolutionId) =>
await _context.ResolutionsByScreen.FirstOrDefaultAsync(i => i.ScreenId == screenId &&
i.ResolutionId == resolutionId) is null
? Json(true) : Json("The selected screen already has the selected resolution.");
2019-06-01 22:59:18 +01:00
}
}