Compare commits

...

3 commits

6 changed files with 81 additions and 8 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

@ -1,14 +1,11 @@
using DrinkRateAPI.ApiModels.ProductTable;
using DrinkRateAPI.AuthorizationPolicies;
using DrinkRateAPI.DbEntities;
using DrinkRateAPI.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace DrinkRateAPI.Controllers;
[ApiController]
[Route("productTable")]
[Route("productTables")]
public class ProductTableController : ControllerBase
{
private ProductTableService _productTableService;

View file

@ -30,10 +30,9 @@ public class ExceptionHandlingMiddleware
private async Task HandleExceptionAsync(HttpContext context, Exception exception)
{
_logger.LogError(exception, "An unexpected error occurred.");
_logger.LogError(exception, "An error occurred.");
var response = exception switch
var defaultResponse = exception switch
{
BadRequestException _ => new ExceptionResponse(StatusCodes.Status400BadRequest, "Application exception occurred."),
NotFoundException _ => new ExceptionResponse(StatusCodes.Status404NotFound, "The request key not found."),
@ -44,7 +43,13 @@ public class ExceptionHandlingMiddleware
UnavailableForLagalReasonsException _ => new ExceptionResponse(StatusCodes.Status451UnavailableForLegalReasons, "Unavailable for legal reasons."),
_ => new ExceptionResponse(StatusCodes.Status500InternalServerError, "Internal server error. Please retry later.")
};
var description = !string.IsNullOrWhiteSpace(exception.Message)
? exception.Message
: defaultResponse.Description;
var response = defaultResponse with { Description = description };
context.Response.ContentType = "application/json";
context.Response.StatusCode = response.StatusCode;
await context.Response.WriteAsJsonAsync(response);

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