diff --git a/DrinkRateAPI/ApiModels/CompanyTable/CompanyTableGet.cs b/DrinkRateAPI/ApiModels/CompanyTable/CompanyTableGet.cs new file mode 100644 index 0000000..9e3844a --- /dev/null +++ b/DrinkRateAPI/ApiModels/CompanyTable/CompanyTableGet.cs @@ -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; } +} diff --git a/DrinkRateAPI/Controllers/CompanyTableController.cs b/DrinkRateAPI/Controllers/CompanyTableController.cs new file mode 100644 index 0000000..b6d1e55 --- /dev/null +++ b/DrinkRateAPI/Controllers/CompanyTableController.cs @@ -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 GetCompanyTable([FromRoute] string companyTableName) + { + return await _companyTableService.GetCompanyTable(companyTableName); + } +} diff --git a/DrinkRateAPI/Program.cs b/DrinkRateAPI/Program.cs index 6ffeeb1..63f6bfd 100644 --- a/DrinkRateAPI/Program.cs +++ b/DrinkRateAPI/Program.cs @@ -59,6 +59,7 @@ builder.Services.AddDbContext(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddScoped(); var app = builder.Build(); diff --git a/DrinkRateAPI/Services/CompanyTableService.cs b/DrinkRateAPI/Services/CompanyTableService.cs new file mode 100644 index 0000000..79bd20b --- /dev/null +++ b/DrinkRateAPI/Services/CompanyTableService.cs @@ -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 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."); + } + } +} \ No newline at end of file