Add product table management endpoints
Implement API endpoints for creating and retrieving product tables. This allows administrators to define product tables and retrieve them by name. The creation endpoint is secured and only accessible to administrators.
This commit is contained in:
parent
9ec9139f69
commit
5401d29d44
5 changed files with 87 additions and 1 deletions
7
DrinkRateAPI/ApiModels/ProductTable/ProductTableGet.cs
Normal file
7
DrinkRateAPI/ApiModels/ProductTable/ProductTableGet.cs
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
namespace DrinkRateAPI.ApiModels.ProductTable;
|
||||||
|
|
||||||
|
public class ProductTableGet
|
||||||
|
{
|
||||||
|
public string ProductTableName { get; set; }
|
||||||
|
public string ProductTableId { get; set; }
|
||||||
|
}
|
6
DrinkRateAPI/ApiModels/ProductTable/ProductTablePost.cs
Normal file
6
DrinkRateAPI/ApiModels/ProductTable/ProductTablePost.cs
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
namespace DrinkRateAPI.ApiModels.ProductTable;
|
||||||
|
|
||||||
|
public class ProductTablePost
|
||||||
|
{
|
||||||
|
public string ProductTableName { get; set; }
|
||||||
|
}
|
35
DrinkRateAPI/Controllers/ProductTableController.cs
Normal file
35
DrinkRateAPI/Controllers/ProductTableController.cs
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
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")]
|
||||||
|
public class ProductTableController : ControllerBase
|
||||||
|
{
|
||||||
|
private ProductTableService _productTableService;
|
||||||
|
|
||||||
|
public ProductTableController(ProductTableService productTableService)
|
||||||
|
{
|
||||||
|
_productTableService = productTableService;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
[Authorize(Policy = PolicyConstants.AdminOnly)]
|
||||||
|
[Produces("application/json")]
|
||||||
|
public async Task<ProductTableGet> PostProductTable([FromBody] ProductTablePost productTable)
|
||||||
|
{
|
||||||
|
return await _productTableService.PostProductTableAsync(productTable);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("{productTableName}")]
|
||||||
|
[Produces("application/json")]
|
||||||
|
public async Task<ProductTableGet> GetProductTable([FromRoute] string productTableName)
|
||||||
|
{
|
||||||
|
return await _productTableService.GetProductTable(productTableName);
|
||||||
|
}
|
||||||
|
}
|
|
@ -20,7 +20,6 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="ApiModels\" />
|
|
||||||
<Folder Include="Migrations\" />
|
<Folder Include="Migrations\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
39
DrinkRateAPI/Services/ProductTableService.cs
Normal file
39
DrinkRateAPI/Services/ProductTableService.cs
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
using DrinkRateAPI.ApiModels.ProductTable;
|
||||||
|
using DrinkRateAPI.Contexts;
|
||||||
|
using DrinkRateAPI.DbEntities;
|
||||||
|
using DrinkRateAPI.Exceptions;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace DrinkRateAPI.Services;
|
||||||
|
|
||||||
|
public class ProductTableService(ApplicationDbContext context)
|
||||||
|
{
|
||||||
|
private ApplicationDbContext _context = context;
|
||||||
|
|
||||||
|
public async Task<ProductTableGet> PostProductTableAsync(ProductTablePost productTablePost)
|
||||||
|
{
|
||||||
|
DbProductTable productTable = new()
|
||||||
|
{
|
||||||
|
ProductTableName = productTablePost.ProductTableName
|
||||||
|
};
|
||||||
|
|
||||||
|
_context.ProductTable.Add(productTable);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
var productTableGet = await GetProductTable(productTable.ProductTableName);
|
||||||
|
return productTableGet;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<ProductTableGet> GetProductTable(string productTableName)
|
||||||
|
{
|
||||||
|
var productTable =
|
||||||
|
await _context.ProductTable.FirstOrDefaultAsync(x => x.ProductTableName == productTableName) ??
|
||||||
|
throw new NotFoundException();
|
||||||
|
|
||||||
|
return new ProductTableGet
|
||||||
|
{
|
||||||
|
ProductTableName = productTable.ProductTableName,
|
||||||
|
ProductTableId = productTable.Id.ToString()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue