Compare commits
No commits in common. "d2789022ce0a01f3a680dad4474e8a077974ae86" and "cfa0da80829bc71979c9fab2441be3a4d202ac28" have entirely different histories.
d2789022ce
...
cfa0da8082
6 changed files with 8 additions and 81 deletions
|
@ -1,8 +0,0 @@
|
|||
namespace DrinkRateAPI.ApiModels.CompanyTable;
|
||||
|
||||
public class CompanyTableGet
|
||||
{
|
||||
public string CompanyTableName { get; set; }
|
||||
public string CompanyTableId { get; set; }
|
||||
public string ProductTableId { get; set; }
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -1,11 +1,14 @@
|
|||
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("productTables")]
|
||||
[Route("productTable")]
|
||||
public class ProductTableController : ControllerBase
|
||||
{
|
||||
private ProductTableService _productTableService;
|
||||
|
|
|
@ -30,9 +30,10 @@ public class ExceptionHandlingMiddleware
|
|||
|
||||
private async Task HandleExceptionAsync(HttpContext context, Exception exception)
|
||||
{
|
||||
_logger.LogError(exception, "An error occurred.");
|
||||
_logger.LogError(exception, "An unexpected error occurred.");
|
||||
|
||||
var defaultResponse = exception switch
|
||||
|
||||
var response = exception switch
|
||||
{
|
||||
BadRequestException _ => new ExceptionResponse(StatusCodes.Status400BadRequest, "Application exception occurred."),
|
||||
NotFoundException _ => new ExceptionResponse(StatusCodes.Status404NotFound, "The request key not found."),
|
||||
|
@ -43,13 +44,7 @@ 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);
|
||||
|
|
|
@ -59,7 +59,6 @@ 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();
|
||||
|
|
|
@ -1,38 +0,0 @@
|
|||
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.");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue