diff --git a/Marechai/Areas/Admin/Views/CompanyLogos/Delete.cshtml b/Marechai/Areas/Admin/Views/CompanyLogos/Delete.cshtml deleted file mode 100644 index 09f5cd39..00000000 --- a/Marechai/Areas/Admin/Views/CompanyLogos/Delete.cshtml +++ /dev/null @@ -1,76 +0,0 @@ -@model Marechai.Database.Models.CompanyLogo - -@{ - ViewData["Title"] = "Delete"; -} -

Delete

-

Are you sure you want to delete this?

-
-

Company logo

-
-
-
- @Html.DisplayNameFor(model => model.Company) -
-
- @Html.DisplayFor(model => model.Company.Name) -
-
- @Html.DisplayNameFor(model => model.Year) -
-
- @Html.DisplayFor(model => model.Year) -
-
-
- - - - - - Back to List - -
- SVG -
-
- -
-
- PNG 1x -
-
- -
-
- PNG 2x -
-
- -
-
- PNG 3x -
-
- -
-
- WebP 1x -
-
- -
-
- WebP 2x -
-
- -
-
- WebP 3x -
-
- -
-
-
\ No newline at end of file diff --git a/Marechai/Areas/Admin/Views/CompanyLogos/Details.cshtml b/Marechai/Areas/Admin/Views/CompanyLogos/Details.cshtml deleted file mode 100644 index c608a0fe..00000000 --- a/Marechai/Areas/Admin/Views/CompanyLogos/Details.cshtml +++ /dev/null @@ -1,74 +0,0 @@ -@model Marechai.Database.Models.CompanyLogo - -@{ - ViewData["Title"] = "Details"; -} -

Details

-
- - Edit - - - Back to List - -
-
-

Company logo

-
-
-
- @Html.DisplayNameFor(model => model.Company) -
-
- @Html.DisplayFor(model => model.Company.Name) -
-
- @Html.DisplayNameFor(model => model.Year) -
-
- @Html.DisplayFor(model => model.Year) -
-
- SVG -
-
- -
-
- PNG 1x -
-
- -
-
- PNG 2x -
-
- -
-
- PNG 3x -
-
- -
-
- WebP 1x -
-
- -
-
- WebP 2x -
-
- -
-
- WebP 3x -
-
- -
-
-
\ No newline at end of file diff --git a/Marechai/Areas/Admin/Views/CompanyLogos/Index.cshtml b/Marechai/Areas/Admin/Views/CompanyLogos/Index.cshtml deleted file mode 100644 index fbbf99b7..00000000 --- a/Marechai/Areas/Admin/Views/CompanyLogos/Index.cshtml +++ /dev/null @@ -1,48 +0,0 @@ -@model IEnumerable - -@{ - ViewData["Title"] = "Index"; -} -

Company logos

-

- - Create new - -

- - - - - - - - - - @foreach (var item in Model) - { - - - - - - } - -
- @Html.DisplayNameFor(model => model.Company) - - @Html.DisplayNameFor(model => model.Year) -
- @Html.DisplayFor(modelItem => item.Company) - - @Html.DisplayFor(modelItem => item.Year) - - - Details - - - Edit - - - Delete - -
\ No newline at end of file diff --git a/Marechai/Marechai.csproj b/Marechai/Marechai.csproj index 9dcc9965..979233f8 100644 --- a/Marechai/Marechai.csproj +++ b/Marechai/Marechai.csproj @@ -2,7 +2,7 @@ netcoreapp3.1 - 3.0.99.1253 + 3.0.99.1258 Canary Islands Computer Museum Copyright © 2003-2020 Natalia Portillo Canary Islands Computer Museum Website diff --git a/Marechai/Pages/Admin/Details/Company.razor b/Marechai/Pages/Admin/Details/Company.razor index 4beba0f9..2d9f7c4a 100644 --- a/Marechai/Pages/Admin/Details/Company.razor +++ b/Marechai/Pages/Admin/Details/Company.razor @@ -38,6 +38,8 @@ @inject IStringLocalizer L @inject Iso31661NumericService CountriesService @inject NavigationManager NavigationManager +@inject CompanyLogosService CompanyLogosService +@inject IWebHostEnvironment Host @attribute [Authorize(Roles = "UberAdmin, Admin")]

