mirror of
https://github.com/claunia/marechai.git
synced 2025-12-16 19:14:25 +00:00
Add code to display and delete company logos in admin view.
This commit is contained in:
@@ -1,76 +0,0 @@
|
||||
@model Marechai.Database.Models.CompanyLogo
|
||||
|
||||
@{
|
||||
ViewData["Title"] = "Delete";
|
||||
}
|
||||
<h1>Delete</h1>
|
||||
<h3>Are you sure you want to delete this?</h3>
|
||||
<div>
|
||||
<h4>Company logo</h4>
|
||||
<hr />
|
||||
<dl class="row">
|
||||
<dt class="col-sm-2">
|
||||
@Html.DisplayNameFor(model => model.Company)
|
||||
</dt>
|
||||
<dd class="col-sm-10">
|
||||
@Html.DisplayFor(model => model.Company.Name)
|
||||
</dd>
|
||||
<dt class="col-sm-2">
|
||||
@Html.DisplayNameFor(model => model.Year)
|
||||
</dt>
|
||||
<dd class="col-sm-10">
|
||||
@Html.DisplayFor(model => model.Year)
|
||||
</dd>
|
||||
</dl>
|
||||
<form asp-action="Delete">
|
||||
<input type="hidden" asp-for="Id" />
|
||||
<input type="hidden" asp-for="CompanyId" />
|
||||
<input type="hidden" asp-for="Guid" />
|
||||
<input class="btn btn-danger" type="submit" value="Delete" />
|
||||
<a asp-action="Index" class="btn btn-secondary">
|
||||
Back to List
|
||||
</a>
|
||||
<dt class="col-sm-2">
|
||||
SVG
|
||||
</dt>
|
||||
<dd class="col-sm-10">
|
||||
<img src="/assets/logos/@(Model.Guid).svg" />
|
||||
</dd>
|
||||
<dt class="col-sm-2">
|
||||
PNG 1x
|
||||
</dt>
|
||||
<dd class="col-sm-10">
|
||||
<img src="/assets/logos/png/1x/@(Model.Guid).png" />
|
||||
</dd>
|
||||
<dt class="col-sm-2">
|
||||
PNG 2x
|
||||
</dt>
|
||||
<dd class="col-sm-10">
|
||||
<img src="/assets/logos/png/2x/@(Model.Guid).png" />
|
||||
</dd>
|
||||
<dt class="col-sm-2">
|
||||
PNG 3x
|
||||
</dt>
|
||||
<dd class="col-sm-10">
|
||||
<img src="/assets/logos/png/3x/@(Model.Guid).png" />
|
||||
</dd>
|
||||
<dt class="col-sm-2">
|
||||
WebP 1x
|
||||
</dt>
|
||||
<dd class="col-sm-10">
|
||||
<img src="/assets/logos/webp/1x/@(Model.Guid).webp" />
|
||||
</dd>
|
||||
<dt class="col-sm-2">
|
||||
WebP 2x
|
||||
</dt>
|
||||
<dd class="col-sm-10">
|
||||
<img src="/assets/logos/webp/2x/@(Model.Guid).webp" />
|
||||
</dd>
|
||||
<dt class="col-sm-2">
|
||||
WebP 3x
|
||||
</dt>
|
||||
<dd class="col-sm-10">
|
||||
<img src="/assets/logos/webp/3x/@(Model.Guid).webp" />
|
||||
</dd>
|
||||
</form>
|
||||
</div>
|
||||
@@ -1,74 +0,0 @@
|
||||
@model Marechai.Database.Models.CompanyLogo
|
||||
|
||||
@{
|
||||
ViewData["Title"] = "Details";
|
||||
}
|
||||
<h1>Details</h1>
|
||||
<div>
|
||||
<a asp-action="Edit" asp-route-id="@Model.Id" class="btn btn-primary">
|
||||
Edit
|
||||
</a>
|
||||
<a asp-action="Index" class="btn btn-secondary">
|
||||
Back to List
|
||||
</a>
|
||||
</div>
|
||||
<div>
|
||||
<h4>Company logo</h4>
|
||||
<hr />
|
||||
<dl class="row">
|
||||
<dt class="col-sm-2">
|
||||
@Html.DisplayNameFor(model => model.Company)
|
||||
</dt>
|
||||
<dd class="col-sm-10">
|
||||
@Html.DisplayFor(model => model.Company.Name)
|
||||
</dd>
|
||||
<dt class="col-sm-2">
|
||||
@Html.DisplayNameFor(model => model.Year)
|
||||
</dt>
|
||||
<dd class="col-sm-10">
|
||||
@Html.DisplayFor(model => model.Year)
|
||||
</dd>
|
||||
<dt class="col-sm-2">
|
||||
SVG
|
||||
</dt>
|
||||
<dd class="col-sm-10">
|
||||
<img src="/assets/logos/@(Model.Guid).svg" />
|
||||
</dd>
|
||||
<dt class="col-sm-2">
|
||||
PNG 1x
|
||||
</dt>
|
||||
<dd class="col-sm-10">
|
||||
<img src="/assets/logos/png/1x/@(Model.Guid).png" />
|
||||
</dd>
|
||||
<dt class="col-sm-2">
|
||||
PNG 2x
|
||||
</dt>
|
||||
<dd class="col-sm-10">
|
||||
<img src="/assets/logos/png/2x/@(Model.Guid).png" />
|
||||
</dd>
|
||||
<dt class="col-sm-2">
|
||||
PNG 3x
|
||||
</dt>
|
||||
<dd class="col-sm-10">
|
||||
<img src="/assets/logos/png/3x/@(Model.Guid).png" />
|
||||
</dd>
|
||||
<dt class="col-sm-2">
|
||||
WebP 1x
|
||||
</dt>
|
||||
<dd class="col-sm-10">
|
||||
<img src="/assets/logos/webp/1x/@(Model.Guid).webp" />
|
||||
</dd>
|
||||
<dt class="col-sm-2">
|
||||
WebP 2x
|
||||
</dt>
|
||||
<dd class="col-sm-10">
|
||||
<img src="/assets/logos/webp/2x/@(Model.Guid).webp" />
|
||||
</dd>
|
||||
<dt class="col-sm-2">
|
||||
WebP 3x
|
||||
</dt>
|
||||
<dd class="col-sm-10">
|
||||
<img src="/assets/logos/webp/3x/@(Model.Guid).webp" />
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
@@ -1,48 +0,0 @@
|
||||
@model IEnumerable<Marechai.Areas.Admin.Models.CompanyLogoViewModel>
|
||||
|
||||
@{
|
||||
ViewData["Title"] = "Index";
|
||||
}
|
||||
<h1>Company logos</h1>
|
||||
<p>
|
||||
<a asp-action="Create" class="btn btn-primary">
|
||||
Create new
|
||||
</a>
|
||||
</p>
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>
|
||||
@Html.DisplayNameFor(model => model.Company)
|
||||
</th>
|
||||
<th>
|
||||
@Html.DisplayNameFor(model => model.Year)
|
||||
</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var item in Model)
|
||||
{
|
||||
<tr>
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.Company)
|
||||
</td>
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.Year)
|
||||
</td>
|
||||
<td>
|
||||
<a asp-action="Details" asp-route-id="@item.Id" class="btn btn-primary">
|
||||
Details
|
||||
</a>
|
||||
<a asp-action="Edit" asp-route-id="@item.Id" class="btn btn-secondary">
|
||||
Edit
|
||||
</a>
|
||||
<a asp-action="Delete" asp-route-id="@item.Id" class="btn btn-danger">
|
||||
Delete
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
@@ -2,7 +2,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<Version>3.0.99.1253</Version>
|
||||
<Version>3.0.99.1258</Version>
|
||||
<Company>Canary Islands Computer Museum</Company>
|
||||
<Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright>
|
||||
<Product>Canary Islands Computer Museum Website</Product>
|
||||
|
||||
@@ -38,6 +38,8 @@
|
||||
@inject IStringLocalizer<CompaniesService> L
|
||||
@inject Iso31661NumericService CountriesService
|
||||
@inject NavigationManager NavigationManager
|
||||
@inject CompanyLogosService CompanyLogosService
|
||||
@inject IWebHostEnvironment Host
|
||||
@attribute [Authorize(Roles = "UberAdmin, Admin")]
|
||||
<h3>@L["Company details"]</h3>
|
||||
<hr />
|
||||
@@ -312,4 +314,78 @@
|
||||
<Button Color="Color.Danger" Clicked="@OnCancelClicked">@L["Cancel"]</Button>
|
||||
}
|
||||
<a href="/admin/companies" class="btn btn-secondary">@L["Back to list"]</a>
|
||||
</div>
|
||||
@if (!_editing)
|
||||
{
|
||||
<a class="btn btn-success">@L["Upload new logo"]</a>
|
||||
}
|
||||
</div>
|
||||
@if (_logos.Count > 0)
|
||||
{
|
||||
<h4>@L["Logos"]</h4>
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>
|
||||
@L["Logo"]
|
||||
</th>
|
||||
<th>
|
||||
@L["Year logo came in use"]
|
||||
</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var logo in _logos)
|
||||
{
|
||||
<tr>
|
||||
<td>
|
||||
@if (File.Exists(Path.Combine(Host.WebRootPath, "assets/logos", logo.Guid + ".svg")))
|
||||
{
|
||||
<picture>
|
||||
<source type="image/svg+xml" srcset="/assets/logos/@(logo.Guid).svg">
|
||||
<source type="image/webp" srcset="/assets/logos/webp/1x/@(logo.Guid).webp, /assets/logos/webp/2x/@(logo.Guid).webp 2x, /assets/logos/webp/3x/@(logo.Guid).webp 3x">
|
||||
<img srcset="/assets/logos/png/1x/@(logo.Guid).png, /assets/logos/png/2x/@(logo.Guid).png 2x, /assets/logos/png/3x/@(logo.Guid).png 3x"
|
||||
src="/assets/logos/png/1x@(logo.Guid).png" alt="" height="auto" width="auto" style="max-height: 256px; max-width: 256px" />
|
||||
</picture>
|
||||
}
|
||||
else
|
||||
{
|
||||
<strong>@L["Cannot find logo file"]</strong>
|
||||
}
|
||||
</td>
|
||||
<td>
|
||||
@if (logo.Year.HasValue)
|
||||
{
|
||||
@logo.Year
|
||||
}
|
||||
else
|
||||
{
|
||||
@L["Unknown (logo year)"]
|
||||
}
|
||||
</td>
|
||||
<td>
|
||||
<a class="btn btn-primary">@L["Change year"]</a>
|
||||
<Button Color="Color.Danger" Clicked="() => {ShowModal(logo.Id);}">@L["Delete"]</Button>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
}
|
||||
|
||||
<Modal @ref="_frmDelete" IsCentered="true" Closing="@ModalClosing">
|
||||
<ModalBackdrop/>
|
||||
<ModalContent Centered="true">
|
||||
<ModalHeader>
|
||||
<ModalTitle>@L["Delete logo"]</ModalTitle>
|
||||
<CloseButton Clicked="@HideModal"/>
|
||||
</ModalHeader>
|
||||
<ModalBody>
|
||||
<Text>@string.Format(@L["Are you sure you want to delete the company logo introduced in {0}?"], _currentLogo?.Year)</Text>
|
||||
</ModalBody>
|
||||
<ModalFooter>
|
||||
<Button Color="Color.Primary" Clicked="@HideModal" Disabled="@_deleteInProgress">@L["Cancel"]</Button>
|
||||
<Button Color="Color.Danger" Clicked="@ConfirmDelete" Disabled="@_deleteInProgress">@L["Delete"]</Button>
|
||||
</ModalFooter>
|
||||
</ModalContent>
|
||||
</Modal>
|
||||
|
||||
@@ -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<CompanyViewModel> _companies;
|
||||
List<Iso31661Numeric> _countries;
|
||||
bool _creating;
|
||||
CompanyLogo _currentLogo;
|
||||
bool _deleteInProgress;
|
||||
bool _editing;
|
||||
Modal _frmDelete;
|
||||
bool _loaded;
|
||||
List<CompanyLogo> _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;
|
||||
}
|
||||
}
|
||||
@@ -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<List<CompanyLogo>> GetByCompany(int companyId) => await _context.CompanyLogos.Where(l => l.CompanyId == companyId).OrderBy(l => l.Year).ToListAsync();
|
||||
public async Task<List<CompanyLogo>> 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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user