|
|
@ -8,6 +8,7 @@ using Microsoft.EntityFrameworkCore; |
|
|
|
using Microsoft.eShopOnContainers.Services.Catalog.API; |
|
|
|
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure; |
|
|
|
using Microsoft.eShopOnContainers.Services.Catalog.API.Model; |
|
|
|
using Microsoft.eShopOnContainers.Services.Catalog.API.ViewModel; |
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
using Microsoft.Extensions.Options; |
|
|
|
using static CatalogApi.Catalog; |
|
|
@ -26,10 +27,10 @@ namespace Catalog.API.Grpc |
|
|
|
_logger = logger; |
|
|
|
} |
|
|
|
|
|
|
|
public override async Task<CatalogItemResponse> GetItemById(CatalogItemRequest request, ServerCallContext context) |
|
|
|
public override async Task<CatalogItemResponse> GetItemById(CatalogItemRequest request, ServerCallContext context) |
|
|
|
{ |
|
|
|
_logger.LogInformation($"Begin grpc call CatalogService.GetItemById for product id {request.Id}"); |
|
|
|
if (request.Id <=0) |
|
|
|
if (request.Id <= 0) |
|
|
|
{ |
|
|
|
context.Status = new Status(StatusCode.FailedPrecondition, $"Id must be > 0 (received {request.Id})"); |
|
|
|
return null; |
|
|
@ -60,5 +61,119 @@ namespace Catalog.API.Grpc |
|
|
|
context.Status = new Status(StatusCode.NotFound, $"Product with id {request.Id} do not exist"); |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
public override async Task<PaginatedItemsResponse> GetItemsByIds(CatalogItemsRequest request, ServerCallContext context) |
|
|
|
{ |
|
|
|
if (!string.IsNullOrEmpty(request.Ids)) |
|
|
|
{ |
|
|
|
var items = await GetItemsByIdsAsync(request.Ids); |
|
|
|
|
|
|
|
if (!items.Any()) |
|
|
|
{ |
|
|
|
context.Status = new Status(StatusCode.NotFound, $"ids value invalid. Must be comma-separated list of numbers"); |
|
|
|
} |
|
|
|
context.Status = new Status(StatusCode.OK, string.Empty); |
|
|
|
return this.MapToResponse(items); |
|
|
|
} |
|
|
|
|
|
|
|
var totalItems = await _catalogContext.CatalogItems |
|
|
|
.LongCountAsync(); |
|
|
|
|
|
|
|
var itemsOnPage = await _catalogContext.CatalogItems |
|
|
|
.OrderBy(c => c.Name) |
|
|
|
.Skip(request.PageSize * request.PageIndex) |
|
|
|
.Take(request.PageSize) |
|
|
|
.ToListAsync(); |
|
|
|
|
|
|
|
/* The "awesome" fix for testing Devspaces */ |
|
|
|
|
|
|
|
/* |
|
|
|
foreach (var pr in itemsOnPage) { |
|
|
|
pr.Name = "Awesome " + pr.Name; |
|
|
|
} |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
itemsOnPage = ChangeUriPlaceholder(itemsOnPage); |
|
|
|
|
|
|
|
var model = this.MapToResponse(itemsOnPage, totalItems, request.PageIndex, request.PageSize); |
|
|
|
context.Status = new Status(StatusCode.OK, string.Empty); |
|
|
|
|
|
|
|
return model; |
|
|
|
} |
|
|
|
|
|
|
|
private PaginatedItemsResponse MapToResponse(List<CatalogItem> items) |
|
|
|
{ |
|
|
|
return this.MapToResponse(items, items.Count(), 1, items.Count()); |
|
|
|
} |
|
|
|
|
|
|
|
private PaginatedItemsResponse MapToResponse(List<CatalogItem> items, long count, int pageIndex, int pageSize) |
|
|
|
{ |
|
|
|
var result = new PaginatedItemsResponse() |
|
|
|
{ |
|
|
|
Count = count, |
|
|
|
PageIndex = pageIndex, |
|
|
|
PageSize = pageSize, |
|
|
|
}; |
|
|
|
|
|
|
|
items.ForEach(i => result.Data.Add(new CatalogItemResponse() |
|
|
|
{ |
|
|
|
AvailableStock = i.AvailableStock, |
|
|
|
Description = i.Description, |
|
|
|
Id = i.Id, |
|
|
|
MaxStockThreshold = i.MaxStockThreshold, |
|
|
|
Name = i.Name, |
|
|
|
OnReorder = i.OnReorder, |
|
|
|
PictureFileName = i.PictureFileName, |
|
|
|
PictureUri = i.PictureUri, |
|
|
|
RestockThreshold = i.RestockThreshold, |
|
|
|
CatalogBrand = new CatalogApi.CatalogBrand() |
|
|
|
{ |
|
|
|
Id = i.CatalogBrand.Id, |
|
|
|
Name = i.CatalogBrand.Brand, |
|
|
|
}, |
|
|
|
CatalogType = new CatalogApi.CatalogType() |
|
|
|
{ |
|
|
|
Id = i.CatalogType.Id, |
|
|
|
Type = i.CatalogType.Type, |
|
|
|
}, |
|
|
|
Price = (double)i.Price, |
|
|
|
})); |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private async Task<List<CatalogItem>> GetItemsByIdsAsync(string ids) |
|
|
|
{ |
|
|
|
var numIds = ids.Split(',').Select(id => (Ok: int.TryParse(id, out int x), Value: x)); |
|
|
|
|
|
|
|
if (!numIds.All(nid => nid.Ok)) |
|
|
|
{ |
|
|
|
return new List<CatalogItem>(); |
|
|
|
} |
|
|
|
|
|
|
|
var idsToSelect = numIds |
|
|
|
.Select(id => id.Value); |
|
|
|
|
|
|
|
var items = await _catalogContext.CatalogItems.Where(ci => idsToSelect.Contains(ci.Id)).ToListAsync(); |
|
|
|
|
|
|
|
items = ChangeUriPlaceholder(items); |
|
|
|
|
|
|
|
return items; |
|
|
|
} |
|
|
|
|
|
|
|
private List<CatalogItem> ChangeUriPlaceholder(List<CatalogItem> items) |
|
|
|
{ |
|
|
|
var baseUri = _settings.PicBaseUrl; |
|
|
|
var azureStorageEnabled = _settings.AzureStorageEnabled; |
|
|
|
|
|
|
|
foreach (var item in items) |
|
|
|
{ |
|
|
|
item.FillProductUrl(baseUri, azureStorageEnabled: azureStorageEnabled); |
|
|
|
} |
|
|
|
|
|
|
|
return items; |
|
|
|
} |
|
|
|
} |
|
|
|
} |