Fix changes to companies service affecting company view.

This commit is contained in:
2020-05-27 22:17:28 +01:00
parent b6c70288be
commit f5d8f2bcca
6 changed files with 82 additions and 37 deletions

View File

@@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<Version>3.0.99.1231</Version> <Version>3.0.99.1236</Version>
<Company>Canary Islands Computer Museum</Company> <Company>Canary Islands Computer Museum</Company>
<Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright> <Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright>
<Product>Canary Islands Computer Museum Website</Product> <Product>Canary Islands Computer Museum Website</Product>

View File

@@ -36,6 +36,7 @@
@inherits OwningComponentBase<CompaniesService> @inherits OwningComponentBase<CompaniesService>
@inject IStringLocalizer<CompaniesService> L @inject IStringLocalizer<CompaniesService> L
@inject CompanyLogosService CompanyLogosService
@inject IWebHostEnvironment Host @inject IWebHostEnvironment Host
@if (!_loaded) @if (!_loaded)
@@ -55,16 +56,16 @@
<div class="container-fluid"> <div class="container-fluid">
<p align=center> <p align=center>
@if (_company.LastLogo != null && @if (_company.LastLogo != null &&
File.Exists(Path.Combine(Host.WebRootPath, "assets/logos", _company.LastLogo.Guid + ".svg"))) File.Exists(Path.Combine(Host.WebRootPath, "assets/logos", _company.LastLogo + ".svg")))
{ {
<picture> <picture>
<source type="image/svg+xml" srcset="/assets/logos/@(_company.LastLogo.Guid).svg"> <source type="image/svg+xml" srcset="/assets/logos/@(_company.LastLogo).svg">
<source type="image/webp" srcset="/assets/logos/webp/1x/@(_company.LastLogo.Guid).webp, <source type="image/webp" srcset="/assets/logos/webp/1x/@(_company.LastLogo).webp,
/assets/logos/webp/2x/@(_company.LastLogo.Guid).webp 2x, /assets/logos/webp/2x/@(_company.LastLogo).webp 2x,
/assets/logos/webp/3x/@(_company.LastLogo.Guid).webp 3x"> /assets/logos/webp/3x/@(_company.LastLogo).webp 3x">
<img srcset="/assets/logos/png/1x/@(_company.LastLogo.Guid).png, <img srcset="/assets/logos/png/1x/@(_company.LastLogo).png,
/assets/logos/png/2x/@(_company.LastLogo.Guid).png 2x, /assets/logos/png/2x/@(_company.LastLogo).png 2x,
/assets/logos/png/3x/@(_company.LastLogo.Guid).png 3x" src="/assets/logos/png/1x@(_company.LastLogo.Guid).png" alt="" height="auto" width="auto" style="max-height: 256px; max-width: 256px" /> /assets/logos/png/3x/@(_company.LastLogo).png 3x" src="/assets/logos/png/1x@(_company.LastLogo).png" alt="" height="auto" width="auto" style="max-height: 256px; max-width: 256px" />
</picture> </picture>
} }
</p> </p>
@@ -72,14 +73,14 @@
@{ @{
_carrouselActive = "active"; _carrouselActive = "active";
} }
@if (_company.Logos != null && @if (_logos != null &&
_company.Logos.Count > 1) _logos.Count > 1)
{ {
// TODO: Carousel // TODO: Carousel
<div class="col-3"> <div class="col-3">
<div class="carousel slide" data-ride="carousel" id="logosCarousel"> <div class="carousel slide" data-ride="carousel" id="logosCarousel">
<div class="carousel-inner"> <div class="carousel-inner">
@foreach (var logo in _company.Logos.Where(logo => File.Exists(Path.Combine(Host.WebRootPath, "assets/logos", logo.Guid + ".svg")))) @foreach (var logo in _logos.Where(logo => File.Exists(Path.Combine(Host.WebRootPath, "assets/logos", logo.Guid + ".svg"))))
{ {
<div class="carousel-item @_carrouselActive"> <div class="carousel-item @_carrouselActive">
<picture> <picture>
@@ -126,20 +127,20 @@
<tr> <tr>
<th>@L["Country"]</th> <th>@L["Country"]</th>
<td> <td>
<a href="/companies/country/@_company.Country.Id"> <a href="/companies/country/@_company.CountryId">
@if (File.Exists(Path.Combine(Host.WebRootPath, "assets/flags/countries", _company.Country.Id + ".svg"))) @if (File.Exists(Path.Combine(Host.WebRootPath, "assets/flags/countries", _company.CountryId + ".svg")))
{ {
<picture> <picture>
<source type="image/svg+xml" srcset="/assets/flags/countries/@(_company.Country.Id).svg"> <source type="image/svg+xml" srcset="/assets/flags/countries/@(_company.CountryId).svg">
<source type="image/webp" srcset="/assets/flags/countries/webp/1x/@(_company.Country.Id).webp, <source type="image/webp" srcset="/assets/flags/countries/webp/1x/@(_company.CountryId).webp,
/assets/flags/countries/webp/1x/@(_company.Country.Id).webp 2x, /assets/flags/countries/webp/1x/@(_company.CountryId).webp 2x,
/assets/flags/countries/webp/1x/@(_company.Country.Id).webp 3x"> /assets/flags/countries/webp/1x/@(_company.CountryId).webp 3x">
<img srcset="/assets/flags/countries/png/1x/@(_company.Country.Id).png, <img srcset="/assets/flags/countries/png/1x/@(_company.CountryId).png,
/assets/flags/countries/png/1x/@(_company.Country.Id).png 2x, /assets/flags/countries/png/1x/@(_company.CountryId).png 2x,
/assets/flags/countries/png/1x/@(_company.Country.Id).webp 3x" src="/assets/flags/countries/png/1x@(_company.Country.Id).png" alt="" height="32" /> /assets/flags/countries/png/1x/@(_company.CountryId).webp 3x" src="/assets/flags/countries/png/1x@(_company.CountryId).png" alt="" height="32" />
</picture> </picture>
} }
@L[_company.Country.Name] @L[_company.Country]
</a> </a>
</td> </td>
</tr> </tr>

View File

@@ -3,6 +3,7 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Marechai.Database; using Marechai.Database;
using Marechai.Database.Models; using Marechai.Database.Models;
using Marechai.ViewModels;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
namespace Marechai.Pages.Companies namespace Marechai.Pages.Companies
@@ -10,12 +11,12 @@ namespace Marechai.Pages.Companies
public partial class View public partial class View
{ {
string _carrouselActive; string _carrouselActive;
Company _company; CompanyViewModel _company;
List<Machine> _computers; List<Machine> _computers;
List<Machine> _consoles; List<Machine> _consoles;
string _description; string _description;
bool _loaded; bool _loaded;
List<CompanyLogo> _logos;
Company _soldTo; Company _soldTo;
[Parameter] [Parameter]
public int Id { get; set; } public int Id { get; set; }
@@ -40,6 +41,7 @@ namespace Marechai.Pages.Companies
_description = await Service.GetDescriptionAsync(Id); _description = await Service.GetDescriptionAsync(Id);
_soldTo = await Service.GetSoldToAsync(_company.SoldToId); _soldTo = await Service.GetSoldToAsync(_company.SoldToId);
_logos = await CompanyLogosService.GetByCompany(Id);
_loaded = true; _loaded = true;
} }

View File

@@ -68,7 +68,11 @@ namespace Marechai.Services
public Task<CompanyViewModel> GetAsync(int id) => _context.Companies.Where(c => c.Id == id). public Task<CompanyViewModel> GetAsync(int id) => _context.Companies.Where(c => c.Id == id).
Select(c => new CompanyViewModel Select(c => new CompanyViewModel
{ {
Id = c.Id, Name = c.Name, Founded = c.Founded, Id = c.Id,
LastLogo = c.
Logos.OrderByDescending(l => l.Year).
FirstOrDefault().Guid,
Name = c.Name, Founded = c.Founded,
Sold = c.Sold, SoldToId = c.SoldToId, Sold = c.Sold, SoldToId = c.SoldToId,
CountryId = c.CountryId, Status = c.Status, CountryId = c.CountryId, Status = c.Status,
Website = c.Website, Twitter = c.Twitter, Website = c.Website, Twitter = c.Twitter,
@@ -102,6 +106,23 @@ namespace Marechai.Services
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
} }
public async Task<int> CreateAsync(CompanyViewModel viewModel)
{
var model = new Company
{
Name = viewModel.Name, Founded = viewModel.Founded, Sold = viewModel.Sold,
SoldToId = viewModel.SoldToId, CountryId = viewModel.CountryId, Status = viewModel.Status,
Website = viewModel.Website, Twitter = viewModel.Twitter, Facebook = viewModel.Facebook,
Address = viewModel.Address, City = viewModel.City, Province = viewModel.Province,
PostalCode = viewModel.PostalCode
};
await _context.Companies.AddAsync(model);
await _context.SaveChangesAsync();
return model.Id;
}
public async Task<List<Machine>> GetMachinesAsync(int id) => public async Task<List<Machine>> GetMachinesAsync(int id) =>
await _context.Machines.Where(m => m.CompanyId == id).OrderBy(m => m.Name).Select(m => new Machine await _context.Machines.Where(m => m.CompanyId == id).OrderBy(m => m.Name).Select(m => new Machine
{ {

View File

@@ -0,0 +1,20 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Marechai.Database.Models;
using Microsoft.EntityFrameworkCore;
namespace Marechai.Services
{
public class CompanyLogosService
{
readonly MarechaiContext _context;
public CompanyLogosService(MarechaiContext context)
{
_context = context;
}
public async Task<List<CompanyLogo>> GetByCompany(int companyId) => await _context.CompanyLogos.Where(l => l.CompanyId == companyId).OrderBy(l => l.Year).ToListAsync();
}
}

View File

@@ -61,6 +61,7 @@ namespace Marechai.Services
services.AddScoped<SoundSynthsService>(); services.AddScoped<SoundSynthsService>();
services.AddScoped<Iso31661NumericService>(); services.AddScoped<Iso31661NumericService>();
services.AddScoped<ResolutionsService>(); services.AddScoped<ResolutionsService>();
services.AddScoped<CompanyLogosService>();
} }
} }
} }