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:
13
src/PublicApi/CatalogItemEndpoints/CatalogItemDto.cs
Normal file
13
src/PublicApi/CatalogItemEndpoints/CatalogItemDto.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
|
||||
{
|
||||
public class CatalogItemDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public decimal Price { get; set; }
|
||||
public string PictureUri { get; set; }
|
||||
public int CatalogTypeId { get; set; }
|
||||
public int CatalogBrandId { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
|
||||
{
|
||||
public class CreateCatalogItemRequest : BaseRequest
|
||||
{
|
||||
public int CatalogBrandId { get; set; }
|
||||
public int CatalogTypeId { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string PictureUri { get; set; }
|
||||
public decimal Price { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
|
||||
namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
|
||||
{
|
||||
public class CreateCatalogItemResponse : BaseResponse
|
||||
{
|
||||
public CreateCatalogItemResponse(Guid correlationId) : base(correlationId)
|
||||
{
|
||||
}
|
||||
|
||||
public CreateCatalogItemResponse()
|
||||
{
|
||||
}
|
||||
|
||||
public CatalogItemDto CatalogItem { get; set; }
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
|
||||
{
|
||||
public class DeleteCatalogItemRequest : BaseRequest
|
||||
{
|
||||
//[FromRoute]
|
||||
public int CatalogItemId { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
|
||||
namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
|
||||
{
|
||||
public class DeleteCatalogItemResponse : BaseResponse
|
||||
{
|
||||
public DeleteCatalogItemResponse(Guid correlationId) : base(correlationId)
|
||||
{
|
||||
}
|
||||
|
||||
public DeleteCatalogItemResponse()
|
||||
{
|
||||
}
|
||||
|
||||
public string Status { get; set; } = "Deleted";
|
||||
}
|
||||
}
|
||||
42
src/PublicApi/CatalogItemEndpoints/Delete.cs
Normal file
42
src/PublicApi/CatalogItemEndpoints/Delete.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
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 Delete : BaseAsyncEndpoint<DeleteCatalogItemRequest, DeleteCatalogItemResponse>
|
||||
{
|
||||
private readonly IAsyncRepository<CatalogItem> _itemRepository;
|
||||
|
||||
public Delete(IAsyncRepository<CatalogItem> itemRepository)
|
||||
{
|
||||
_itemRepository = itemRepository;
|
||||
}
|
||||
|
||||
[HttpDelete("api/catalog-items/{CatalogItemId}")]
|
||||
[SwaggerOperation(
|
||||
Summary = "Deletes a Catalog Item",
|
||||
Description = "Deletes a Catalog Item",
|
||||
OperationId = "catalog-items.Delete",
|
||||
Tags = new[] { "CatalogItemEndpoints" })
|
||||
]
|
||||
public override async Task<ActionResult<DeleteCatalogItemResponse>> HandleAsync([FromRoute]DeleteCatalogItemRequest request)
|
||||
{
|
||||
var response = new DeleteCatalogItemResponse(request.CorrelationId());
|
||||
|
||||
var itemToDelete = await _itemRepository.GetByIdAsync(request.CatalogItemId);
|
||||
if (itemToDelete is null) return NotFound();
|
||||
|
||||
await _itemRepository.DeleteAsync(itemToDelete);
|
||||
|
||||
return Ok(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
|
||||
{
|
||||
public class GetByIdCatalogItemRequest : BaseRequest
|
||||
{
|
||||
public int CatalogItemId { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
|
||||
namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
|
||||
{
|
||||
public class GetByIdCatalogItemResponse : BaseResponse
|
||||
{
|
||||
public GetByIdCatalogItemResponse(Guid correlationId) : base(correlationId)
|
||||
{
|
||||
}
|
||||
|
||||
public GetByIdCatalogItemResponse()
|
||||
{
|
||||
}
|
||||
|
||||
public CatalogItemDto CatalogItem { get; set; }
|
||||
}
|
||||
}
|
||||
46
src/PublicApi/CatalogItemEndpoints/GetById.cs
Normal file
46
src/PublicApi/CatalogItemEndpoints/GetById.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using Ardalis.ApiEndpoints;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
using Swashbuckle.AspNetCore.Annotations;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
|
||||
{
|
||||
public class GetById : BaseAsyncEndpoint<GetByIdCatalogItemRequest, GetByIdCatalogItemResponse>
|
||||
{
|
||||
private readonly IAsyncRepository<CatalogItem> _itemRepository;
|
||||
|
||||
public GetById(IAsyncRepository<CatalogItem> itemRepository)
|
||||
{
|
||||
_itemRepository = itemRepository;
|
||||
}
|
||||
|
||||
[HttpGet("api/catalog-items/{CatalogItemId}")]
|
||||
[SwaggerOperation(
|
||||
Summary = "Get a Catalog Item by Id",
|
||||
Description = "Gets a Catalog Item by Id",
|
||||
OperationId = "catalog-items.GetById",
|
||||
Tags = new[] { "CatalogItemEndpoints" })
|
||||
]
|
||||
public override async Task<ActionResult<GetByIdCatalogItemResponse>> HandleAsync([FromRoute]GetByIdCatalogItemRequest request)
|
||||
{
|
||||
var response = new GetByIdCatalogItemResponse(request.CorrelationId());
|
||||
|
||||
var item = await _itemRepository.GetByIdAsync(request.CatalogItemId);
|
||||
if (item is null) return NotFound();
|
||||
|
||||
response.CatalogItem = new CatalogItemDto
|
||||
{
|
||||
Id = item.Id,
|
||||
CatalogBrandId = item.CatalogBrandId,
|
||||
CatalogTypeId = item.CatalogTypeId,
|
||||
Description = item.Description,
|
||||
Name = item.Name,
|
||||
PictureUri = item.PictureUri,
|
||||
Price = item.Price
|
||||
};
|
||||
return Ok(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
|
||||
{
|
||||
public class ListPagedCatalogItemRequest : BaseRequest
|
||||
{
|
||||
public int PageSize { get; set; }
|
||||
public int PageIndex { get; set; }
|
||||
public int? CatalogBrandId { get; set; }
|
||||
public int? CatalogTypeId { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
|
||||
{
|
||||
public class ListPagedCatalogItemResponse : BaseResponse
|
||||
{
|
||||
public ListPagedCatalogItemResponse(Guid correlationId) : base(correlationId)
|
||||
{
|
||||
}
|
||||
|
||||
public ListPagedCatalogItemResponse()
|
||||
{
|
||||
}
|
||||
|
||||
public List<CatalogItemDto> CatalogItems { get; set; } = new List<CatalogItemDto>();
|
||||
public int PageCount { get; set; }
|
||||
}
|
||||
}
|
||||
61
src/PublicApi/CatalogItemEndpoints/ListPaged.cs
Normal file
61
src/PublicApi/CatalogItemEndpoints/ListPaged.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using Ardalis.ApiEndpoints;
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Specifications;
|
||||
using Swashbuckle.AspNetCore.Annotations;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
|
||||
{
|
||||
public class ListPaged : BaseAsyncEndpoint<ListPagedCatalogItemRequest, ListPagedCatalogItemResponse>
|
||||
{
|
||||
private readonly IAsyncRepository<CatalogItem> _itemRepository;
|
||||
private readonly IUriComposer _uriComposer;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public ListPaged(IAsyncRepository<CatalogItem> itemRepository,
|
||||
IUriComposer uriComposer,
|
||||
IMapper mapper)
|
||||
{
|
||||
_itemRepository = itemRepository;
|
||||
_uriComposer = uriComposer;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
[HttpGet("api/catalog-items")]
|
||||
[SwaggerOperation(
|
||||
Summary = "List Catalog Items (paged)",
|
||||
Description = "List Catalog Items (paged)",
|
||||
OperationId = "catalog-items.ListPaged",
|
||||
Tags = new[] { "CatalogItemEndpoints" })
|
||||
]
|
||||
public override async Task<ActionResult<ListPagedCatalogItemResponse>> HandleAsync([FromQuery]ListPagedCatalogItemRequest request)
|
||||
{
|
||||
var response = new ListPagedCatalogItemResponse(request.CorrelationId());
|
||||
|
||||
var filterSpec = new CatalogFilterSpecification(request.CatalogBrandId, request.CatalogTypeId);
|
||||
int totalItems = await _itemRepository.CountAsync(filterSpec);
|
||||
|
||||
var pagedSpec = new CatalogFilterPaginatedSpecification(
|
||||
skip: request.PageIndex * request.PageSize,
|
||||
take: request.PageSize,
|
||||
brandId: request.CatalogBrandId,
|
||||
typeId: request.CatalogTypeId);
|
||||
|
||||
var items = await _itemRepository.ListAsync(pagedSpec);
|
||||
|
||||
response.CatalogItems.AddRange(items.Select(_mapper.Map<CatalogItemDto>));
|
||||
foreach (CatalogItemDto item in response.CatalogItems)
|
||||
{
|
||||
item.PictureUri = _uriComposer.ComposePicUri(item.PictureUri);
|
||||
}
|
||||
response.PageCount = int.Parse(Math.Ceiling((decimal)totalItems / request.PageSize).ToString());
|
||||
|
||||
return Ok(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user