Compare commits
3 commits
cfa0da8082
...
d2789022ce
Author | SHA1 | Date | |
---|---|---|---|
d2789022ce | |||
dffa410c71 | |||
84e6f7e2c9 |
6 changed files with 81 additions and 8 deletions
8
DrinkRateAPI/ApiModels/CompanyTable/CompanyTableGet.cs
Normal file
8
DrinkRateAPI/ApiModels/CompanyTable/CompanyTableGet.cs
Normal 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; }
|
||||||
|
}
|
24
DrinkRateAPI/Controllers/CompanyTableController.cs
Normal file
24
DrinkRateAPI/Controllers/CompanyTableController.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,14 +1,11 @@
|
||||||
using DrinkRateAPI.ApiModels.ProductTable;
|
using DrinkRateAPI.ApiModels.ProductTable;
|
||||||
using DrinkRateAPI.AuthorizationPolicies;
|
|
||||||
using DrinkRateAPI.DbEntities;
|
|
||||||
using DrinkRateAPI.Services;
|
using DrinkRateAPI.Services;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace DrinkRateAPI.Controllers;
|
namespace DrinkRateAPI.Controllers;
|
||||||
|
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("productTable")]
|
[Route("productTables")]
|
||||||
public class ProductTableController : ControllerBase
|
public class ProductTableController : ControllerBase
|
||||||
{
|
{
|
||||||
private ProductTableService _productTableService;
|
private ProductTableService _productTableService;
|
||||||
|
|
|
@ -30,10 +30,9 @@ public class ExceptionHandlingMiddleware
|
||||||
|
|
||||||
private async Task HandleExceptionAsync(HttpContext context, Exception exception)
|
private async Task HandleExceptionAsync(HttpContext context, Exception exception)
|
||||||
{
|
{
|
||||||
_logger.LogError(exception, "An unexpected error occurred.");
|
_logger.LogError(exception, "An error occurred.");
|
||||||
|
|
||||||
|
var defaultResponse = exception switch
|
||||||
var response = exception switch
|
|
||||||
{
|
{
|
||||||
BadRequestException _ => new ExceptionResponse(StatusCodes.Status400BadRequest, "Application exception occurred."),
|
BadRequestException _ => new ExceptionResponse(StatusCodes.Status400BadRequest, "Application exception occurred."),
|
||||||
NotFoundException _ => new ExceptionResponse(StatusCodes.Status404NotFound, "The request key not found."),
|
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."),
|
UnavailableForLagalReasonsException _ => new ExceptionResponse(StatusCodes.Status451UnavailableForLegalReasons, "Unavailable for legal reasons."),
|
||||||
_ => new ExceptionResponse(StatusCodes.Status500InternalServerError, "Internal server error. Please retry later.")
|
_ => 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.ContentType = "application/json";
|
||||||
context.Response.StatusCode = response.StatusCode;
|
context.Response.StatusCode = response.StatusCode;
|
||||||
await context.Response.WriteAsJsonAsync(response);
|
await context.Response.WriteAsJsonAsync(response);
|
||||||
|
|
|
@ -59,6 +59,7 @@ builder.Services.AddDbContext<ApplicationDbContext>();
|
||||||
builder.Services.AddScoped<ApplicationUserService>();
|
builder.Services.AddScoped<ApplicationUserService>();
|
||||||
builder.Services.AddScoped<UserProfileService>();
|
builder.Services.AddScoped<UserProfileService>();
|
||||||
builder.Services.AddScoped<ProductTableService>();
|
builder.Services.AddScoped<ProductTableService>();
|
||||||
|
builder.Services.AddScoped<CompanyTableService>();
|
||||||
builder.Services.AddScoped<ProductCompanyTableCoupleService>();
|
builder.Services.AddScoped<ProductCompanyTableCoupleService>();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
38
DrinkRateAPI/Services/CompanyTableService.cs
Normal file
38
DrinkRateAPI/Services/CompanyTableService.cs
Normal 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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue