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">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>3.0.99.1231</Version>
<Version>3.0.99.1236</Version>
<Company>Canary Islands Computer Museum</Company>
<Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright>
<Product>Canary Islands Computer Museum Website</Product>

View File

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

View File

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

View File

@@ -68,14 +68,18 @@ namespace Marechai.Services
public Task<CompanyViewModel> GetAsync(int id) => _context.Companies.Where(c => c.Id == id).
Select(c => new CompanyViewModel
{
Id = c.Id, Name = c.Name, Founded = c.Founded,
Sold = c.Sold, SoldToId = c.SoldToId,
CountryId = c.CountryId, Status = c.Status,
Website = c.Website, Twitter = c.Twitter,
Facebook = c.Facebook, Address = c.Address,
City = c.City, Province = c.Province,
Id = c.Id,
LastLogo = c.
Logos.OrderByDescending(l => l.Year).
FirstOrDefault().Guid,
Name = c.Name, Founded = c.Founded,
Sold = c.Sold, SoldToId = c.SoldToId,
CountryId = c.CountryId, Status = c.Status,
Website = c.Website, Twitter = c.Twitter,
Facebook = c.Facebook, Address = c.Address,
City = c.City, Province = c.Province,
PostalCode = c.PostalCode,
Country = c.Country.Name
Country = c.Country.Name
}).FirstOrDefaultAsync();
public async Task UpdateAsync(CompanyViewModel viewModel)
@@ -102,6 +106,23 @@ namespace Marechai.Services
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) =>
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<Iso31661NumericService>();
services.AddScoped<ResolutionsService>();
services.AddScoped<CompanyLogosService>();
}
}
}