drinkrate/DrinkRateAPI/Services/ProductTableService.cs
martinshoob 5401d29d44 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.
2025-08-11 19:47:34 +02:00

39 lines
No EOL
1.2 KiB
C#

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()
};
}
}