Adding Endpoints with Authorization in separate PublicApi project (#413)
* Adding tests for GetById endpoint * Updating tests and messages * Adding paged endpoint and also AutoMapper * Authenticate endpoint works as bool with tests * Got JWT token security working with Create and Delete endpoints and Swashbuckle. * Working on getting cookie and jwt token auth working in the same app All tests are passing * Creating new project and moving APIs Build succeeds; tests need updated. * all tests passing after moving services to PublicApi project * Fix authorize attributes * Uncomment and update ApiCatalogControllerLists tests Co-authored-by: Eric Fleming <eric-fleming18@hotmail.com>
This commit is contained in:
52
src/PublicApi/CatalogItemEndpoints/Create.cs
Normal file
52
src/PublicApi/CatalogItemEndpoints/Create.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using Ardalis.ApiEndpoints;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Constants;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
using Swashbuckle.AspNetCore.Annotations;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
|
||||
{
|
||||
[Authorize(Roles = AuthorizationConstants.Roles.ADMINISTRATORS, AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
||||
public class Create : BaseAsyncEndpoint<CreateCatalogItemRequest, CreateCatalogItemResponse>
|
||||
{
|
||||
private readonly IAsyncRepository<CatalogItem> _itemRepository;
|
||||
|
||||
public Create(IAsyncRepository<CatalogItem> itemRepository)
|
||||
{
|
||||
_itemRepository = itemRepository;
|
||||
}
|
||||
|
||||
[HttpPost("api/catalog-items")]
|
||||
[SwaggerOperation(
|
||||
Summary = "Creates a new Catalog Item",
|
||||
Description = "Creates a new Catalog Item",
|
||||
OperationId = "catalog-items.create",
|
||||
Tags = new[] { "CatalogItemEndpoints" })
|
||||
]
|
||||
public override async Task<ActionResult<CreateCatalogItemResponse>> HandleAsync(CreateCatalogItemRequest request)
|
||||
{
|
||||
var response = new CreateCatalogItemResponse(request.CorrelationId());
|
||||
|
||||
CatalogItem newItem = new CatalogItem(request.CatalogTypeId, request.CatalogBrandId, request.Description, request.Name, request.Price, request.PictureUri);
|
||||
|
||||
newItem = await _itemRepository.AddAsync(newItem);
|
||||
|
||||
var dto = new CatalogItemDto
|
||||
{
|
||||
Id = newItem.Id,
|
||||
CatalogBrandId = newItem.CatalogBrandId,
|
||||
CatalogTypeId = newItem.CatalogTypeId,
|
||||
Description = newItem.Description,
|
||||
Name = newItem.Name,
|
||||
PictureUri = newItem.PictureUri,
|
||||
Price = newItem.Price
|
||||
};
|
||||
response.CatalogItem = dto;
|
||||
return response;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user