Add license creation in admin view.

This commit is contained in:
2020-05-28 02:44:39 +01:00
parent 81e0d4af11
commit cb6cde600d
8 changed files with 45 additions and 267 deletions

View File

@@ -1,127 +0,0 @@
using System.Linq;
using System.Threading.Tasks;
using Marechai.Database.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace Marechai
{
[Area("Admin"), Authorize]
public class LicensesController : Controller
{
readonly MarechaiContext _context;
public LicensesController(MarechaiContext context) => _context = context;
// GET: Licenses
public async Task<IActionResult> Index() => View(await _context.Licenses.OrderBy(l => l.Name).ToListAsync());
// GET: Licenses/Details/5
public async Task<IActionResult> Details(int? id)
{
if(id == null)
return NotFound();
License license = await _context.Licenses.FirstOrDefaultAsync(m => m.Id == id);
if(license == null)
return NotFound();
return View(license);
}
// GET: Licenses/Create
public IActionResult Create() => View();
// POST: Licenses/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("Name,SPDX,FsfApproved,OsiApproved,Link,Text,Id")]
License license)
{
if(ModelState.IsValid)
{
_context.Add(license);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(license);
}
// GET: Licenses/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if(id == null)
return NotFound();
License license = await _context.Licenses.FindAsync(id);
if(license == null)
return NotFound();
return View(license);
}
// POST: Licenses/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("Name,SPDX,FsfApproved,OsiApproved,Link,Text,Id")]
License license)
{
if(id != license.Id)
return NotFound();
if(ModelState.IsValid)
{
try
{
_context.Update(license);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
if(!LicenseExists(license.Id))
return NotFound();
throw;
}
return RedirectToAction(nameof(Index));
}
return View(license);
}
// GET: Licenses/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if(id == null)
return NotFound();
License license = await _context.Licenses.FirstOrDefaultAsync(m => m.Id == id);
if(license == null)
return NotFound();
return View(license);
}
// POST: Licenses/Delete/5
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
License license = await _context.Licenses.FindAsync(id);
_context.Licenses.Remove(license);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
bool LicenseExists(int id) => _context.Licenses.Any(e => e.Id == id);
}
}

View File

@@ -1,64 +0,0 @@
@model Marechai.Database.Models.License
@{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>License</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="Name" class="control-label">
</label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="SPDX" class="control-label">
</label>
<input asp-for="SPDX" class="form-control" />
<span asp-validation-for="SPDX" class="text-danger">
</span>
</div>
<div class="form-check form-group">
<label class="form-check-label">
<input class="form-check-input" asp-for="FsfApproved" /> @Html.DisplayNameFor(model => model.FsfApproved)
</label>
</div>
<div class="form-check form-group">
<label class="form-check-label">
<input class="form-check-input" asp-for="OsiApproved" /> @Html.DisplayNameFor(model => model.OsiApproved)
</label>
</div>
<div class="form-group">
<label asp-for="Link" class="control-label">
</label>
<input asp-for="Link" class="form-control" />
<span asp-validation-for="Link" class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="Text" class="control-label">
</label>
<textarea asp-for="Text" class="form-control" ></textarea>
<span asp-validation-for="Text" 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"); }
}

View File

@@ -1,65 +0,0 @@
@model Marechai.Database.Models.License
@{
ViewData["Title"] = "Edit";
}
<h1>Edit</h1>
<h4>License</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger">
</div>
<div class="form-group">
<label asp-for="Name" class="control-label">
</label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="SPDX" class="control-label">
</label>
<input asp-for="SPDX" class="form-control" />
<span asp-validation-for="SPDX" class="text-danger">
</span>
</div>
<div class="form-check form-group">
<label class="form-check-label">
<input class="form-check-input" asp-for="FsfApproved" /> @Html.DisplayNameFor(model => model.FsfApproved)
</label>
</div>
<div class="form-check form-group">
<label class="form-check-label">
<input class="form-check-input" asp-for="OsiApproved" /> @Html.DisplayNameFor(model => model.OsiApproved)
</label>
</div>
<div class="form-group">
<label asp-for="Link" class="control-label">
</label>
<input asp-for="Link" class="form-control" />
<span asp-validation-for="Link" class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="Text" class="control-label">
</label>
<textarea asp-for="Text" class="form-control"></textarea>
<span asp-validation-for="Text" class="text-danger">
</span>
</div>
<input type="hidden" asp-for="Id" />
<div class="form-group">
<input class="btn btn-primary" type="submit" value="Save" />
<a asp-action="Index" class="btn btn-secondary">
Back to List
</a>
</div>
</form>
</div>
</div>
@section Scripts {
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}

