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:
@@ -1,7 +1,7 @@
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Marechai.Database.Models;
|
||||
using Marechai.Areas.Admin.Models;
|
||||
using Marechai.Database.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
@@ -10,40 +10,38 @@ using Microsoft.EntityFrameworkCore.Query;
|
||||
|
||||
namespace Marechai.Areas.Admin.Controllers
|
||||
{
|
||||
[Area("Admin")]
|
||||
[Authorize]
|
||||
[Area("Admin"), Authorize]
|
||||
public class SoundByMachineController : Controller
|
||||
{
|
||||
readonly MarechaiContext _context;
|
||||
|
||||
public SoundByMachineController(MarechaiContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
public SoundByMachineController(MarechaiContext context) => _context = context;
|
||||
|
||||
// GET: SoundByMachine
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
IIncludableQueryable<SoundByMachine, SoundSynth> marechaiContext =
|
||||
_context.SoundByMachine.Include(s => s.Machine).Include(s => s.SoundSynth);
|
||||
return View(await marechaiContext.OrderBy(s => s.Machine.Name).ThenBy(s => s.SoundSynth.Name)
|
||||
.Select(s => new SoundByMachineViewModel
|
||||
{
|
||||
Id = s.Id,
|
||||
Machine = s.Machine.Name,
|
||||
SoundSynth = s.SoundSynth.Name
|
||||
}).ToListAsync());
|
||||
|
||||
return View(await marechaiContext.OrderBy(s => s.Machine.Name).ThenBy(s => s.SoundSynth.Name).
|
||||
Select(s => new SoundByMachineViewModel
|
||||
{
|
||||
Id = s.Id, Machine = s.Machine.Name, SoundSynth = s.SoundSynth.Name
|
||||
}).ToListAsync());
|
||||
}
|
||||
|
||||
// GET: SoundByMachine/Details/5
|
||||
public async Task<IActionResult> Details(long? id)
|
||||
{
|
||||
if(id == null) return NotFound();
|
||||
if(id == null)
|
||||
return NotFound();
|
||||
|
||||
SoundByMachine soundByMachine = await _context.SoundByMachine
|
||||
.Include(s => s.Machine).Include(s => s.SoundSynth)
|
||||
.FirstOrDefaultAsync(m => m.Id == id);
|
||||
if(soundByMachine == null) return NotFound();
|
||||
SoundByMachine soundByMachine = await _context.
|
||||
SoundByMachine.Include(s => s.Machine).Include(s => s.SoundSynth).
|
||||
FirstOrDefaultAsync(m => m.Id == id);
|
||||
|
||||
if(soundByMachine == null)
|
||||
return NotFound();
|
||||
|
||||
return View(soundByMachine);
|
||||
}
|
||||
@@ -51,52 +49,58 @@ namespace Marechai.Areas.Admin.Controllers
|
||||
// GET: SoundByMachine/Create
|
||||
public IActionResult Create()
|
||||
{
|
||||
ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name");
|
||||
ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name");
|
||||
ViewData["SoundSynthId"] = new SelectList(_context.SoundSynths, "Id", "Name");
|
||||
|
||||
return View();
|
||||
}
|
||||
|
||||
// POST: SoundByMachine/Create
|
||||
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
||||
// 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("SoundSynthId,MachineId,Id")] SoundByMachine soundByMachine)
|
||||
{
|
||||
if(ModelState.IsValid)
|
||||
{
|
||||
_context.Add(soundByMachine);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name", soundByMachine.MachineId);
|
||||
ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name", soundByMachine.MachineId);
|
||||
ViewData["SoundSynthId"] = new SelectList(_context.SoundSynths, "Id", "Name", soundByMachine.SoundSynthId);
|
||||
|
||||
return View(soundByMachine);
|
||||
}
|
||||
|
||||
// GET: SoundByMachine/Edit/5
|
||||
public async Task<IActionResult> Edit(long? id)
|
||||
{
|
||||
if(id == null) return NotFound();
|
||||
if(id == null)
|
||||
return NotFound();
|
||||
|
||||
SoundByMachine soundByMachine = await _context.SoundByMachine.FindAsync(id);
|
||||
if(soundByMachine == null) return NotFound();
|
||||
|
||||
ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name", soundByMachine.MachineId);
|
||||
if(soundByMachine == null)
|
||||
return NotFound();
|
||||
|
||||
ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name", soundByMachine.MachineId);
|
||||
ViewData["SoundSynthId"] = new SelectList(_context.SoundSynths, "Id", "Name", soundByMachine.SoundSynthId);
|
||||
|
||||
return View(soundByMachine);
|
||||
}
|
||||
|
||||
// POST: SoundByMachine/Edit/5
|
||||
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
|
||||
// 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("SoundSynthId,MachineId,Id")] SoundByMachine soundByMachine)
|
||||
{
|
||||
if(id != soundByMachine.Id) return NotFound();
|
||||
if(id != soundByMachine.Id)
|
||||
return NotFound();
|
||||
|
||||
if(ModelState.IsValid)
|
||||
{
|
||||
@@ -107,7 +111,8 @@ namespace Marechai.Areas.Admin.Controllers
|
||||
}
|
||||
catch(DbUpdateConcurrencyException)
|
||||
{
|
||||
if(!SoundByMachineExists(soundByMachine.Id)) return NotFound();
|
||||
if(!SoundByMachineExists(soundByMachine.Id))
|
||||
return NotFound();
|
||||
|
||||
throw;
|
||||
}
|
||||
@@ -115,39 +120,39 @@ namespace Marechai.Areas.Admin.Controllers
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name", soundByMachine.MachineId);
|
||||
ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name", soundByMachine.MachineId);
|
||||
ViewData["SoundSynthId"] = new SelectList(_context.SoundSynths, "Id", "Name", soundByMachine.SoundSynthId);
|
||||
|
||||
return View(soundByMachine);
|
||||
}
|
||||
|
||||
// GET: SoundByMachine/Delete/5
|
||||
public async Task<IActionResult> Delete(long? id)
|
||||
{
|
||||
if(id == null) return NotFound();
|
||||
if(id == null)
|
||||
return NotFound();
|
||||
|
||||
SoundByMachine soundByMachine = await _context.SoundByMachine
|
||||
.Include(s => s.Machine).Include(s => s.SoundSynth)
|
||||
.FirstOrDefaultAsync(m => m.Id == id);
|
||||
if(soundByMachine == null) return NotFound();
|
||||
SoundByMachine soundByMachine = await _context.
|
||||
SoundByMachine.Include(s => s.Machine).Include(s => s.SoundSynth).
|
||||
FirstOrDefaultAsync(m => m.Id == id);
|
||||
|
||||
if(soundByMachine == null)
|
||||
return NotFound();
|
||||
|
||||
return View(soundByMachine);
|
||||
}
|
||||
|
||||
// POST: SoundByMachine/Delete/5
|
||||
[HttpPost]
|
||||
[ActionName("Delete")]
|
||||
[ValidateAntiForgeryToken]
|
||||
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> DeleteConfirmed(long id)
|
||||
{
|
||||
SoundByMachine soundByMachine = await _context.SoundByMachine.FindAsync(id);
|
||||
_context.SoundByMachine.Remove(soundByMachine);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
bool SoundByMachineExists(long id)
|
||||
{
|
||||
return _context.SoundByMachine.Any(e => e.Id == id);
|
||||
}
|
||||
bool SoundByMachineExists(long id) => _context.SoundByMachine.Any(e => e.Id == id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user