mirror of
https://github.com/claunia/marechai.git
synced 2025-12-16 19:14:25 +00:00
Add license creation in admin view.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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"); }
|
||||
}
|
||||
@@ -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"); }
|
||||
}
|
||||
@@ -134,5 +134,7 @@
|
||||
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Gpus\Create.cshtml" />
|
||||
<_ContentIncludedByDefault Remove="Areas\Admin\Views\InstructionSetExtensions\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>
|
||||
</Project>
|
||||
@@ -32,6 +32,7 @@
|
||||
|
||||
@page "/admin/licenses/details/{Id:int}"
|
||||
@page "/admin/licenses/edit/{Id:int}"
|
||||
@page "/admin/licenses/create"
|
||||
@inherits OwningComponentBase<LicensesService>
|
||||
@inject IStringLocalizer<LicensesService> L
|
||||
@inject NavigationManager NavigationManager
|
||||
|
||||
@@ -8,6 +8,7 @@ namespace Marechai.Pages.Admin.Details
|
||||
{
|
||||
public partial class License
|
||||
{
|
||||
bool _creating;
|
||||
bool _editing;
|
||||
bool _loaded;
|
||||
Database.Models.License _model;
|
||||
@@ -23,13 +24,18 @@ namespace Marechai.Pages.Admin.Details
|
||||
|
||||
_loaded = true;
|
||||
|
||||
if(Id <= 0)
|
||||
_creating = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
|
||||
StartsWith("admin/licenses/create", StringComparison.InvariantCulture);
|
||||
|
||||
if(Id <= 0 &&
|
||||
!_creating)
|
||||
return;
|
||||
|
||||
_model = await Service.GetAsync(Id);
|
||||
_model = _creating ? new Database.Models.License() : await Service.GetAsync(Id);
|
||||
|
||||
_editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
|
||||
StartsWith("admin/licenses/edit/", StringComparison.InvariantCulture);
|
||||
_editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
|
||||
StartsWith("admin/licenses/edit/",
|
||||
StringComparison.InvariantCulture);
|
||||
|
||||
if(_editing)
|
||||
SetCheckboxes();
|
||||
@@ -53,6 +59,14 @@ namespace Marechai.Pages.Admin.Details
|
||||
async void OnCancelClicked()
|
||||
{
|
||||
_editing = false;
|
||||
|
||||
if(_creating)
|
||||
{
|
||||
NavigationManager.ToBaseRelativePath("admin/licenses");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
_model = await Service.GetAsync(Id);
|
||||
SetCheckboxes();
|
||||
StateHasChanged();
|
||||
@@ -79,8 +93,13 @@ namespace Marechai.Pages.Admin.Details
|
||||
_model.Link?.Length > 512)
|
||||
return;
|
||||
|
||||
_editing = false;
|
||||
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/licenses/create">@L["Create new"]</a>
|
||||
</p>
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
|
||||
@@ -43,6 +43,20 @@ namespace Marechai.Services
|
||||
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)
|
||||
{
|
||||
License item = await _context.Licenses.FindAsync(id);
|
||||
|
||||
Reference in New Issue
Block a user