View File

@@ -134,5 +134,7 @@
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Gpus\Create.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\Gpus\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\InstructionSetExtensions\Create.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\InstructionSetExtensions\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\InstructionSets\Create.cshtml" /> <_ContentIncludedByDefault Remove="Areas\Admin\Views\InstructionSets\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Licenses\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Licenses\Edit.cshtml" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -32,6 +32,7 @@
@page "/admin/licenses/details/{Id:int}" @page "/admin/licenses/details/{Id:int}"
@page "/admin/licenses/edit/{Id:int}" @page "/admin/licenses/edit/{Id:int}"
@page "/admin/licenses/create"
@inherits OwningComponentBase<LicensesService> @inherits OwningComponentBase<LicensesService>
@inject IStringLocalizer<LicensesService> L @inject IStringLocalizer<LicensesService> L
@inject NavigationManager NavigationManager @inject NavigationManager NavigationManager

View File

@@ -8,6 +8,7 @@ namespace Marechai.Pages.Admin.Details
{ {
public partial class License public partial class License
{ {
bool _creating;
bool _editing; bool _editing;
bool _loaded; bool _loaded;
Database.Models.License _model; Database.Models.License _model;
@@ -23,13 +24,18 @@ namespace Marechai.Pages.Admin.Details
_loaded = true; _loaded = true;
if(Id <= 0) _creating = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/licenses/create", StringComparison.InvariantCulture);
if(Id <= 0 &&
!_creating)
return; return;
_model = await Service.GetAsync(Id); _model = _creating ? new Database.Models.License() : await Service.GetAsync(Id);
_editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). _editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/licenses/edit/", StringComparison.InvariantCulture); StartsWith("admin/licenses/edit/",
StringComparison.InvariantCulture);
if(_editing) if(_editing)
SetCheckboxes(); SetCheckboxes();
@@ -53,7 +59,15 @@ namespace Marechai.Pages.Admin.Details
async void OnCancelClicked() async void OnCancelClicked()
{ {
_editing = false; _editing = false;
_model = await Service.GetAsync(Id);
if(_creating)
{
NavigationManager.ToBaseRelativePath("admin/licenses");
return;
}
_model = await Service.GetAsync(Id);
SetCheckboxes(); SetCheckboxes();
StateHasChanged(); StateHasChanged();
} }
@@ -79,9 +93,14 @@ namespace Marechai.Pages.Admin.Details
_model.Link?.Length > 512) _model.Link?.Length > 512)
return; return;
_editing = false; if(_creating)
await Service.UpdateAsync(_model); Id = await Service.CreateAsync(_model);
_model = await Service.GetAsync(Id); else
await Service.UpdateAsync(_model);
_editing = false;
_creating = false;
_model = await Service.GetAsync(Id);
SetCheckboxes(); SetCheckboxes();
StateHasChanged(); StateHasChanged();
} }

View File

@@ -42,9 +42,7 @@
return; return;
} }
<p> <p>
<span class="btn btn-primary"> <a class="btn btn-primary" href="/admin/licenses/create">@L["Create new"]</a>
@L["Create new"]
</span>
</p> </p>
<table class="table table-striped"> <table class="table table-striped">
<thead> <thead>

View File

@@ -43,6 +43,20 @@ namespace Marechai.Services
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
} }
public async Task<int> CreateAsync(License viewModel)
{
var model = new License
{
FsfApproved = viewModel.FsfApproved, Link = viewModel.Link, Name = viewModel.Name,
OsiApproved = viewModel.OsiApproved, SPDX = viewModel.SPDX, Text = viewModel.Text
};
await _context.Licenses.AddAsync(model);
await _context.SaveChangesAsync();
return model.Id;
}
public async Task DeleteAsync(int id) public async Task DeleteAsync(int id)
{ {
License item = await _context.Licenses.FindAsync(id); License item = await _context.Licenses.FindAsync(id);