Compare commits

..

No commits in common. "5401d29d4401d3211923128b2840aaa04dc2797b" and "1dc37d328240804440015facbca4cfe6c7274a4d" have entirely different histories.

9 changed files with 4 additions and 109 deletions

View file

@ -1,7 +0,0 @@
namespace DrinkRateAPI.ApiModels.ProductTable;
public class ProductTableGet
{
public string ProductTableName { get; set; }
public string ProductTableId { get; set; }
}

View file

@ -1,6 +0,0 @@
namespace DrinkRateAPI.ApiModels.ProductTable;
public class ProductTablePost
{
public string ProductTableName { get; set; }
}

View file

@ -1,5 +1,4 @@
using DrinkRateAPI.DbEntities; using DrinkRateAPI.DbEntities;
using DrinkRateAPI.Exceptions;
using DrinkRateAPI.Services; using DrinkRateAPI.Services;
namespace DrinkRateAPI.AuthorizationPolicies; namespace DrinkRateAPI.AuthorizationPolicies;
@ -27,16 +26,7 @@ public class AdminOnlyHandler : AuthorizationHandler<AdminOnlyRequirement>
AuthorizationHandlerContext context, AuthorizationHandlerContext context,
AdminOnlyRequirement requirement) AdminOnlyRequirement requirement)
{ {
DbUserProfile userProfile; var userProfile = await _applicationUserService.UserProfileByApplicationUserAsync(context.User);
try
{
userProfile = await _applicationUserService.UserProfileByApplicationUserAsync(context.User);
}
catch (NotFoundException _)
{
throw new ForbiddenException();
}
if (_userProfileService.IsUserProfileAdmin(userProfile)) if (_userProfileService.IsUserProfileAdmin(userProfile))
{ {

View file

@ -1,7 +0,0 @@
namespace DrinkRateAPI.AuthorizationPolicies;
public static class PolicyConstants
{
public const string AdminOnly = "AdminOnly";
}

View file

@ -1,35 +0,0 @@
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);
}
}

View file

@ -1,6 +1,5 @@
using System.Security.Claims; using System.Security.Claims;
using DrinkRateAPI.ApiModels.UserProfile; using DrinkRateAPI.ApiModels.UserProfile;
using DrinkRateAPI.AuthorizationPolicies;
using DrinkRateAPI.Services; using DrinkRateAPI.Services;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -29,7 +28,7 @@ public class UserProfileController : ControllerBase
} }
[HttpPut("{userId}/adminStatus")] [HttpPut("{userId}/adminStatus")]
[Authorize(Policy = PolicyConstants.AdminOnly)] [Authorize(Policy = "AdminOnly")]
[Produces("application/json")] [Produces("application/json")]
public async Task<IActionResult> PutUserAdminStatus(string userId, [FromBody] UserProfileAdminStatusPut body) public async Task<IActionResult> PutUserAdminStatus(string userId, [FromBody] UserProfileAdminStatusPut body)
{ {

View file

@ -20,6 +20,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="ApiModels\" />
<Folder Include="Migrations\" /> <Folder Include="Migrations\" />
</ItemGroup> </ItemGroup>

View file

@ -15,7 +15,7 @@ builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddAuthorizationBuilder() builder.Services.AddAuthorizationBuilder()
.AddPolicy(PolicyConstants.AdminOnly, policy => .AddPolicy("AdminOnly", policy =>
policy.Requirements.Add(new AdminOnlyRequirement())); policy.Requirements.Add(new AdminOnlyRequirement()));
builder.Services.AddIdentityApiEndpoints<DbApplicationUser>() builder.Services.AddIdentityApiEndpoints<DbApplicationUser>()
.AddEntityFrameworkStores<ApplicationDbContext>(); .AddEntityFrameworkStores<ApplicationDbContext>();
@ -58,7 +58,6 @@ builder.Services.AddSwaggerGen(c =>
builder.Services.AddDbContext<ApplicationDbContext>(); builder.Services.AddDbContext<ApplicationDbContext>();
builder.Services.AddScoped<ApplicationUserService>(); builder.Services.AddScoped<ApplicationUserService>();
builder.Services.AddScoped<UserProfileService>(); builder.Services.AddScoped<UserProfileService>();
builder.Services.AddScoped<ProductTableService>();
var app = builder.Build(); var app = builder.Build();

View file

@ -1,39 +0,0 @@
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()
};
}
}