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\Machines\Create.cshtml" />
|
||||||
<_ContentIncludedByDefault Remove="Areas\Admin\Views\People\Create.cshtml" />
|
<_ContentIncludedByDefault Remove="Areas\Admin\Views\People\Create.cshtml" />
|
||||||
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Processors\Create.cshtml" />
|
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Processors\Create.cshtml" />
|
||||||
|
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Screens\Create.cshtml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -32,6 +32,7 @@
|
|||||||
|
|
||||||
@page "/admin/screens/details/{Id:int}"
|
@page "/admin/screens/details/{Id:int}"
|
||||||
@page "/admin/screens/edit/{Id:int}"
|
@page "/admin/screens/edit/{Id:int}"
|
||||||
|
@page "/admin/screens/create"
|
||||||
@inherits OwningComponentBase<ScreensService>
|
@inherits OwningComponentBase<ScreensService>
|
||||||
@inject IStringLocalizer<ScreensService> L
|
@inject IStringLocalizer<ScreensService> L
|
||||||
@inject ResolutionsService ResolutionsService
|
@inject ResolutionsService ResolutionsService
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ namespace Marechai.Pages.Admin.Details
|
|||||||
{
|
{
|
||||||
public partial class Screen
|
public partial class Screen
|
||||||
{
|
{
|
||||||
|
bool _creating;
|
||||||
bool _editing;
|
bool _editing;
|
||||||
bool _loaded;
|
bool _loaded;
|
||||||
ScreenViewModel _model;
|
ScreenViewModel _model;
|
||||||
@@ -28,14 +29,19 @@ namespace Marechai.Pages.Admin.Details
|
|||||||
|
|
||||||
_loaded = true;
|
_loaded = true;
|
||||||
|
|
||||||
if(Id <= 0)
|
_creating = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
|
||||||
|
StartsWith("admin/screens/create", StringComparison.InvariantCulture);
|
||||||
|
|
||||||
|
if(Id <= 0 &&
|
||||||
|
!_creating)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_resolutions = await ResolutionsService.GetAsync();
|
_resolutions = await ResolutionsService.GetAsync();
|
||||||
_model = await Service.GetAsync(Id);
|
_model = _creating ? new ScreenViewModel() : await Service.GetAsync(Id);
|
||||||
|
|
||||||
_editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
|
_editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
|
||||||
StartsWith("admin/screens/edit/", StringComparison.InvariantCulture);
|
StartsWith("admin/screens/edit/",
|
||||||
|
StringComparison.InvariantCulture);
|
||||||
|
|
||||||
if(_editing)
|
if(_editing)
|
||||||
SetCheckboxes();
|
SetCheckboxes();
|
||||||
@@ -61,6 +67,14 @@ namespace Marechai.Pages.Admin.Details
|
|||||||
async void OnCancelClicked()
|
async void OnCancelClicked()
|
||||||
{
|
{
|
||||||
_editing = false;
|
_editing = false;
|
||||||
|
|
||||||
|
if(_creating)
|
||||||
|
{
|
||||||
|
NavigationManager.ToBaseRelativePath("admin/screens");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
_model = await Service.GetAsync(Id);
|
_model = await Service.GetAsync(Id);
|
||||||
SetCheckboxes();
|
SetCheckboxes();
|
||||||
StateHasChanged();
|
StateHasChanged();
|
||||||
@@ -88,8 +102,13 @@ namespace Marechai.Pages.Admin.Details
|
|||||||
else if(_model.EffectiveColors < 0)
|
else if(_model.EffectiveColors < 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_editing = false;
|
if(_creating)
|
||||||
|
Id = await Service.CreateAsync(_model);
|
||||||
|
else
|
||||||
await Service.UpdateAsync(_model);
|
await Service.UpdateAsync(_model);
|
||||||
|
|
||||||
|
_editing = false;
|
||||||
|
_creating = false;
|
||||||
_model = await Service.GetAsync(Id);
|
_model = await Service.GetAsync(Id);
|
||||||
SetCheckboxes();
|
SetCheckboxes();
|
||||||
StateHasChanged();
|
StateHasChanged();
|
||||||
|
|||||||
@@ -42,9 +42,7 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
<p>
|
<p>
|
||||||
<span class="btn btn-primary">
|
<a class="btn btn-primary" href="/admin/screens/create">@L["Create new"]</a>
|
||||||
@L["Create new"]
|
|
||||||
</span>
|
|
||||||
</p>
|
</p>
|
||||||
<table class="table table-striped">
|
<table class="table table-striped">
|
||||||
<thead>
|
<thead>
|
||||||
|
|||||||
@@ -52,6 +52,20 @@ namespace Marechai.Services
|
|||||||
await _context.SaveChangesAsync();
|
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)
|
public async Task DeleteAsync(int id)
|
||||||
{
|
{
|
||||||
Screen item = await _context.Screens.FindAsync(id);
|
Screen item = await _context.Screens.FindAsync(id);
|
||||||
|
|||||||
Reference in New Issue
Block a user