Add CompanyTable controller/service

This commit is contained in:
martinshoob 2025-08-12 07:52:03 +02:00
parent dffa410c71
commit d2789022ce
4 changed files with 71 additions and 0 deletions

View file

@ -0,0 +1,8 @@
namespace DrinkRateAPI.ApiModels.CompanyTable;
public class CompanyTableGet
{
public string CompanyTableName { get; set; }
public string CompanyTableId { get; set; }
public string ProductTableId { get; set; }
}

View file

@ -0,0 +1,24 @@
using DrinkRateAPI.ApiModels.CompanyTable;
using DrinkRateAPI.Services;
using Microsoft.AspNetCore.Mvc;
namespace DrinkRateAPI.Controllers;
[ApiController]
[Route("companyTables")]
public class CompanyTableController : ControllerBase
{
private CompanyTableService _companyTableService;
public CompanyTableController(CompanyTableService companyTableService)
{
_companyTableService = companyTableService;
}
[HttpGet("{companyTableName}")]
[Produces("application/json")]
public async Task<CompanyTableGet> GetCompanyTable([FromRoute] string companyTableName)
{
return await _companyTableService.GetCompanyTable(companyTableName);
}
}

View file

@ -59,6 +59,7 @@ builder.Services.AddDbContext<ApplicationDbContext>();
builder.Services.AddScoped<ApplicationUserService>();
builder.Services.AddScoped<UserProfileService>();
builder.Services.AddScoped<ProductTableService>();
builder.Services.AddScoped<CompanyTableService>();
builder.Services.AddScoped<ProductCompanyTableCoupleService>();
var app = builder.Build();

View file

@ -0,0 +1,38 @@
using DrinkRateAPI.ApiModels.CompanyTable;
using DrinkRateAPI.Contexts;
using DrinkRateAPI.Exceptions;
using Microsoft.EntityFrameworkCore;
namespace DrinkRateAPI.Services;
public class CompanyTableService(ApplicationDbContext context)
{
private ApplicationDbContext _context = context;
public async Task<CompanyTableGet> GetCompanyTable(string companyTableName)
{
if (string.IsNullOrWhiteSpace(companyTableName))
{
throw new BadRequestException("Company table name cannot be null or empty.");
}
try
{
var companyTable =
await _context.CompanyTable.Include(dbCompanyTable => dbCompanyTable.ProductTable)
.FirstOrDefaultAsync(x => x.CompanyTableName == companyTableName) ??
throw new NotFoundException($"Company table with the name {companyTableName} not found.");
return new CompanyTableGet
{
CompanyTableName = companyTable.CompanyTableName,
CompanyTableId = companyTable.Id.ToString(),
ProductTableId = companyTable.ProductTable.Id.ToString(),
};
}
catch (ArgumentNullException _)
{
throw new BadRequestException("Company table name cannot be null or empty.");
}
}
}