From 139618f41f11e2bf311d9bf1d4925442a727e948 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sun, 9 Aug 2020 20:19:18 +0100 Subject: [PATCH] Add machine families to magazine issue admin page. --- .../Pages/Admin/Details/MagazineIssue.razor | 69 +++++++++ .../Admin/Details/MagazineIssue.razor.cs | 141 ++++++++++++++++-- .../Resources/Services/MagazineIssues.en.resx | 9 ++ .../Resources/Services/MagazineIssues.es.resx | 9 ++ .../MagazinesByMachineFamilyService.cs | 77 ++++++++++ Marechai/Services/Register.cs | 1 + .../MagazinesByMachineFamilyViewModel.cs | 35 +++++ 7 files changed, 327 insertions(+), 14 deletions(-) create mode 100644 Marechai/Services/MagazinesByMachineFamilyService.cs create mode 100644 Marechai/ViewModels/MagazinesByMachineFamilyViewModel.cs diff --git a/Marechai/Pages/Admin/Details/MagazineIssue.razor b/Marechai/Pages/Admin/Details/MagazineIssue.razor index d6268f17..c69a15ba 100644 --- a/Marechai/Pages/Admin/Details/MagazineIssue.razor +++ b/Marechai/Pages/Admin/Details/MagazineIssue.razor @@ -33,6 +33,8 @@ @inherits OwningComponentBase @inject IStringLocalizer L @inject MagazinesService MagazinesService +@inject MachineFamiliesService MachineFamiliesService +@inject MagazinesByMachineFamilyService MagazinesByMachineFamilyService @inject NavigationManager NavigationManager @inject IWebHostEnvironment Host @inject IJSRuntime JSRuntime @@ -189,3 +191,70 @@ } @L["Back to list"] +@if (!_editing) +{ +
+

@L["Machine families this magazine issue talks about"]

+ + @if (_addingMachineFamily) + { +
+ + @L["Family"] + + + + +
+ } + @if (_magazineMachineFamilies?.Count > 0) + { +
+ + + + + + + + + @foreach (var item in _magazineMachineFamilies) + { + + + + + } + +
+ @L["Family"] +
+ @item.MachineFamily + + +
+
+ } + + + + + + @_deleteTitle + + + + @_deleteText + + + + + + + +} \ No newline at end of file diff --git a/Marechai/Pages/Admin/Details/MagazineIssue.razor.cs b/Marechai/Pages/Admin/Details/MagazineIssue.razor.cs index c2c3bb92..6e2be447 100644 --- a/Marechai/Pages/Admin/Details/MagazineIssue.razor.cs +++ b/Marechai/Pages/Admin/Details/MagazineIssue.razor.cs @@ -25,6 +25,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using Blazorise; using Marechai.Shared; @@ -36,17 +37,28 @@ namespace Marechai.Pages.Admin.Details { public partial class MagazineIssue { - AuthenticationState _authState; - bool _creating; - bool _editing; - bool _loaded; - List _magazines; - MagazineIssueViewModel _model; - bool _unknownIssueNumber; - bool _unknownNativeCaption; - bool _unknownPages; - bool _unknownProductCode; - bool _unknownPublished; + bool _addingMachineFamily; + int? _addingMachineFamilyId; + AuthenticationState _authState; + bool _creating; + MagazineByMachineFamilyViewModel _currentMagazineByMachineFamily; + bool _deleteInProgress; + string _deleteText; + string _deleteTitle; + bool _deletingMagazineByMachineFamily; + bool _editing; + Modal _frmDelete; + bool _loaded; + List _machineFamilies; + List _magazineMachineFamilies; + List _magazines; + MagazineIssueViewModel _model; + bool _savingMachineFamily; + bool _unknownIssueNumber; + bool _unknownNativeCaption; + bool _unknownPages; + bool _unknownProductCode; + bool _unknownPublished; [Parameter] public long Id { get; set; } @@ -65,9 +77,12 @@ namespace Marechai.Pages.Admin.Details !_creating) return; - _magazines = await MagazinesService.GetTitlesAsync(); - _model = _creating ? new MagazineIssueViewModel() : await Service.GetAsync(Id); - _authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + _magazines = await MagazinesService.GetTitlesAsync(); + _machineFamilies = await MachineFamiliesService.GetAsync(); + _model = _creating ? new MagazineIssueViewModel() : await Service.GetAsync(Id); + _authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + _addingMachineFamilyId = _machineFamilies.First().Id; + _magazineMachineFamilies = await MagazinesByMachineFamilyService.GetByMagazine(Id); _editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). StartsWith("admin/magazine_issues/edit/", @@ -168,5 +183,103 @@ namespace Marechai.Pages.Admin.Details void ValidateProductCode(ValidatorEventArgs e) => Validators.ValidateString(e, L["Product code must be smaller than 18 characters."], 18); + + void ModalClosing(ModalClosingEventArgs obj) + { + _deleteInProgress = false; + _deletingMagazineByMachineFamily = false; + _currentMagazineByMachineFamily = null; + } + + void HideModal() => _frmDelete.Hide(); + + async void ConfirmDelete() + { + if(_deletingMagazineByMachineFamily) + await ConfirmDeleteMagazineByMachineFamily(); + } + + void OnAddFamilyClick() + { + _addingMachineFamily = true; + _savingMachineFamily = false; + _addingMachineFamilyId = _machineFamilies.First().Id; + } + + void CancelAddFamily() + { + _addingMachineFamily = false; + _savingMachineFamily = false; + _addingMachineFamilyId = null; + } + + async Task ConfirmAddFamily() + { + if(_addingMachineFamilyId is null || + _addingMachineFamilyId <= 0) + { + CancelAddFamily(); + + return; + } + + _savingMachineFamily = true; + + // Yield thread to let UI to update + await Task.Yield(); + + await MagazinesByMachineFamilyService.CreateAsync(_addingMachineFamilyId.Value, Id, + (await UserManager.GetUserAsync(_authState.User)).Id); + + _magazineMachineFamilies = await MagazinesByMachineFamilyService.GetByMagazine(Id); + + _addingMachineFamily = false; + _savingMachineFamily = false; + _addingMachineFamilyId = null; + + // Yield thread to let UI to update + await Task.Yield(); + + // Tell we finished loading + StateHasChanged(); + } + + void ShowMachineFamilyDeleteModal(long itemId) + { + _currentMagazineByMachineFamily = _magazineMachineFamilies.FirstOrDefault(n => n.Id == itemId); + _deletingMagazineByMachineFamily = true; + _deleteTitle = L["Delete machine family from this magazine"]; + + _deleteText = + string.Format(L["Are you sure you want to delete the machine family {0} from this magazine issue?"], + _currentMagazineByMachineFamily?.MachineFamily); + + _frmDelete.Show(); + } + + async Task ConfirmDeleteMagazineByMachineFamily() + { + if(_currentMagazineByMachineFamily is null) + return; + + _deleteInProgress = true; + + // Yield thread to let UI to update + await Task.Yield(); + + await MagazinesByMachineFamilyService.DeleteAsync(_currentMagazineByMachineFamily.Id, + (await UserManager.GetUserAsync(_authState.User)).Id); + + _magazineMachineFamilies = await MagazinesByMachineFamilyService.GetByMagazine(Id); + + _deleteInProgress = false; + _frmDelete.Hide(); + + // Yield thread to let UI to update + await Task.Yield(); + + // Tell we finished loading + StateHasChanged(); + } } } \ No newline at end of file diff --git a/Marechai/Resources/Services/MagazineIssues.en.resx b/Marechai/Resources/Services/MagazineIssues.en.resx index c10e611f..fdbfd220 100644 --- a/Marechai/Resources/Services/MagazineIssues.en.resx +++ b/Marechai/Resources/Services/MagazineIssues.en.resx @@ -119,4 +119,13 @@ Native caption must be smaller than 256 characters. + + Machine families this magazine issue talks about + + + Add new + + + Family + \ No newline at end of file diff --git a/Marechai/Resources/Services/MagazineIssues.es.resx b/Marechai/Resources/Services/MagazineIssues.es.resx index f95c633f..1f5f96b1 100644 --- a/Marechai/Resources/Services/MagazineIssues.es.resx +++ b/Marechai/Resources/Services/MagazineIssues.es.resx @@ -226,4 +226,13 @@ El código de producto debe contener menos de 18 caracteres. + + Familias de máquinas sobre las que habla este número de revista + + + Añadir nueva + + + Familia + \ No newline at end of file diff --git a/Marechai/Services/MagazinesByMachineFamilyService.cs b/Marechai/Services/MagazinesByMachineFamilyService.cs new file mode 100644 index 00000000..2ad82e35 --- /dev/null +++ b/Marechai/Services/MagazinesByMachineFamilyService.cs @@ -0,0 +1,77 @@ +/****************************************************************************** +// MARECHAI: Master repository of computing history artifacts information +// ---------------------------------------------------------------------------- +// +// Author(s) : Natalia Portillo +// +// --[ License ] -------------------------------------------------------------- +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// ---------------------------------------------------------------------------- +// Copyright © 2003-2020 Natalia Portillo +*******************************************************************************/ + +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Marechai.Database.Models; +using Marechai.ViewModels; +using Microsoft.EntityFrameworkCore; + +namespace Marechai.Services +{ + public class MagazinesByMachineFamilyService + { + readonly MarechaiContext _context; + + public MagazinesByMachineFamilyService(MarechaiContext context) => _context = context; + + public async Task> GetByMagazine(long bookId) => + await _context.MagazinesByMachinesFamilies.Where(p => p.MagazineId == bookId). + Select(p => new MagazineByMachineFamilyViewModel + { + Id = p.Id, + MagazineId = p.MagazineId, + MachineFamilyId = p.MachineFamilyId, + MachineFamily = p.MachineFamily.Name + }).OrderBy(p => p.MachineFamily).ThenBy(p => p.Magazine).ToListAsync(); + + public async Task DeleteAsync(long id, string userId) + { + MagazinesByMachineFamily item = await _context.MagazinesByMachinesFamilies.FindAsync(id); + + if(item is null) + return; + + _context.MagazinesByMachinesFamilies.Remove(item); + + await _context.SaveChangesWithUserAsync(userId); + } + + public async Task CreateAsync(int machineFamilyId, long bookId, string userId) + { + var item = new MagazinesByMachineFamily + { + MachineFamilyId = machineFamilyId, + MagazineId = bookId + }; + + await _context.MagazinesByMachinesFamilies.AddAsync(item); + await _context.SaveChangesWithUserAsync(userId); + + return item.Id; + } + } +} \ No newline at end of file diff --git a/Marechai/Services/Register.cs b/Marechai/Services/Register.cs index 4c491d85..f3d210db 100644 --- a/Marechai/Services/Register.cs +++ b/Marechai/Services/Register.cs @@ -87,6 +87,7 @@ namespace Marechai.Services services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); } } } \ No newline at end of file diff --git a/Marechai/ViewModels/MagazinesByMachineFamilyViewModel.cs b/Marechai/ViewModels/MagazinesByMachineFamilyViewModel.cs new file mode 100644 index 00000000..f652f4f3 --- /dev/null +++ b/Marechai/ViewModels/MagazinesByMachineFamilyViewModel.cs @@ -0,0 +1,35 @@ +/****************************************************************************** +// MARECHAI: Master repository of computing history artifacts information +// ---------------------------------------------------------------------------- +// +// Author(s) : Natalia Portillo +// +// --[ License ] -------------------------------------------------------------- +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// ---------------------------------------------------------------------------- +// Copyright © 2003-2020 Natalia Portillo +*******************************************************************************/ + +namespace Marechai.ViewModels +{ + public class MagazineByMachineFamilyViewModel : BaseViewModel + { + public long MagazineId { get; set; } + public string Magazine { get; set; } + public int MachineFamilyId { get; set; } + public string MachineFamily { get; set; } + } +} \ No newline at end of file