diff --git a/Marechai.Database/Models/CurrencyPegging.cs b/Marechai.Database/Models/CurrencyPegging.cs index 5caa9b35..ec2163bd 100644 --- a/Marechai.Database/Models/CurrencyPegging.cs +++ b/Marechai.Database/Models/CurrencyPegging.cs @@ -39,5 +39,8 @@ namespace Marechai.Database.Models public DateTime Start { get; set; } [DataType(DataType.Date)] public DateTime? End { get; set; } + + public string SourceCode { get; set; } + public string DestinationCode { get; set; } } } \ No newline at end of file diff --git a/Marechai/Marechai.csproj b/Marechai/Marechai.csproj index 8cf49d82..8552f77b 100644 --- a/Marechai/Marechai.csproj +++ b/Marechai/Marechai.csproj @@ -2,7 +2,7 @@ netcoreapp3.1 - 4.0.0.1800 + 4.0.0.1801 Canary Islands Computer Museum Copyright © 2003-2020 Natalia Portillo Canary Islands Computer Museum Website diff --git a/Marechai/Pages/Admin/CurrencyPegging.razor b/Marechai/Pages/Admin/CurrencyPegging.razor new file mode 100644 index 00000000..73358add --- /dev/null +++ b/Marechai/Pages/Admin/CurrencyPegging.razor @@ -0,0 +1,110 @@ +@{ +/****************************************************************************** +// 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 +*******************************************************************************/ +} + +@page "/admin/currency_pegging" +@using Marechai.Database.Models +@inherits OwningComponentBase +@inject IStringLocalizer L +@inject Microsoft.AspNetCore.Identity.UserManager UserManager +@inject AuthenticationStateProvider AuthenticationStateProvider +@attribute [Authorize(Roles = "UberAdmin, Admin")] +

@L["Currency pegging"]

+@if (_peggings is null) +{ +

@L["Loading..."]

+ + return; +} +

+ @L["Create new"] +

+ + + + + + + + + + + + + @foreach (var item in _peggings) + { + + + + + + + + + } + +
+ @L["Source currency"] + + @L["Destination currency"] + + @L["Starting date"] + + @L["Ending date"] + + @L["Pegging"] +
+ @item.SourceName + + @item.DestinationName + + @item.Start.ToLongDateString() + + @item.End?.ToLongDateString() + + @($"{item.Ratio:F2}") + + @L["Details"] + @L["Edit"] + +
+ + + + + + @L["Delete currency pegging"] + + + + @string.Format(@L["Are you sure you want to delete the pegging of {0:F} happened to currency {1} with currency {2} since {3}?"], _currentPegging?.Ratio, _currentPegging?.SourceName, _currentPegging?.DestinationName, _currentPegging?.Start.ToLongDateString()) + + + + + + + diff --git a/Marechai/Pages/Admin/CurrencyPegging.razor.cs b/Marechai/Pages/Admin/CurrencyPegging.razor.cs new file mode 100644 index 00000000..675a8ab6 --- /dev/null +++ b/Marechai/Pages/Admin/CurrencyPegging.razor.cs @@ -0,0 +1,88 @@ +/****************************************************************************** +// 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 Blazorise; +using Marechai.ViewModels; +using Microsoft.AspNetCore.Components.Authorization; + +namespace Marechai.Pages.Admin +{ + public partial class CurrencyPegging + { + CurrencyPeggingViewModel _currentPegging; + bool _deleteInProgress; + Modal _frmDelete; + bool _loaded; + List _peggings; + + protected override async Task OnAfterRenderAsync(bool firstRender) + { + if(_loaded) + return; + + _peggings = await Service.GetAsync(); + _loaded = true; + StateHasChanged(); + } + + void ShowModal(long itemId) + { + _currentPegging = _peggings.FirstOrDefault(n => n.Id == itemId); + _frmDelete.Show(); + } + + void HideModal() => _frmDelete.Hide(); + + async void ConfirmDelete() + { + if(_currentPegging is null) + return; + + _deleteInProgress = true; + _peggings = null; + AuthenticationState authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + + // Yield thread to let UI to update + await Task.Yield(); + + await Service.DeleteAsync(_currentPegging.Id, (await UserManager.GetUserAsync(authState.User)).Id); + _peggings = await Service.GetAsync(); + + _deleteInProgress = false; + _frmDelete.Hide(); + + // Yield thread to let UI to update + await Task.Yield(); + + // Tell we finished loading + StateHasChanged(); + } + + void ModalClosing(ModalClosingEventArgs obj) => _currentPegging = null; + } +} \ No newline at end of file diff --git a/Marechai/Pages/Admin/Details/CurrencyPegging.razor b/Marechai/Pages/Admin/Details/CurrencyPegging.razor new file mode 100644 index 00000000..7ee6f55a --- /dev/null +++ b/Marechai/Pages/Admin/Details/CurrencyPegging.razor @@ -0,0 +1,124 @@ +@{ +/****************************************************************************** +// 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 +*******************************************************************************/ +} + +@page "/admin/currency_pegging/details/{Id:int}" +@page "/admin/currency_pegging/edit/{Id:int}" +@page "/admin/currency_pegging/create" +@using Marechai.Database.Models +@inherits OwningComponentBase +@inject IStringLocalizer L +@inject Iso4217Service CurrenciesService +@inject NavigationManager NavigationManager +@inject IWebHostEnvironment Host +@inject Microsoft.AspNetCore.Identity.UserManager UserManager +@inject AuthenticationStateProvider AuthenticationStateProvider +@attribute [Authorize(Roles = "UberAdmin, Admin")] + + +

@L["Currency pegging details"]

+
+ +@if (!_loaded) +{ +

@L["Loading..."]

+ + return; +} + +
+ + @L["Source currency"] + + + + @L["Destination currency"] + + + + @L["Pegging (ratio)"] + + + + @L["Please enter a valid pegging."] + + + + + + @L["Start date"] + + + + @L["Please enter a start date."] + + + + + @if (_editing || _model.End.HasValue) + { + + @L["End date"] + @if (_editing) + { + @L["Unknown or never (end date)"] + } + @if (!_editing || + (!_unknownEnd)) + { + + + + @L["Please enter an ending date."] + + + + } + + } +
+
+ @if (!_editing) + { + + } + else + { + + + } + @L["Back to list"] +
diff --git a/Marechai/Pages/Admin/Details/CurrencyPegging.razor.cs b/Marechai/Pages/Admin/Details/CurrencyPegging.razor.cs new file mode 100644 index 00000000..c7841407 --- /dev/null +++ b/Marechai/Pages/Admin/Details/CurrencyPegging.razor.cs @@ -0,0 +1,183 @@ +/****************************************************************************** +// 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; +using System.Collections.Generic; +using System.Threading.Tasks; +using Blazorise; +using Marechai.Database.Models; +using Marechai.ViewModels; +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Authorization; + +namespace Marechai.Pages.Admin.Details +{ + public partial class CurrencyPegging + { + AuthenticationState _authState; + bool _creating; + List _currencies; + bool _editing; + bool _loaded; + CurrencyPeggingViewModel _model; + bool _unknownEnd; + + [Parameter] + public int Id { get; set; } + + protected override async Task OnAfterRenderAsync(bool firstRender) + { + if(_loaded) + return; + + _loaded = true; + + _creating = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). + StartsWith("admin/currency_pegging/create", + StringComparison.InvariantCulture); + + if(Id <= 0 && + !_creating) + return; + + _currencies = await CurrenciesService.GetAsync(); + _model = _creating ? new CurrencyPeggingViewModel() : await Service.GetAsync(Id); + _authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); + + _editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). + StartsWith("admin/currency_pegging/edit/", + StringComparison.InvariantCulture); + + if(_editing) + SetCheckboxes(); + + StateHasChanged(); + } + + void SetCheckboxes() => _unknownEnd = !_model.End.HasValue; + + void OnEditClicked() + { + _editing = true; + SetCheckboxes(); + StateHasChanged(); + } + + async void OnCancelClicked() + { + _editing = false; + + if(_creating) + { + NavigationManager.ToBaseRelativePath("admin/currency_pegging"); + + return; + } + + _model = await Service.GetAsync(Id); + SetCheckboxes(); + StateHasChanged(); + } + + async void OnSaveClicked() + { + if(string.IsNullOrWhiteSpace(_model.SourceCode)) + return; + + if(string.IsNullOrWhiteSpace(_model.DestinationCode)) + return; + + if(_model.Start >= DateTime.UtcNow) + return; + + if(_model.End >= DateTime.UtcNow || + _model.End < _model.Start) + return; + + if(_model.Ratio <= 0) + return; + + if(_creating) + Id = await Service.CreateAsync(_model, (await UserManager.GetUserAsync(_authState.User)).Id); + else + await Service.UpdateAsync(_model, (await UserManager.GetUserAsync(_authState.User)).Id); + + _editing = false; + _creating = false; + _model = await Service.GetAsync(Id); + SetCheckboxes(); + StateHasChanged(); + } + + void ValidateStart(ValidatorEventArgs e) + { + e.Status = ValidationStatus.Success; + + if((DateTime)e.Value <= DateTime.UtcNow) + return; + + e.Status = ValidationStatus.Error; + e.ErrorText = L["Starting date must be before now."]; + } + + void ValidatePegging(ValidatorEventArgs e) + { + e.Status = ValidationStatus.Success; + + if((float)e.Value > 0) + return; + + e.Status = ValidationStatus.Error; + e.ErrorText = L["Pegging must be bigger than 0."]; + } + + void ValidateEnd(ValidatorEventArgs e) + { + e.Status = ValidationStatus.Success; + + if(e.Value is null) + { + e.Status = ValidationStatus.Error; + e.ErrorText = L["Please enter an ending date."]; + + return; + } + + if((DateTime?)e.Value > DateTime.UtcNow) + { + e.Status = ValidationStatus.Error; + e.ErrorText = L["Ending date must be before now."]; + + return; + } + + if(!((DateTime?)e.Value < _model.Start)) + return; + + e.Status = ValidationStatus.Error; + e.ErrorText = L["Ending date must be before starting date."]; + } + } +} \ No newline at end of file diff --git a/Marechai/Pages/Admin/Index.razor b/Marechai/Pages/Admin/Index.razor index b48cb8d6..06ea3c89 100644 --- a/Marechai/Pages/Admin/Index.razor +++ b/Marechai/Pages/Admin/Index.razor @@ -106,5 +106,8 @@
  • @L["Currency inflation"]
  • +
  • + @L["Currency pegging"] +
  • diff --git a/Marechai/Resources/Services/AdminService.en.resx b/Marechai/Resources/Services/AdminService.en.resx index 02ce3d05..3d02d4eb 100644 --- a/Marechai/Resources/Services/AdminService.en.resx +++ b/Marechai/Resources/Services/AdminService.en.resx @@ -20,4 +20,7 @@ Currency inflation + + Currency pegging + \ No newline at end of file diff --git a/Marechai/Resources/Services/AdminService.es.resx b/Marechai/Resources/Services/AdminService.es.resx index 3c3e4b71..c5b98060 100644 --- a/Marechai/Resources/Services/AdminService.es.resx +++ b/Marechai/Resources/Services/AdminService.es.resx @@ -219,4 +219,7 @@ Inflación de divisa + + Vinculación de divisa + \ No newline at end of file diff --git a/Marechai/Resources/Services/CurrencyPeggingService.en.resx b/Marechai/Resources/Services/CurrencyPeggingService.en.resx new file mode 100644 index 00000000..a92b656f --- /dev/null +++ b/Marechai/Resources/Services/CurrencyPeggingService.en.resx @@ -0,0 +1,101 @@ + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Currency pegging + + + Loading... + + + Create new + + + Source currency + + + Destination currency + + + Starting date + + + Ending date + + + Pegging + + + Details + + + Edit + + + Delete + + + Delete currency pegging + + + Are you sure you want to delete the pegging of {0:F} happened to currency {1} with currency {2} since {3}? + + + Cancel + + + Currency pegging details + + + Pegging (ratio) + + + Please enter a valid pegging. + + + Start date + + + Please enter a start date. + + + End date + + + Unknown or never + + + Please enter an ending date. + + + Save + + + Back to list + + + Starting date must be before now. + + + Pegging must be bigger than 0. + + + Please enter an ending date. + + + Ending date must be before now. + + + Ending date must be before starting date. + + \ No newline at end of file diff --git a/Marechai/Resources/Services/CurrencyPeggingService.es.resx b/Marechai/Resources/Services/CurrencyPeggingService.es.resx new file mode 100644 index 00000000..34ab58d3 --- /dev/null +++ b/Marechai/Resources/Services/CurrencyPeggingService.es.resx @@ -0,0 +1,205 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Vinculación de divisa + + + Cargando... + + + Crear nueva + + + Divisa de origen + + + Divisa de destino + + + Fecha de comienzo + + + Fecha de finalización + + + Vinculación + + + Detalles + + + Editar + + + Eliminar + + + Eliminar vinculación de divisa + + + ¿Estás seguro de querer eliminar la vinculación de divisa de {0:F} en la divisa {1} con la divisa {2} desde {3}? + + + Cancelar + + + Detalles de vinculación de divisa + + + Vinculación (relación) + + + Por favor introduce una vinculación válida. + + + Fecha de comienzo + + + Por favor introduce una fecha de comienzo válida. + + + Fecha de finalización + + + Desconocida o nunca + + + Por favor introduce una fecha de finalización. + + + Guardar + + + Volver a la lista + + + La fecha de comienzo debe ser anterior a ahora. + + + La vinculación debe ser mayor de 0. + + + La fecha de finalización debe ser anterior a ahora. + + + La fecha de finalización debe ser anterior a la de comienzo. + + \ No newline at end of file diff --git a/Marechai/Services/CurrencyPeggingService.cs b/Marechai/Services/CurrencyPeggingService.cs new file mode 100644 index 00000000..2b6e120b --- /dev/null +++ b/Marechai/Services/CurrencyPeggingService.cs @@ -0,0 +1,115 @@ +/****************************************************************************** +// 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 CurrencyPeggingService + { + readonly MarechaiContext _context; + + public CurrencyPeggingService(MarechaiContext context) => _context = context; + + public async Task> GetAsync() => await _context. + CurrenciesPegging. + OrderBy(i => i.Source.Name). + ThenBy(i => i.Destination.Name). + ThenBy(i => i.Start).ThenBy(i => i.End). + Select(i => new CurrencyPeggingViewModel + { + Id = i.Id, + SourceCode = i.Source.Code, + SourceName = i.Source.Name, + DestinationCode = i.Source.Code, + DestinationName = i.Source.Name, + Ratio = i.Ratio, + Start = i.Start, + End = i.End + }).ToListAsync(); + + public async Task GetAsync(int id) => + await _context.CurrenciesPegging.Where(b => b.Id == id).Select(i => new CurrencyPeggingViewModel + { + Id = i.Id, + SourceCode = i.Source.Code, + SourceName = i.Source.Name, + DestinationCode = i.Destination.Code, + DestinationName = i.Destination.Name, + Ratio = i.Ratio, + Start = i.Start, + End = i.End + }).FirstOrDefaultAsync(); + + public async Task UpdateAsync(CurrencyPeggingViewModel viewModel, string userId) + { + CurrencyPegging model = await _context.CurrenciesPegging.FindAsync(viewModel.Id); + + if(model is null) + return; + + model.SourceCode = viewModel.SourceCode; + model.DestinationCode = viewModel.DestinationCode; + model.Ratio = viewModel.Ratio; + model.Start = viewModel.Start; + model.End = viewModel.End; + await _context.SaveChangesWithUserAsync(userId); + } + + public async Task CreateAsync(CurrencyPeggingViewModel viewModel, string userId) + { + var model = new CurrencyPegging + { + SourceCode = viewModel.SourceCode, + DestinationCode = viewModel.DestinationCode, + Ratio = viewModel.Ratio, + Start = viewModel.Start, + End = viewModel.End + }; + + await _context.CurrenciesPegging.AddAsync(model); + await _context.SaveChangesWithUserAsync(userId); + + return model.Id; + } + + public async Task DeleteAsync(int id, string userId) + { + CurrencyPegging item = await _context.CurrenciesPegging.FindAsync(id); + + if(item is null) + return; + + _context.CurrenciesPegging.Remove(item); + + await _context.SaveChangesWithUserAsync(userId); + } + } +} \ No newline at end of file diff --git a/Marechai/Services/Register.cs b/Marechai/Services/Register.cs index 573c731a..9505611a 100644 --- a/Marechai/Services/Register.cs +++ b/Marechai/Services/Register.cs @@ -70,6 +70,7 @@ namespace Marechai.Services services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); } } } \ No newline at end of file diff --git a/Marechai/ViewModels/CurrencyPeggingViewModel.cs b/Marechai/ViewModels/CurrencyPeggingViewModel.cs new file mode 100644 index 00000000..0665512f --- /dev/null +++ b/Marechai/ViewModels/CurrencyPeggingViewModel.cs @@ -0,0 +1,40 @@ +/****************************************************************************** +// 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; + +namespace Marechai.ViewModels +{ + public class CurrencyPeggingViewModel : BaseViewModel + { + public string SourceCode { get; set; } + public string DestinationCode { get; set; } + public string SourceName { get; set; } + public string DestinationName { get; set; } + public float Ratio { get; set; } + public DateTime Start { get; set; } + public DateTime? End { get; set; } + } +} \ No newline at end of file