From cb6cde600db61d7dad63ed0eee31b6cf33802cfc Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Thu, 28 May 2020 02:44:39 +0100 Subject: [PATCH] Add license creation in admin view. --- .../Admin/Controllers/LicensesController.cs | 127 ------------------ .../Areas/Admin/Views/Licenses/Create.cshtml | 64 --------- .../Areas/Admin/Views/Licenses/Edit.cshtml | 65 --------- Marechai/Marechai.csproj | 2 + Marechai/Pages/Admin/Details/License.razor | 1 + Marechai/Pages/Admin/Details/License.razor.cs | 35 +++-- Marechai/Pages/Admin/Licenses.razor | 4 +- Marechai/Services/LicensesService.cs | 14 ++ 8 files changed, 45 insertions(+), 267 deletions(-) delete mode 100644 Marechai/Areas/Admin/Controllers/LicensesController.cs delete mode 100644 Marechai/Areas/Admin/Views/Licenses/Create.cshtml delete mode 100644 Marechai/Areas/Admin/Views/Licenses/Edit.cshtml diff --git a/Marechai/Areas/Admin/Controllers/LicensesController.cs b/Marechai/Areas/Admin/Controllers/LicensesController.cs deleted file mode 100644 index ddda59c9..00000000 --- a/Marechai/Areas/Admin/Controllers/LicensesController.cs +++ /dev/null @@ -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 Index() => View(await _context.Licenses.OrderBy(l => l.Name).ToListAsync()); - - // GET: Licenses/Details/5 - public async Task 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 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 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 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 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 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); - } -} \ No newline at end of file diff --git a/Marechai/Areas/Admin/Views/Licenses/Create.cshtml b/Marechai/Areas/Admin/Views/Licenses/Create.cshtml deleted file mode 100644 index b026693b..00000000 --- a/Marechai/Areas/Admin/Views/Licenses/Create.cshtml +++ /dev/null @@ -1,64 +0,0 @@ -@model Marechai.Database.Models.License - -@{ - ViewData["Title"] = "Create"; -} -

Create

-

License

-
-
-
-
-
-
-
- - - - -
-
- - - - -
-
- -
-
- -
-
- - - - -
-
- - - - -
- -
-
-
- -@section Scripts { - @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } -} \ No newline at end of file diff --git a/Marechai/Areas/Admin/Views/Licenses/Edit.cshtml b/Marechai/Areas/Admin/Views/Licenses/Edit.cshtml deleted file mode 100644 index 5455eced..00000000 --- a/Marechai/Areas/Admin/Views/Licenses/Edit.cshtml +++ /dev/null @@ -1,65 +0,0 @@ -@model Marechai.Database.Models.License - -@{ - ViewData["Title"] = "Edit"; -} -

Edit

-

License

-
-
-
-
-
-
-
- - - - -
-
- - - - -
-
- -
-
- -
-
- - - - -
-
- - - - -
- - -
-
-
- -@section Scripts { - @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } -} \ No newline at end of file diff --git a/Marechai/Marechai.csproj b/Marechai/Marechai.csproj index 9463c1ba..64f2249b 100644 --- a/Marechai/Marechai.csproj +++ b/Marechai/Marechai.csproj @@ -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" /> \ No newline at end of file diff --git a/Marechai/Pages/Admin/Details/License.razor b/Marechai/Pages/Admin/Details/License.razor index 4e7d7195..014c4a0b 100644 --- a/Marechai/Pages/Admin/Details/License.razor +++ b/Marechai/Pages/Admin/Details/License.razor @@ -32,6 +32,7 @@ @page "/admin/licenses/details/{Id:int}" @page "/admin/licenses/edit/{Id:int}" +@page "/admin/licenses/create" @inherits OwningComponentBase @inject IStringLocalizer L @inject NavigationManager NavigationManager diff --git a/Marechai/Pages/Admin/Details/License.razor.cs b/Marechai/Pages/Admin/Details/License.razor.cs index ca28a6e5..f01f239c 100644 --- a/Marechai/Pages/Admin/Details/License.razor.cs +++ b/Marechai/Pages/Admin/Details/License.razor.cs @@ -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,7 +59,15 @@ namespace Marechai.Pages.Admin.Details async void OnCancelClicked() { _editing = false; - _model = await Service.GetAsync(Id); + + if(_creating) + { + NavigationManager.ToBaseRelativePath("admin/licenses"); + + return; + } + + _model = await Service.GetAsync(Id); SetCheckboxes(); StateHasChanged(); } @@ -79,9 +93,14 @@ namespace Marechai.Pages.Admin.Details _model.Link?.Length > 512) 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(); } diff --git a/Marechai/Pages/Admin/Licenses.razor b/Marechai/Pages/Admin/Licenses.razor index 173ba3d6..4e2b1936 100644 --- a/Marechai/Pages/Admin/Licenses.razor +++ b/Marechai/Pages/Admin/Licenses.razor @@ -42,9 +42,7 @@ return; }

- - @L["Create new"] - + @L["Create new"]

diff --git a/Marechai/Services/LicensesService.cs b/Marechai/Services/LicensesService.cs index 24ff1e1d..fc9c664e 100644 --- a/Marechai/Services/LicensesService.cs +++ b/Marechai/Services/LicensesService.cs @@ -43,6 +43,20 @@ namespace Marechai.Services await _context.SaveChangesAsync(); } + public async Task 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);