drinkrate/DrinkRateAPI/Services/CompanyTableService.cs

38 lines
No EOL
1.3 KiB
C#

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.");
}
}
}