Skip to content

Commit

Permalink
- Primary constructor is applied on Public Api Endpoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadyNagy committed Aug 12, 2024
1 parent b33e238 commit 4bf9634
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 103 deletions.
18 changes: 5 additions & 13 deletions src/PublicApi/AuthEndpoints/AuthenticateEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,10 @@ namespace Microsoft.eShopWeb.PublicApi.AuthEndpoints;
/// <summary>
/// Authenticates a user
/// </summary>
public class AuthenticateEndpoint : Endpoint<AuthenticateRequest, AuthenticateResponse>
{
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly ITokenClaimsService _tokenClaimsService;

public AuthenticateEndpoint(SignInManager<ApplicationUser> signInManager,
public class AuthenticateEndpoint(SignInManager<ApplicationUser> signInManager,
ITokenClaimsService tokenClaimsService)
{
_signInManager = signInManager;
_tokenClaimsService = tokenClaimsService;
}

: Endpoint<AuthenticateRequest, AuthenticateResponse>
{
public override void Configure()
{
Post("api/authenticate");
Expand All @@ -43,7 +35,7 @@ public override async Task<AuthenticateResponse> ExecuteAsync(AuthenticateReques
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, set lockoutOnFailure: true
//var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true);
var result = await _signInManager.PasswordSignInAsync(request.Username, request.Password, false, true);
var result = await signInManager.PasswordSignInAsync(request.Username, request.Password, false, true);

response.Result = result.Succeeded;
response.IsLockedOut = result.IsLockedOut;
Expand All @@ -53,7 +45,7 @@ public override async Task<AuthenticateResponse> ExecuteAsync(AuthenticateReques

if (result.Succeeded)
{
response.Token = await _tokenClaimsService.GetTokenAsync(request.Username);
response.Token = await tokenClaimsService.GetTokenAsync(request.Username);
}

return response;
Expand Down
16 changes: 4 additions & 12 deletions src/PublicApi/CatalogBrandEndpoints/CatalogBrandListEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,9 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogBrandEndpoints;
/// <summary>
/// List Catalog Brands
/// </summary>
public class CatalogBrandListEndpoint : EndpointWithoutRequest<ListCatalogBrandsResponse>
public class CatalogBrandListEndpoint(IRepository<CatalogBrand> catalogBrandRepository, AutoMapper.IMapper mapper)
: EndpointWithoutRequest<ListCatalogBrandsResponse>
{
private readonly IRepository<CatalogBrand> _catalogBrandRepository;
private readonly AutoMapper.IMapper _mapper;

public CatalogBrandListEndpoint(IRepository<CatalogBrand> catalogBrandRepository, AutoMapper.IMapper mapper)
{
_catalogBrandRepository = catalogBrandRepository;
_mapper = mapper;
}

public override void Configure()
{
Get("api/catalog-brands");
Expand All @@ -35,9 +27,9 @@ public override async Task<ListCatalogBrandsResponse> ExecuteAsync(CancellationT
{
var response = new ListCatalogBrandsResponse();

var items = await _catalogBrandRepository.ListAsync(ct);
var items = await catalogBrandRepository.ListAsync(ct);

response.CatalogBrands.AddRange(items.Select(_mapper.Map<CatalogBrandDto>));
response.CatalogBrands.AddRange(items.Select(mapper.Map<CatalogBrandDto>));

return response;
}
Expand Down
15 changes: 3 additions & 12 deletions src/PublicApi/CatalogItemEndpoints/CatalogItemGetByIdEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,9 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints;
/// <summary>
/// Get a Catalog Item by Id
/// </summary>
public class CatalogItemGetByIdEndpoint
public class CatalogItemGetByIdEndpoint(IRepository<CatalogItem> itemRepository, IUriComposer uriComposer)
: Endpoint<GetByIdCatalogItemRequest, Results<Ok<GetByIdCatalogItemResponse>, NotFound>>
{
private readonly IRepository<CatalogItem> _itemRepository;
private readonly IUriComposer _uriComposer;

public CatalogItemGetByIdEndpoint(IRepository<CatalogItem> itemRepository, IUriComposer uriComposer)
{
_itemRepository = itemRepository;
_uriComposer = uriComposer;
}

public override void Configure()
{
Get("api/catalog-items/{catalogItemId}");
Expand All @@ -36,7 +27,7 @@ public override async Task<Results<Ok<GetByIdCatalogItemResponse>, NotFound>> Ex
{
var response = new GetByIdCatalogItemResponse(request.CorrelationId());

var item = await _itemRepository.GetByIdAsync(request.CatalogItemId, ct);
var item = await itemRepository.GetByIdAsync(request.CatalogItemId, ct);
if (item is null)
return TypedResults.NotFound();

Expand All @@ -47,7 +38,7 @@ public override async Task<Results<Ok<GetByIdCatalogItemResponse>, NotFound>> Ex
CatalogTypeId = item.CatalogTypeId,
Description = item.Description,
Name = item.Name,
PictureUri = _uriComposer.ComposePicUri(item.PictureUri),
PictureUri = uriComposer.ComposePicUri(item.PictureUri),
Price = item.Price
};
return TypedResults.Ok(response);
Expand Down
23 changes: 7 additions & 16 deletions src/PublicApi/CatalogItemEndpoints/CatalogItemListPagedEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,10 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints;
/// <summary>
/// List Catalog Items (paged)
/// </summary>
public class CatalogItemListPagedEndpoint : Endpoint<ListPagedCatalogItemRequest, ListPagedCatalogItemResponse>
public class CatalogItemListPagedEndpoint(IRepository<CatalogItem> itemRepository, IUriComposer uriComposer,
AutoMapper.IMapper mapper)
: Endpoint<ListPagedCatalogItemRequest, ListPagedCatalogItemResponse>
{
private readonly IRepository<CatalogItem> _itemRepository;
private readonly IUriComposer _uriComposer;
private readonly AutoMapper.IMapper _mapper;

public CatalogItemListPagedEndpoint(IRepository<CatalogItem> itemRepository, IUriComposer uriComposer, AutoMapper.IMapper mapper)
{
_itemRepository = itemRepository;
_uriComposer = uriComposer;
_mapper = mapper;
}

public override void Configure()
{
Get("api/catalog-items");
Expand All @@ -42,20 +33,20 @@ public override async Task<ListPagedCatalogItemResponse> ExecuteAsync(ListPagedC
var response = new ListPagedCatalogItemResponse(request.CorrelationId());

var filterSpec = new CatalogFilterSpecification(request.CatalogBrandId, request.CatalogTypeId);
int totalItems = await _itemRepository.CountAsync(filterSpec, ct);
int totalItems = await itemRepository.CountAsync(filterSpec, ct);

var pagedSpec = new CatalogFilterPaginatedSpecification(
skip: request.PageIndex * request.PageSize,
take: request.PageSize,
brandId: request.CatalogBrandId,
typeId: request.CatalogTypeId);

var items = await _itemRepository.ListAsync(pagedSpec, ct);
var items = await itemRepository.ListAsync(pagedSpec, ct);

response.CatalogItems.AddRange(items.Select(_mapper.Map<CatalogItemDto>));
response.CatalogItems.AddRange(items.Select(mapper.Map<CatalogItemDto>));
foreach (CatalogItemDto item in response.CatalogItems)
{
item.PictureUri = _uriComposer.ComposePicUri(item.PictureUri);
item.PictureUri = uriComposer.ComposePicUri(item.PictureUri);
}

if (request.PageSize > 0)
Expand Down
20 changes: 6 additions & 14 deletions src/PublicApi/CatalogItemEndpoints/CreateCatalogItemEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,9 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints;
/// <summary>
/// Creates a new Catalog Item
/// </summary>
public class CreateCatalogItemEndpoint : Endpoint<CreateCatalogItemRequest, CreateCatalogItemResponse>
public class CreateCatalogItemEndpoint(IRepository<CatalogItem> itemRepository, IUriComposer uriComposer)
: Endpoint<CreateCatalogItemRequest, CreateCatalogItemResponse>
{
private readonly IRepository<CatalogItem> _itemRepository;
private readonly IUriComposer _uriComposer;

public CreateCatalogItemEndpoint(IRepository<CatalogItem> itemRepository, IUriComposer uriComposer)
{
_itemRepository = itemRepository;
_uriComposer = uriComposer;
}

public override void Configure()
{
Post("api/catalog-items");
Expand All @@ -39,14 +31,14 @@ public override async Task HandleAsync(CreateCatalogItemRequest request, Cancell
var response = new CreateCatalogItemResponse(request.CorrelationId());

var catalogItemNameSpecification = new CatalogItemNameSpecification(request.Name);
var existingCataloogItem = await _itemRepository.CountAsync(catalogItemNameSpecification, ct);
var existingCataloogItem = await itemRepository.CountAsync(catalogItemNameSpecification, ct);
if (existingCataloogItem > 0)
{
throw new DuplicateException($"A catalogItem with name {request.Name} already exists");
}

var newItem = new CatalogItem(request.CatalogTypeId, request.CatalogBrandId, request.Description, request.Name, request.Price, request.PictureUri);
newItem = await _itemRepository.AddAsync(newItem, ct);
newItem = await itemRepository.AddAsync(newItem, ct);

if (newItem.Id != 0)
{
Expand All @@ -55,7 +47,7 @@ public override async Task HandleAsync(CreateCatalogItemRequest request, Cancell
// In production, we recommend uploading to a blob storage and deliver the image via CDN after a verification process.

newItem.UpdatePictureUri("eCatalog-item-default.png");
await _itemRepository.UpdateAsync(newItem, ct);
await itemRepository.UpdateAsync(newItem, ct);
}

var dto = new CatalogItemDto
Expand All @@ -65,7 +57,7 @@ public override async Task HandleAsync(CreateCatalogItemRequest request, Cancell
CatalogTypeId = newItem.CatalogTypeId,
Description = newItem.Description,
Name = newItem.Name,
PictureUri = _uriComposer.ComposePicUri(newItem.PictureUri),
PictureUri = uriComposer.ComposePicUri(newItem.PictureUri),
Price = newItem.Price
};
response.CatalogItem = dto;
Expand Down
13 changes: 3 additions & 10 deletions src/PublicApi/CatalogItemEndpoints/DeleteCatalogItemEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,8 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints;
/// <summary>
/// Deletes a Catalog Item
/// </summary>
public class DeleteCatalogItemEndpoint : Endpoint<DeleteCatalogItemRequest, Results<Ok<DeleteCatalogItemResponse>, NotFound>>
public class DeleteCatalogItemEndpoint(IRepository<CatalogItem> itemRepository) : Endpoint<DeleteCatalogItemRequest, Results<Ok<DeleteCatalogItemResponse>, NotFound>>
{
private readonly IRepository<CatalogItem> _itemRepository;

public DeleteCatalogItemEndpoint(IRepository<CatalogItem> itemRepository)
{
_itemRepository = itemRepository;
}

public override void Configure()
{
Delete("api/catalog-items/{catalogItemId}");
Expand All @@ -35,11 +28,11 @@ public override async Task<Results<Ok<DeleteCatalogItemResponse>, NotFound>> Exe
{
var response = new DeleteCatalogItemResponse(request.CorrelationId());

var itemToDelete = await _itemRepository.GetByIdAsync(request.CatalogItemId, ct);
var itemToDelete = await itemRepository.GetByIdAsync(request.CatalogItemId, ct);
if (itemToDelete is null)
return TypedResults.NotFound();

await _itemRepository.DeleteAsync(itemToDelete, ct);
await itemRepository.DeleteAsync(itemToDelete, ct);

return TypedResults.Ok(response);
}
Expand Down
18 changes: 5 additions & 13 deletions src/PublicApi/CatalogItemEndpoints/UpdateCatalogItemEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,9 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints;
/// <summary>
/// Updates a Catalog Item
/// </summary>
public class UpdateCatalogItemEndpoint : Endpoint<UpdateCatalogItemRequest, Results<Ok<UpdateCatalogItemResponse>, NotFound>>
public class UpdateCatalogItemEndpoint(IRepository<CatalogItem> itemRepository, IUriComposer uriComposer)
: Endpoint<UpdateCatalogItemRequest, Results<Ok<UpdateCatalogItemResponse>, NotFound>>
{
private readonly IRepository<CatalogItem> _itemRepository;
private readonly IUriComposer _uriComposer;

public UpdateCatalogItemEndpoint(IRepository<CatalogItem> itemRepository, IUriComposer uriComposer)
{
_itemRepository = itemRepository;
_uriComposer = uriComposer;
}

public override void Configure()
{
Put("api/catalog-items");
Expand All @@ -37,7 +29,7 @@ public override async Task<Results<Ok<UpdateCatalogItemResponse>, NotFound>> Exe
{
var response = new UpdateCatalogItemResponse(request.CorrelationId());

var existingItem = await _itemRepository.GetByIdAsync(request.Id, ct);
var existingItem = await itemRepository.GetByIdAsync(request.Id, ct);
if (existingItem == null)
{
return TypedResults.NotFound();
Expand All @@ -48,7 +40,7 @@ public override async Task<Results<Ok<UpdateCatalogItemResponse>, NotFound>> Exe
existingItem.UpdateBrand(request.CatalogBrandId);
existingItem.UpdateType(request.CatalogTypeId);

await _itemRepository.UpdateAsync(existingItem, ct);
await itemRepository.UpdateAsync(existingItem, ct);

var dto = new CatalogItemDto
{
Expand All @@ -57,7 +49,7 @@ public override async Task<Results<Ok<UpdateCatalogItemResponse>, NotFound>> Exe
CatalogTypeId = existingItem.CatalogTypeId,
Description = existingItem.Description,
Name = existingItem.Name,
PictureUri = _uriComposer.ComposePicUri(existingItem.PictureUri),
PictureUri = uriComposer.ComposePicUri(existingItem.PictureUri),
Price = existingItem.Price
};
response.CatalogItem = dto;
Expand Down
17 changes: 4 additions & 13 deletions src/PublicApi/CatalogTypeEndpoints/CatalogTypeListEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,9 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogTypeEndpoints;
/// <summary>
/// List Catalog Types
/// </summary>
public class CatalogTypeListEndpoint : EndpointWithoutRequest<ListCatalogTypesResponse>
public class CatalogTypeListEndpoint(IRepository<CatalogType> catalogTypeRepository, AutoMapper.IMapper mapper)
: EndpointWithoutRequest<ListCatalogTypesResponse>
{
private readonly IRepository<CatalogType> _catalogTypeRepository;
private readonly AutoMapper.IMapper _mapper;

public CatalogTypeListEndpoint(IRepository<CatalogType> catalogTypeRepository, AutoMapper.IMapper mapper)
{
_catalogTypeRepository = catalogTypeRepository;
_mapper = mapper;

}

public override void Configure()
{
Get("api/catalog-types");
Expand All @@ -36,9 +27,9 @@ public override async Task<ListCatalogTypesResponse> ExecuteAsync(CancellationTo
{
var response = new ListCatalogTypesResponse();

var items = await _catalogTypeRepository.ListAsync(ct);
var items = await catalogTypeRepository.ListAsync(ct);

response.CatalogTypes.AddRange(items.Select(_mapper.Map<CatalogTypeDto>));
response.CatalogTypes.AddRange(items.Select(mapper.Map<CatalogTypeDto>));

return response;
}
Expand Down

0 comments on commit 4bf9634

Please sign in to comment.