Files
marechai/Marechai/Services/CompaniesService.cs

266 lines
15 KiB
C#
Raw Permalink Normal View History

2020-05-24 02:12:37 +01:00
/******************************************************************************
2020-05-21 20:25:47 +01:00
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ 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 <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2025-11-14 05:08:14 +00:00
// Copyright © 2003-2026 Natalia Portillo
2020-05-21 20:25:47 +01:00
*******************************************************************************/
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Marechai.Data.Dtos;
2020-05-21 20:25:47 +01:00
using Marechai.Database.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization;
2025-11-13 04:05:35 +00:00
namespace Marechai.Services;
2020-05-21 20:25:47 +01:00
2025-11-13 04:05:35 +00:00
public class CompaniesService(MarechaiContext context, IStringLocalizer<CompaniesService> localizer)
{
readonly IStringLocalizer<CompaniesService> _l = localizer;
2020-05-21 20:25:47 +01:00
public async Task<List<CompanyDto>> GetAsync() => await context.Companies.Include(c => c.Logos)
2025-11-13 04:05:35 +00:00
.OrderBy(c => c.Name)
.Select(c => new CompanyDto
2025-11-13 04:05:35 +00:00
{
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,
FoundedDayIsUnknown =
c.FoundedDayIsUnknown,
FoundedMonthIsUnknown =
c.FoundedMonthIsUnknown,
SoldDayIsUnknown =
c.SoldDayIsUnknown,
SoldMonthIsUnknown =
c.SoldMonthIsUnknown,
LegalName = c.LegalName
})
.ToListAsync();
public async Task<CompanyDto> GetAsync(int id) => await context.Companies.Where(c => c.Id == id)
.Select(c => new CompanyDto
2025-11-13 04:05:35 +00:00
{
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,
FoundedDayIsUnknown =
c.FoundedDayIsUnknown,
FoundedMonthIsUnknown =
c.FoundedMonthIsUnknown,
SoldDayIsUnknown =
c.SoldDayIsUnknown,
SoldMonthIsUnknown =
c.SoldMonthIsUnknown,
LegalName = c.LegalName
})
.FirstOrDefaultAsync();
2020-05-27 21:24:53 +01:00
public async Task UpdateAsync(CompanyDto dto, string userId)
2025-11-13 04:05:35 +00:00
{
Company model = await context.Companies.FindAsync(dto.Id);
2020-05-27 21:24:53 +01:00
2025-11-13 04:05:35 +00:00
if(model is null) return;
2020-05-27 21:24:53 +01:00
model.Name = dto.Name;
model.Founded = dto.Founded;
model.Sold = dto.Sold;
model.SoldToId = dto.SoldToId;
model.CountryId = dto.CountryId;
model.Status = dto.Status;
model.Website = dto.Website;
model.Twitter = dto.Twitter;
model.Facebook = dto.Facebook;
model.Address = dto.Address;
model.City = dto.City;
model.Province = dto.Province;
model.PostalCode = dto.PostalCode;
model.FoundedDayIsUnknown = dto.FoundedDayIsUnknown;
model.FoundedMonthIsUnknown = dto.FoundedMonthIsUnknown;
model.SoldDayIsUnknown = dto.SoldDayIsUnknown;
model.SoldMonthIsUnknown = dto.SoldMonthIsUnknown;
model.LegalName = dto.LegalName;
2025-11-13 04:05:35 +00:00
await context.SaveChangesWithUserAsync(userId);
}
2020-05-21 22:24:57 +01:00
public async Task<int> CreateAsync(CompanyDto dto, string userId)
2025-11-13 04:05:35 +00:00
{
var model = new Company
{
Name = dto.Name,
Founded = dto.Founded,
Sold = dto.Sold,
SoldToId = dto.SoldToId,
CountryId = dto.CountryId,
Status = dto.Status,
Website = dto.Website,
Twitter = dto.Twitter,
Facebook = dto.Facebook,
Address = dto.Address,
City = dto.City,
Province = dto.Province,
PostalCode = dto.PostalCode,
FoundedDayIsUnknown = dto.FoundedDayIsUnknown,
FoundedMonthIsUnknown = dto.FoundedMonthIsUnknown,
SoldDayIsUnknown = dto.SoldDayIsUnknown,
SoldMonthIsUnknown = dto.SoldMonthIsUnknown,
LegalName = dto.LegalName
2025-11-13 04:05:35 +00:00
};
2025-11-13 04:05:35 +00:00
await context.Companies.AddAsync(model);
await context.SaveChangesWithUserAsync(userId);
2025-11-13 04:05:35 +00:00
return model.Id;
}
2020-05-21 22:24:57 +01:00
2025-11-13 04:05:35 +00:00
public async Task<List<Machine>> GetMachinesAsync(int id) => await context.Machines.Where(m => m.CompanyId == id)
.OrderBy(m => m.Name)
.Select(m => new Machine
{
Id = m.Id,
Name = m.Name,
Type = m.Type
})
.ToListAsync();
2020-05-21 22:24:57 +01:00
2025-11-13 04:05:35 +00:00
public async Task<string> GetDescriptionTextAsync(int id)
{
CompanyDescription description = await context.CompanyDescriptions.FirstOrDefaultAsync(d => d.CompanyId == id);
2020-05-21 22:24:57 +01:00
2025-11-13 04:05:35 +00:00
return description?.Html ?? description?.Text;
}
2020-05-21 23:32:21 +01:00
2025-11-13 04:05:35 +00:00
public async Task<Company> GetSoldToAsync(int? id) => await context.Companies.Select(c => new Company
{
Id = c.Id,
Name = c.Name
})
.FirstOrDefaultAsync(c => c.Id == id);
2020-05-21 23:32:21 +01:00
2025-11-13 04:05:35 +00:00
public async Task<string> GetCountryNameAsync(int id) =>
(await context.Iso31661Numeric.FirstOrDefaultAsync(c => c.Id == id))?.Name;
2020-05-22 00:12:13 +01:00
public Task<List<CompanyDto>> GetCompaniesByCountryAsync(int countryId) => context.Companies
2025-11-13 04:05:35 +00:00
.Include(c => c.Logos)
.Where(c => c.CountryId == countryId)
.OrderBy(c => c.Name)
.Select(c => new CompanyDto
2025-11-13 04:05:35 +00:00
{
Id = c.Id,
LastLogo = c.Logos.OrderByDescending(l => l.Year).FirstOrDefault().Guid,
Name = c.Name
})
.ToListAsync();
2020-05-24 20:53:29 +01:00
public Task<List<CompanyDto>> GetCompaniesByLetterAsync(char id) => context.Companies.Include(c => c.Logos)
2025-11-13 04:05:35 +00:00
.Where(c => EF.Functions.Like(c.Name, $"{id}%"))
.OrderBy(c => c.Name)
.Select(c => new CompanyDto
2020-05-24 20:53:29 +01:00
{
2025-11-13 04:05:35 +00:00
Id = c.Id,
LastLogo = c.Logos.OrderByDescending(l => l.Year).FirstOrDefault().Guid,
Name = c.Name
})
.ToListAsync();
2020-05-24 20:53:29 +01:00
2025-11-13 04:05:35 +00:00
public async Task DeleteAsync(int id, string userId)
{
Company item = await context.Companies.FindAsync(id);
2020-05-24 20:53:29 +01:00
2025-11-13 04:05:35 +00:00
if(item is null) return;
2020-05-24 20:53:29 +01:00
2025-11-13 04:05:35 +00:00
context.Companies.Remove(item);
2025-11-13 04:05:35 +00:00
await context.SaveChangesWithUserAsync(userId);
}
public async Task<CompanyDescriptionDto> GetDescriptionAsync(int id) => await context.CompanyDescriptions
2025-11-13 04:05:35 +00:00
.Where(d => d.CompanyId == id)
.Select(d => new CompanyDescriptionDto
{
2025-11-13 04:05:35 +00:00
Id = d.Id,
CompanyId = d.CompanyId,
Html = d.Html,
Markdown = d.Text
})
.FirstOrDefaultAsync();
public async Task<int> CreateOrUpdateDescriptionAsync(int id, CompanyDescriptionDto description,
2025-11-13 04:05:35 +00:00
string userId)
{
CompanyDescription current = await context.CompanyDescriptions.FirstOrDefaultAsync(d => d.CompanyId == id);
2025-11-13 04:05:35 +00:00
if(current is null)
{
current = new CompanyDescription
{
2025-11-13 04:05:35 +00:00
CompanyId = id,
Html = description.Html,
Text = description.Markdown
};
2025-11-13 04:05:35 +00:00
await context.CompanyDescriptions.AddAsync(current);
}
2025-11-13 04:05:35 +00:00
else
{
current.Html = description.Html;
current.Text = description.Markdown;
}
await context.SaveChangesWithUserAsync(userId);
return current.Id;
2020-05-21 20:25:47 +01:00
}
}