mirror of
https://github.com/claunia/marechai.git
synced 2025-12-16 19:14:25 +00:00
Refactor and reorganize code.
This commit is contained in:
@@ -9,35 +9,35 @@ using Microsoft.EntityFrameworkCore.Query;
|
||||
|
||||
namespace Marechai.Areas.Admin.Controllers
|
||||
{
|
||||
[Area("Admin")]
|
||||
[Authorize]
|
||||
[Area("Admin"), Authorize]
|
||||
public class ResolutionsByScreenController : Controller
|
||||
{
|
||||
readonly MarechaiContext _context;
|
||||
|
||||
public ResolutionsByScreenController(MarechaiContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
public ResolutionsByScreenController(MarechaiContext context) => _context = context;
|
||||
|
||||
// GET: ResolutionsByScreen
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
IIncludableQueryable<ResolutionsByScreen, Screen> marechaiContext =
|
||||
_context.ResolutionsByScreen.Include(r => r.Resolution).Include(r => r.Screen);
|
||||
return View(await marechaiContext.OrderBy(r => r.Screen.ToString()).ThenBy(r => r.Resolution.ToString())
|
||||
.ToListAsync());
|
||||
|
||||
return View(await marechaiContext.OrderBy(r => r.Screen.ToString()).ThenBy(r => r.Resolution.ToString()).
|
||||
ToListAsync());
|
||||
}
|
||||
|
||||
// GET: ResolutionsByScreen/Details/5
|
||||
public async Task<IActionResult> Details(long? id)
|
||||
{
|
||||
if(id == null) return NotFound();
|
||||
if(id == null)
|
||||
return NotFound();
|
||||
|
||||
ResolutionsByScreen resolutionsByScreen = await _context.ResolutionsByScreen
|
||||
.Include(r => r.Resolution).Include(r => r.Screen)
|
||||
.FirstOrDefaultAsync(m => m.Id == id);
|
||||
if(resolutionsByScreen == null) return NotFound();
|
||||
ResolutionsByScreen resolutionsByScreen =
|
||||
await _context.ResolutionsByScreen.Include(r => r.Resolution).Include(r => r.Screen).
|
||||
FirstOrDefaultAsync(m => m.Id == id);
|
||||
|
||||
if(resolutionsByScreen == null)
|
||||
return NotFound();
|
||||
|
||||
return View(resolutionsByScreen);
|
||||
}
|
||||
@@ -45,21 +45,25 @@ namespace Marechai.Areas.Admin.Controllers
|
||||
// GET: ResolutionsByScreen/Create
|
||||
public IActionResult Create()
|
||||
{
|
||||
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");
|
||||
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");
|
||||
|
||||
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.
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
[HttpPost, ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Create(
|
||||
[Bind("ScreenId,ResolutionId,Id")] ResolutionsByScreen resolutionsByScreen)
|
||||
{
|
||||
@@ -67,46 +71,60 @@ namespace Marechai.Areas.Admin.Controllers
|
||||
{
|
||||
_context.Add(resolutionsByScreen);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
|
||||
return View(resolutionsByScreen);
|
||||
}
|
||||
|
||||
// GET: ResolutionsByScreen/Edit/5
|
||||
public async Task<IActionResult> Edit(long? id)
|
||||
{
|
||||
if(id == null) return NotFound();
|
||||
if(id == null)
|
||||
return NotFound();
|
||||
|
||||
ResolutionsByScreen resolutionsByScreen = await _context.ResolutionsByScreen.FindAsync(id);
|
||||
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);
|
||||
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);
|
||||
|
||||
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.
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
[HttpPost, ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> Edit(
|
||||
long id, [Bind("ScreenId,ResolutionId,Id")] ResolutionsByScreen resolutionsByScreen)
|
||||
{
|
||||
if(id != resolutionsByScreen.Id) return NotFound();
|
||||
if(id != resolutionsByScreen.Id)
|
||||
return NotFound();
|
||||
|
||||
if(ModelState.IsValid)
|
||||
{
|
||||
@@ -117,7 +135,8 @@ namespace Marechai.Areas.Admin.Controllers
|
||||
}
|
||||
catch(DbUpdateConcurrencyException)
|
||||
{
|
||||
if(!ResolutionsByScreenExists(resolutionsByScreen.Id)) return NotFound();
|
||||
if(!ResolutionsByScreenExists(resolutionsByScreen.Id))
|
||||
return NotFound();
|
||||
|
||||
throw;
|
||||
}
|
||||
@@ -125,53 +144,54 @@ namespace Marechai.Areas.Admin.Controllers
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
|
||||
return View(resolutionsByScreen);
|
||||
}
|
||||
|
||||
// GET: ResolutionsByScreen/Delete/5
|
||||
public async Task<IActionResult> Delete(long? id)
|
||||
{
|
||||
if(id == null) return NotFound();
|
||||
if(id == null)
|
||||
return NotFound();
|
||||
|
||||
ResolutionsByScreen resolutionsByScreen = await _context.ResolutionsByScreen
|
||||
.Include(r => r.Resolution).Include(r => r.Screen)
|
||||
.FirstOrDefaultAsync(m => m.Id == id);
|
||||
if(resolutionsByScreen == null) return NotFound();
|
||||
ResolutionsByScreen resolutionsByScreen =
|
||||
await _context.ResolutionsByScreen.Include(r => r.Resolution).Include(r => r.Screen).
|
||||
FirstOrDefaultAsync(m => m.Id == id);
|
||||
|
||||
if(resolutionsByScreen == null)
|
||||
return NotFound();
|
||||
|
||||
return View(resolutionsByScreen);
|
||||
}
|
||||
|
||||
// POST: ResolutionsByScreen/Delete/5
|
||||
[HttpPost]
|
||||
[ActionName("Delete")]
|
||||
[ValidateAntiForgeryToken]
|
||||
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> DeleteConfirmed(long id)
|
||||
{
|
||||
ResolutionsByScreen resolutionsByScreen = await _context.ResolutionsByScreen.FindAsync(id);
|
||||
_context.ResolutionsByScreen.Remove(resolutionsByScreen);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
bool ResolutionsByScreenExists(long id)
|
||||
{
|
||||
return _context.ResolutionsByScreen.Any(e => e.Id == id);
|
||||
}
|
||||
bool ResolutionsByScreenExists(long id) => _context.ResolutionsByScreen.Any(e => e.Id == id);
|
||||
|
||||
[AcceptVerbs("Get", "Post")]
|
||||
public async Task<IActionResult> VerifyUnique(int screenId, int resolutionId)
|
||||
{
|
||||
return await _context.ResolutionsByScreen.FirstOrDefaultAsync(i => i.ScreenId == screenId &&
|
||||
i.ResolutionId == resolutionId) is null
|
||||
? Json(true)
|
||||
: Json("The selected screen already has the selected resolution.");
|
||||
}
|
||||
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.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user