mirror of
https://github.com/claunia/marechai.git
synced 2025-12-16 19:14:25 +00:00
Add screen creation in admin view.
This commit is contained in:
@@ -1,164 +0,0 @@
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Marechai.Database.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Marechai.Areas.Admin.Controllers
|
||||
{
|
||||
[Area("Admin"), Authorize]
|
||||
public class ScreensController : Controller
|
||||
{
|
||||
readonly MarechaiContext _context;
|
||||
|
||||
public ScreensController(MarechaiContext context) => _context = context;
|
||||
|
||||
// GET: Screens
|
||||
public async Task<IActionResult> Index() =>
|
||||
View(await _context.Screens.OrderBy(s => s.Diagonal).ThenBy(s => s.EffectiveColors).
|
||||
ThenBy(s => s.NativeResolution.ToString()).ThenBy(s => s.Type).ThenBy(s => s.Size).
|
||||
ToListAsync());
|
||||
|
||||
// GET: Screens/Details/5
|
||||
public async Task<IActionResult> Details(int? id)
|
||||
{
|
||||
if(id == null)
|
||||
return NotFound();
|
||||
|
||||
Screen screen = await _context.Screens.FirstOrDefaultAsync(m => m.Id == id);
|
||||
|
||||
if(screen == null)
|
||||
return NotFound();
|
||||
|
||||
return View(screen);
|
||||
}
|
||||
|
||||
// GET: Screens/Create
|
||||
public IActionResult Create()
|
||||
{
|
||||
ViewData["NativeResolutionId"] = new SelectList(_context.Resolutions.OrderBy(r => r.Chars).
|
||||
ThenBy(r => r.Width).ThenBy(r => r.Height).
|
||||
ThenBy(r => r.Colors).Select(r => new
|
||||
{
|
||||
r.Id, Name = r.ToString()
|
||||
}), "Id", "Name");
|
||||
|
||||
return View();
|
||||
}
|
||||
|
||||
// POST: Screens/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]
|
||||
public async Task<IActionResult> Create(
|
||||
[Bind("Width,Height,Diagonal,EffectiveColors,Type,NativeResolutionId,Id")]
|
||||
Screen screen)
|
||||
{
|
||||
if(ModelState.IsValid)
|
||||
{
|
||||
_context.Add(screen);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
ViewData["NativeResolutionId"] = new SelectList(_context.Resolutions.OrderBy(r => r.Chars).
|
||||
ThenBy(r => r.Width).ThenBy(r => r.Height).
|
||||
ThenBy(r => r.Colors).Select(r => new
|
||||
{
|
||||
r.Id, Name = r.ToString()
|
||||
}), "Id", "Name");
|
||||
|
||||
return View(screen);
|
||||
}
|
||||
|
||||
// GET: Screens/Edit/5
|
||||
public async Task<IActionResult> Edit(int? id)
|
||||
{
|
||||
if(id == null)
|
||||
return NotFound();
|
||||
|
||||
Screen screen = await _context.Screens.FindAsync(id);
|
||||
|
||||
if(screen == null)
|
||||
return NotFound();
|
||||
|
||||
ViewData["NativeResolutionId"] = new SelectList(_context.Resolutions.OrderBy(r => r.Chars).
|
||||
ThenBy(r => r.Width).ThenBy(r => r.Height).
|
||||
ThenBy(r => r.Colors).Select(r => new
|
||||
{
|
||||
r.Id, Name = r.ToString()
|
||||
}), "Id", "Name");
|
||||
|
||||
return View(screen);
|
||||
}
|
||||
|
||||
// POST: Screens/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]
|
||||
public async Task<IActionResult> Edit(
|
||||
int id, [Bind("Width,Height,Diagonal,EffectiveColors,NativeResolutionId,Type,Id")]
|
||||
Screen screen)
|
||||
{
|
||||
if(id != screen.Id)
|
||||
return NotFound();
|
||||
|
||||
if(ModelState.IsValid)
|
||||
{
|
||||
try
|
||||
{
|
||||
_context.Update(screen);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch(DbUpdateConcurrencyException)
|
||||
{
|
||||
if(!ScreenExists(screen.Id))
|
||||
return NotFound();
|
||||
|
||||
throw;
|
||||
}
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
ViewData["NativeResolutionId"] = new SelectList(_context.Resolutions.OrderBy(r => r.Chars).
|
||||
ThenBy(r => r.Width).ThenBy(r => r.Height).
|
||||
ThenBy(r => r.Colors).Select(r => new
|
||||
{
|
||||
r.Id, Name = r.ToString()
|
||||
}), "Id", "Name");
|
||||
|
||||
return View(screen);
|
||||
}
|
||||
|
||||
// GET: Screens/Delete/5
|
||||
public async Task<IActionResult> Delete(int? id)
|
||||
{
|
||||
if(id == null)
|
||||
return NotFound();
|
||||
|
||||
Screen screen = await _context.Screens.FirstOrDefaultAsync(m => m.Id == id);
|
||||
|
||||
if(screen == null)
|
||||
return NotFound();
|
||||
|
||||
return View(screen);
|
||||
}
|
||||
|
||||
// POST: Screens/Delete/5
|
||||
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> DeleteConfirmed(int id)
|
||||
{
|
||||
Screen screen = await _context.Screens.FindAsync(id);
|
||||
_context.Screens.Remove(screen);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
bool ScreenExists(int id) => _context.Screens.Any(e => e.Id == id);
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
@model Marechai.Database.Models.Screen
|
||||
|
||||
@{
|
||||
ViewData["Title"] = "Create";
|
||||
}
|
||||
<h1>Create</h1>
|
||||
<h4>Screen</h4>
|
||||
<hr />
|
||||
<div class="row">
|
||||
<div class="col-md-4">
|
||||
<form asp-action="Create">
|
||||
<div asp-validation-summary="ModelOnly" class="text-danger">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="Diagonal" class="control-label">
|
||||
</label>
|
||||
<input asp-for="Diagonal" class="form-control" />
|
||||
<span asp-validation-for="Diagonal" class="text-danger">
|
||||
</span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="Width" class="control-label">
|
||||
</label>
|
||||
<input asp-for="Width" class="form-control" />
|
||||
<span asp-validation-for="Width" class="text-danger">
|
||||
</span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="Height" class="control-label">
|
||||
</label>
|
||||
<input asp-for="Height" class="form-control" />
|
||||
<span asp-validation-for="Height" class="text-danger">
|
||||
</span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="NativeResolution" class="control-label">
|
||||
</label>
|
||||
<select asp-for="NativeResolutionId" class="form-control" asp-items="ViewBag.NativeResolutionId">
|
||||
</select>
|
||||
<span asp-validation-for="NativeResolutionId" class="text-danger">
|
||||
</span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="EffectiveColors" class="control-label">
|
||||
</label>
|
||||
<input asp-for="EffectiveColors" class="form-control" />
|
||||
<span asp-validation-for="EffectiveColors" class="text-danger">
|
||||
</span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="Type" class="control-label">
|
||||
</label>
|
||||
<input asp-for="Type" class="form-control" />
|
||||
<span asp-validation-for="Type" class="text-danger">
|
||||
</span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<input class="btn btn-primary" type="submit" value="Create" />
|
||||
<a asp-action="Index" class="btn btn-secondary">
|
||||
Back to List
|
||||
</a>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@section Scripts {
|
||||
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
|
||||
}
|
||||
@@ -140,5 +140,6 @@
|
||||
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Machines\Create.cshtml" />
|
||||
<_ContentIncludedByDefault Remove="Areas\Admin\Views\People\Create.cshtml" />
|
||||
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Processors\Create.cshtml" />
|
||||
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Screens\Create.cshtml" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -32,6 +32,7 @@
|
||||
|
||||
@page "/admin/screens/details/{Id:int}"
|
||||
@page "/admin/screens/edit/{Id:int}"
|
||||
@page "/admin/screens/create"
|
||||
@inherits OwningComponentBase<ScreensService>
|
||||
@inject IStringLocalizer<ScreensService> L
|
||||
@inject ResolutionsService ResolutionsService
|
||||
|
||||
@@ -10,6 +10,7 @@ namespace Marechai.Pages.Admin.Details
|
||||
{
|
||||
public partial class Screen
|
||||
{
|
||||
bool _creating;
|
||||
bool _editing;
|
||||
bool _loaded;
|
||||
ScreenViewModel _model;
|
||||
@@ -28,14 +29,19 @@ namespace Marechai.Pages.Admin.Details
|
||||
|
||||
_loaded = true;
|
||||
|
||||
if(Id <= 0)
|
||||
_creating = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
|
||||
StartsWith("admin/screens/create", StringComparison.InvariantCulture);
|
||||
|
||||
if(Id <= 0 &&
|
||||
!_creating)
|
||||
return;
|
||||
|
||||
_resolutions = await ResolutionsService.GetAsync();
|
||||
_model = await Service.GetAsync(Id);
|
||||
_model = _creating ? new ScreenViewModel() : await Service.GetAsync(Id);
|
||||
|
||||
_editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
|
||||
StartsWith("admin/screens/edit/", StringComparison.InvariantCulture);
|
||||
_editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
|
||||
StartsWith("admin/screens/edit/",
|
||||
StringComparison.InvariantCulture);
|
||||
|
||||
if(_editing)
|
||||
SetCheckboxes();
|
||||
@@ -61,7 +67,15 @@ namespace Marechai.Pages.Admin.Details
|
||||
async void OnCancelClicked()
|
||||
{
|
||||
_editing = false;
|
||||
_model = await Service.GetAsync(Id);
|
||||
|
||||
if(_creating)
|
||||
{
|
||||
NavigationManager.ToBaseRelativePath("admin/screens");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
_model = await Service.GetAsync(Id);
|
||||
SetCheckboxes();
|
||||
StateHasChanged();
|
||||
}
|
||||
@@ -88,9 +102,14 @@ namespace Marechai.Pages.Admin.Details
|
||||
else if(_model.EffectiveColors < 0)
|
||||
return;
|
||||
|
||||
_editing = false;
|
||||
await Service.UpdateAsync(_model);
|
||||
_model = await Service.GetAsync(Id);
|
||||
if(_creating)
|
||||
Id = await Service.CreateAsync(_model);
|
||||
else
|
||||
await Service.UpdateAsync(_model);
|
||||
|
||||
_editing = false;
|
||||
_creating = false;
|
||||
_model = await Service.GetAsync(Id);
|
||||
SetCheckboxes();
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
@@ -42,9 +42,7 @@
|
||||
return;
|
||||
}
|
||||
<p>
|
||||
<span class="btn btn-primary">
|
||||
@L["Create new"]
|
||||
</span>
|
||||
<a class="btn btn-primary" href="/admin/screens/create">@L["Create new"]</a>
|
||||
</p>
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
|
||||
@@ -52,6 +52,20 @@ namespace Marechai.Services
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<int> CreateAsync(ScreenViewModel viewModel)
|
||||
{
|
||||
var model = new Screen
|
||||
{
|
||||
Diagonal = viewModel.Diagonal, EffectiveColors = viewModel.EffectiveColors, Height = viewModel.Height,
|
||||
NativeResolutionId = viewModel.NativeResolutionId, Type = viewModel.Type, Width = viewModel.Width
|
||||
};
|
||||
|
||||
await _context.Screens.AddAsync(model);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return model.Id;
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(int id)
|
||||
{
|
||||
Screen item = await _context.Screens.FindAsync(id);
|
||||
|
||||
Reference in New Issue
Block a user