@L["Company details"]


@@ -312,4 +314,78 @@ } @L["Back to list"] - \ No newline at end of file +@if (!_editing) +{ + @L["Upload new logo"] +} + +@if (_logos.Count > 0) +{ +

@L["Logos"]

+ + + + + + + + + + @foreach (var logo in _logos) + { + + + + + + } + +
+ @L["Logo"] + + @L["Year logo came in use"] +
+ @if (File.Exists(Path.Combine(Host.WebRootPath, "assets/logos", logo.Guid + ".svg"))) + { + + + + + + } + else + { + @L["Cannot find logo file"] + } + + @if (logo.Year.HasValue) + { + @logo.Year + } + else + { + @L["Unknown (logo year)"] + } + + @L["Change year"] + +
+} + + + + + + @L["Delete logo"] + + + + @string.Format(@L["Are you sure you want to delete the company logo introduced in {0}?"], _currentLogo?.Year) + + + + + + + diff --git a/Marechai/Pages/Admin/Details/Company.razor.cs b/Marechai/Pages/Admin/Details/Company.razor.cs index be02e4c0..43baa7d8 100644 --- a/Marechai/Pages/Admin/Details/Company.razor.cs +++ b/Marechai/Pages/Admin/Details/Company.razor.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using Blazorise; using Marechai.Database; @@ -15,8 +16,12 @@ namespace Marechai.Pages.Admin.Details List _companies; List _countries; bool _creating; + CompanyLogo _currentLogo; + bool _deleteInProgress; bool _editing; + Modal _frmDelete; bool _loaded; + List _logos; CompanyViewModel _model; bool _unknownAddress; bool _unknownCity; @@ -48,12 +53,14 @@ namespace Marechai.Pages.Admin.Details _creating = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). StartsWith("admin/companies/create", StringComparison.InvariantCulture); - if(Id <= 0 && !_creating) + if(Id <= 0 && + !_creating) return; _countries = await CountriesService.GetAsync(); _companies = await Service.GetAsync(); _model = _creating ? new CompanyViewModel() : await Service.GetAsync(Id); + _logos = await CompanyLogosService.GetByCompany(Id); _editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant(). StartsWith("admin/companies/edit/", @@ -229,5 +236,38 @@ namespace Marechai.Pages.Admin.Details void ValidateFacebook(ValidatorEventArgs e) => Validators.ValidateString(e, L["Facebook username must be smaller than 256 characters."], 256); + + void ShowModal(int itemId) + { + _currentLogo = _logos.FirstOrDefault(n => n.Id == itemId); + _frmDelete.Show(); + } + + void HideModal() => _frmDelete.Hide(); + + async void ConfirmDelete() + { + if(_currentLogo is null) + return; + + _deleteInProgress = true; + + // Yield thread to let UI to update + await Task.Yield(); + + await CompanyLogosService.DeleteAsync(_currentLogo.Id); + _logos = await CompanyLogosService.GetByCompany(Id); + + _deleteInProgress = false; + _frmDelete.Hide(); + + // Yield thread to let UI to update + await Task.Yield(); + + // Tell we finished loading + StateHasChanged(); + } + + void ModalClosing(ModalClosingEventArgs obj) => _currentLogo = null; } } \ No newline at end of file diff --git a/Marechai/Services/CompanyLogosService.cs b/Marechai/Services/CompanyLogosService.cs index b94ab064..43e4ceaa 100644 --- a/Marechai/Services/CompanyLogosService.cs +++ b/Marechai/Services/CompanyLogosService.cs @@ -1,20 +1,77 @@ using System.Collections.Generic; +using System.IO; using System.Linq; using System.Threading.Tasks; using Marechai.Database.Models; +using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; namespace Marechai.Services { public class CompanyLogosService { - readonly MarechaiContext _context; + readonly MarechaiContext _context; + readonly IWebHostEnvironment _host; + readonly string _webRootPath; - public CompanyLogosService(MarechaiContext context) + public CompanyLogosService(MarechaiContext context, IWebHostEnvironment host) { - _context = context; + _context = context; + _host = host; + _webRootPath = host.WebRootPath; } - public async Task> GetByCompany(int companyId) => await _context.CompanyLogos.Where(l => l.CompanyId == companyId).OrderBy(l => l.Year).ToListAsync(); + public async Task> GetByCompany(int companyId) => + await _context.CompanyLogos.Where(l => l.CompanyId == companyId).OrderBy(l => l.Year).ToListAsync(); + + public async Task DeleteAsync(int id) + { + CompanyLogo logo = await _context.CompanyLogos.Where(l => l.Id == id).FirstOrDefaultAsync(); + + if(logo is null) + return; + + _context.CompanyLogos.Remove(logo); + await _context.SaveChangesAsync(); + + if(File.Exists(Path.Combine(_webRootPath, "assets/logos", logo.Guid + ".svg"))) + File.Delete(Path.Combine(_webRootPath, "assets/logos", logo.Guid + ".svg")); + + if(File.Exists(Path.Combine(_webRootPath, "assets/logos/webp/1x", logo.Guid + ".webp"))) + File.Delete(Path.Combine(_webRootPath, "assets/logos/webp/1x", logo.Guid + ".webp")); + + if(File.Exists(Path.Combine(_webRootPath, "assets/logos/webp/2x", logo.Guid + ".webp"))) + File.Delete(Path.Combine(_webRootPath, "assets/logos/webp/2x", logo.Guid + ".webp")); + + if(File.Exists(Path.Combine(_webRootPath, "assets/logos/webp/3x", logo.Guid + ".webp"))) + File.Delete(Path.Combine(_webRootPath, "assets/logos/webp/3x", logo.Guid + ".webp")); + + if(File.Exists(Path.Combine(_webRootPath, "assets/logos/png/1x", logo.Guid + ".png"))) + File.Delete(Path.Combine(_webRootPath, "assets/logos/png/1x", logo.Guid + ".png")); + + if(File.Exists(Path.Combine(_webRootPath, "assets/logos/png/2x", logo.Guid + ".png"))) + File.Delete(Path.Combine(_webRootPath, "assets/logos/png/2x", logo.Guid + ".png")); + + if(File.Exists(Path.Combine(_webRootPath, "assets/logos/png/3x", logo.Guid + ".png"))) + File.Delete(Path.Combine(_webRootPath, "assets/logos/png/3x", logo.Guid + ".png")); + + if(File.Exists(Path.Combine(_webRootPath, "assets/logos/thumbs/webp/1x", logo.Guid + ".webp"))) + File.Delete(Path.Combine(_webRootPath, "assets/logos/thumbs/webp/1x", logo.Guid + ".webp")); + + if(File.Exists(Path.Combine(_webRootPath, "assets/logos/thumbs/webp/2x", logo.Guid + ".webp"))) + File.Delete(Path.Combine(_webRootPath, "assets/logos/thumbs/webp/2x", logo.Guid + ".webp")); + + if(File.Exists(Path.Combine(_webRootPath, "assets/logos/thumbs/webp/3x", logo.Guid + ".webp"))) + File.Delete(Path.Combine(_webRootPath, "assets/logos/thumbs/webp/3x", logo.Guid + ".webp")); + + if(File.Exists(Path.Combine(_webRootPath, "assets/logos/thumbs/png/1x", logo.Guid + ".png"))) + File.Delete(Path.Combine(_webRootPath, "assets/logos/thumbs/png/1x", logo.Guid + ".png")); + + if(File.Exists(Path.Combine(_webRootPath, "assets/logos/thumbs/png/2x", logo.Guid + ".png"))) + File.Delete(Path.Combine(_webRootPath, "assets/logos/thumbs/png/2x", logo.Guid + ".png")); + + if(File.Exists(Path.Combine(_webRootPath, "assets/logos/thumbs/png/3x", logo.Guid + ".png"))) + File.Delete(Path.Combine(_webRootPath, "assets/logos/thumbs/png/3x", logo.Guid + ".png")); + } } } \ No newline at end of file