2025-11-13 18:22:44 +00: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/>.
|
|
|
|
|
//
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// Copyright © 2003-2025 Natalia Portillo
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Marechai.Data.Dtos;
|
|
|
|
|
using Marechai.Database.Models;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
|
|
namespace Marechai.Server.Controllers;
|
|
|
|
|
|
|
|
|
|
[Route("/companies")]
|
|
|
|
|
[ApiController]
|
2025-11-13 18:28:14 +00:00
|
|
|
public class CompaniesController(MarechaiContext context) : ControllerBase
|
2025-11-13 18:22:44 +00:00
|
|
|
{
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
2025-11-13 18:27:00 +00:00
|
|
|
public Task<List<CompanyDto>> GetAsync() => context.Companies.Include(c => c.Logos)
|
|
|
|
|
.OrderBy(c => c.Name)
|
|
|
|
|
.Select(c => new CompanyDto
|
|
|
|
|
{
|
|
|
|
|
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();
|
2025-11-13 18:22:44 +00:00
|
|
|
|
2025-11-13 20:51:05 +00:00
|
|
|
[HttpGet("{id:int}")]
|
2025-11-13 18:22:44 +00:00
|
|
|
[AllowAnonymous]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
2025-11-13 18:27:00 +00:00
|
|
|
public Task<CompanyDto> GetAsync(int id) => context.Companies.Where(c => c.Id == id)
|
|
|
|
|
.Select(c => new CompanyDto
|
|
|
|
|
{
|
|
|
|
|
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();
|
2025-11-13 18:22:44 +00:00
|
|
|
|
2025-11-13 20:51:05 +00:00
|
|
|
[HttpPut("{id:int}")]
|
2025-11-13 18:22:44 +00:00
|
|
|
[Authorize(Roles = "Admin,UberAdmin")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
2025-11-13 19:10:08 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
2025-11-13 19:28:21 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
2025-11-13 20:51:05 +00:00
|
|
|
public async Task<ActionResult> UpdateAsync(int id, [FromBody] CompanyDto dto)
|
2025-11-13 18:22:44 +00:00
|
|
|
{
|
|
|
|
|
string userId = User.FindFirstValue(ClaimTypes.Sid);
|
2025-11-13 18:27:00 +00:00
|
|
|
|
2025-11-13 18:52:45 +00:00
|
|
|
if(userId is null) return Unauthorized();
|
2025-11-13 20:51:05 +00:00
|
|
|
Company model = await context.Companies.FindAsync(id);
|
2025-11-13 18:22:44 +00:00
|
|
|
|
2025-11-13 18:52:45 +00:00
|
|
|
if(model is null) return NotFound();
|
2025-11-13 18:22:44 +00: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;
|
|
|
|
|
await context.SaveChangesWithUserAsync(userId);
|
2025-11-13 18:52:45 +00:00
|
|
|
|
|
|
|
|
return Ok();
|
2025-11-13 18:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[Authorize(Roles = "Admin,UberAdmin")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
2025-11-13 19:28:21 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
2025-11-13 20:51:05 +00:00
|
|
|
public async Task<ActionResult<int>> CreateAsync([FromBody] CompanyDto dto)
|
2025-11-13 18:22:44 +00:00
|
|
|
{
|
|
|
|
|
string userId = User.FindFirstValue(ClaimTypes.Sid);
|
2025-11-13 18:27:00 +00:00
|
|
|
|
2025-11-13 18:52:45 +00:00
|
|
|
if(userId is null) return Unauthorized();
|
2025-11-13 18:27:00 +00:00
|
|
|
|
2025-11-13 18:22:44 +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
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await context.Companies.AddAsync(model);
|
|
|
|
|
await context.SaveChangesWithUserAsync(userId);
|
|
|
|
|
|
|
|
|
|
return model.Id;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-13 20:51:05 +00:00
|
|
|
[HttpGet("{id:int}/machines")]
|
2025-11-13 18:22:44 +00:00
|
|
|
[AllowAnonymous]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
2025-11-13 18:27:00 +00:00
|
|
|
public Task<List<Machine>> GetMachinesAsync(int id) => 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();
|
2025-11-13 18:22:44 +00:00
|
|
|
|
2025-11-13 20:51:05 +00:00
|
|
|
[HttpGet("{id:int}/description/text")]
|
2025-11-13 18:22:44 +00:00
|
|
|
[AllowAnonymous]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
|
|
public async Task<string> GetDescriptionTextAsync(int id)
|
|
|
|
|
{
|
|
|
|
|
CompanyDescription description = await context.CompanyDescriptions.FirstOrDefaultAsync(d => d.CompanyId == id);
|
|
|
|
|
|
|
|
|
|
return description?.Html ?? description?.Text;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-13 20:51:05 +00:00
|
|
|
[HttpGet("{id:int}/soldto")]
|
2025-11-13 18:22:44 +00:00
|
|
|
[AllowAnonymous]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
2025-11-13 18:27:00 +00:00
|
|
|
public Task<Company> GetSoldToAsync(int? id) => context.Companies.Select(c => new Company
|
|
|
|
|
{
|
|
|
|
|
Id = c.Id,
|
|
|
|
|
Name = c.Name
|
|
|
|
|
})
|
|
|
|
|
.FirstOrDefaultAsync(c => c.Id == id);
|
2025-11-13 18:22:44 +00:00
|
|
|
|
2025-11-13 20:51:05 +00:00
|
|
|
[HttpGet("/countries/{id:int}/name")]
|
2025-11-13 18:22:44 +00:00
|
|
|
[AllowAnonymous]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
|
|
public async Task<string> GetCountryNameAsync(int id) =>
|
|
|
|
|
(await context.Iso31661Numeric.FirstOrDefaultAsync(c => c.Id == id))?.Name;
|
|
|
|
|
|
2025-11-13 20:51:05 +00:00
|
|
|
[HttpGet("/countries/{id:int}/companies")]
|
2025-11-13 18:22:44 +00:00
|
|
|
[AllowAnonymous]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
2025-11-13 18:27:00 +00:00
|
|
|
public Task<List<CompanyDto>> GetCompaniesByCountryAsync(int countryId) => context.Companies.Include(c => c.Logos)
|
2025-11-13 18:22:44 +00:00
|
|
|
.Where(c => c.CountryId == countryId)
|
|
|
|
|
.OrderBy(c => c.Name)
|
|
|
|
|
.Select(c => new CompanyDto
|
|
|
|
|
{
|
|
|
|
|
Id = c.Id,
|
|
|
|
|
LastLogo = c.Logos.OrderByDescending(l => l.Year).FirstOrDefault().Guid,
|
|
|
|
|
Name = c.Name
|
|
|
|
|
})
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
2025-11-13 20:51:05 +00:00
|
|
|
[HttpGet("/companies/letter/{id:char}")]
|
2025-11-13 18:22:44 +00:00
|
|
|
[AllowAnonymous]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
|
|
public Task<List<CompanyDto>> GetCompaniesByLetterAsync(char id) => context.Companies.Include(c => c.Logos)
|
|
|
|
|
.Where(c => EF.Functions.Like(c.Name, $"{id}%"))
|
|
|
|
|
.OrderBy(c => c.Name)
|
|
|
|
|
.Select(c => new CompanyDto
|
|
|
|
|
{
|
|
|
|
|
Id = c.Id,
|
|
|
|
|
LastLogo = c.Logos.OrderByDescending(l => l.Year).FirstOrDefault().Guid,
|
|
|
|
|
Name = c.Name
|
|
|
|
|
})
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
2025-11-13 20:51:05 +00:00
|
|
|
[HttpDelete("{id:int}")]
|
2025-11-13 18:22:44 +00:00
|
|
|
[Authorize(Roles = "Admin,UberAdmin")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
2025-11-13 19:10:08 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
2025-11-13 19:28:21 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
2025-11-13 18:52:45 +00:00
|
|
|
public async Task<ActionResult> DeleteAsync(int id)
|
2025-11-13 18:22:44 +00:00
|
|
|
{
|
|
|
|
|
string userId = User.FindFirstValue(ClaimTypes.Sid);
|
2025-11-13 18:27:00 +00:00
|
|
|
|
2025-11-13 18:52:45 +00:00
|
|
|
if(userId is null) return Unauthorized();
|
2025-11-13 18:22:44 +00:00
|
|
|
Company item = await context.Companies.FindAsync(id);
|
|
|
|
|
|
2025-11-13 18:52:45 +00:00
|
|
|
if(item is null) return NotFound();
|
2025-11-13 18:22:44 +00:00
|
|
|
|
|
|
|
|
context.Companies.Remove(item);
|
|
|
|
|
|
|
|
|
|
await context.SaveChangesWithUserAsync(userId);
|
2025-11-13 18:52:45 +00:00
|
|
|
|
|
|
|
|
return Ok();
|
2025-11-13 18:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-13 20:51:05 +00:00
|
|
|
[HttpGet("{id:int}/description")]
|
2025-11-13 18:22:44 +00:00
|
|
|
[AllowAnonymous]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
2025-11-13 18:27:00 +00:00
|
|
|
public Task<CompanyDescriptionDto> GetDescriptionAsync(int id) => context.CompanyDescriptions
|
|
|
|
|
.Where(d => d.CompanyId == id)
|
|
|
|
|
.Select(d => new CompanyDescriptionDto
|
|
|
|
|
{
|
|
|
|
|
Id = d.Id,
|
|
|
|
|
CompanyId = d.CompanyId,
|
|
|
|
|
Html = d.Html,
|
|
|
|
|
Markdown = d.Text
|
|
|
|
|
})
|
|
|
|
|
.FirstOrDefaultAsync();
|
2025-11-13 18:22:44 +00:00
|
|
|
|
2025-11-13 20:51:05 +00:00
|
|
|
[HttpPost("{id:int}/description")]
|
2025-11-13 18:22:44 +00:00
|
|
|
[Authorize(Roles = "Admin,UberAdmin")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
2025-11-13 19:28:21 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
2025-11-13 20:51:05 +00:00
|
|
|
public async Task<ActionResult<int>> CreateOrUpdateDescriptionAsync(
|
|
|
|
|
int id, [FromBody] CompanyDescriptionDto description)
|
2025-11-13 18:22:44 +00:00
|
|
|
{
|
|
|
|
|
string userId = User.FindFirstValue(ClaimTypes.Sid);
|
2025-11-13 18:27:00 +00:00
|
|
|
|
2025-11-13 18:52:45 +00:00
|
|
|
if(userId is null) return Unauthorized();
|
2025-11-13 18:22:44 +00:00
|
|
|
CompanyDescription current = await context.CompanyDescriptions.FirstOrDefaultAsync(d => d.CompanyId == id);
|
|
|
|
|
|
|
|
|
|
if(current is null)
|
|
|
|
|
{
|
|
|
|
|
current = new CompanyDescription
|
|
|
|
|
{
|
|
|
|
|
CompanyId = id,
|
|
|
|
|
Html = description.Html,
|
|
|
|
|
Text = description.Markdown
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await context.CompanyDescriptions.AddAsync(current);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
current.Html = description.Html;
|
|
|
|
|
current.Text = description.Markdown;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await context.SaveChangesWithUserAsync(userId);
|
|
|
|
|
|
|
|
|
|
return current.Id;
|
|
|
|
|
}
|
2025-11-13 18:27:00 +00:00
|
|
|
}
|