diff --git a/DrinkRateAPI/ApiModels/ProductTable/ProductTableGet.cs b/DrinkRateAPI/ApiModels/ProductTable/ProductTableGet.cs new file mode 100644 index 0000000..9668caa --- /dev/null +++ b/DrinkRateAPI/ApiModels/ProductTable/ProductTableGet.cs @@ -0,0 +1,7 @@ +namespace DrinkRateAPI.ApiModels.ProductTable; + +public class ProductTableGet +{ + public string ProductTableName { get; set; } + public string ProductTableId { get; set; } +} \ No newline at end of file diff --git a/DrinkRateAPI/ApiModels/ProductTable/ProductTablePost.cs b/DrinkRateAPI/ApiModels/ProductTable/ProductTablePost.cs new file mode 100644 index 0000000..020d2f5 --- /dev/null +++ b/DrinkRateAPI/ApiModels/ProductTable/ProductTablePost.cs @@ -0,0 +1,6 @@ +namespace DrinkRateAPI.ApiModels.ProductTable; + +public class ProductTablePost +{ + public string ProductTableName { get; set; } +} \ No newline at end of file diff --git a/DrinkRateAPI/Controllers/ProductTableController.cs b/DrinkRateAPI/Controllers/ProductTableController.cs new file mode 100644 index 0000000..c8bd34a --- /dev/null +++ b/DrinkRateAPI/Controllers/ProductTableController.cs @@ -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 PostProductTable([FromBody] ProductTablePost productTable) + { + return await _productTableService.PostProductTableAsync(productTable); + } + + [HttpGet("{productTableName}")] + [Produces("application/json")] + public async Task GetProductTable([FromRoute] string productTableName) + { + return await _productTableService.GetProductTable(productTableName); + } +} \ No newline at end of file diff --git a/DrinkRateAPI/DrinkRateAPI.csproj b/DrinkRateAPI/DrinkRateAPI.csproj index 7cdbba1..dacaac5 100644 --- a/DrinkRateAPI/DrinkRateAPI.csproj +++ b/DrinkRateAPI/DrinkRateAPI.csproj @@ -20,7 +20,6 @@ - diff --git a/DrinkRateAPI/Services/ProductTableService.cs b/DrinkRateAPI/Services/ProductTableService.cs new file mode 100644 index 0000000..7d00cec --- /dev/null +++ b/DrinkRateAPI/Services/ProductTableService.cs @@ -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 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 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() + }; + } +} \ No newline at end of file