src/Services/Catalog/Catalog.API/Grpc/CatalogService.cs

(开头部分) 5KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using CatalogApi;
using static CatalogApi.Catalog;

namespace Microsoft.eShopOnContainers.Services.Catalog.API.Grpc;
using Microsoft.Extensions.Logging;

public class CatalogService : CatalogBase
{
    private readonly CatalogContext _catalogContext;
    private readonly CatalogSettings _settings;
    private readonly ILogger _logger;
    
    public CatalogService(CatalogContext dbContext, IOptions<CatalogSettings> settings, ILogger<CatalogService> logger)
    {
        _settings = settings.Value;
        _catalogContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
        _logger = logger;
    }

    public override async Task<CatalogItemResponse> GetItemById(CatalogItemRequest request, ServerCallContext context)
    {
        _logger.LogInformation("Begin grpc call CatalogService.GetItemById for product id {Id}", request.Id);
        if (request.Id <= 0)
        {
            context.Status = new Status(StatusCode.FailedPrecondition, $"Id must be > 0 (received {request.Id})");
            return null;
        }

        var item = await _catalogContext.CatalogItems.SingleOrDefaultAsync(ci => ci.Id == request.Id);
        var baseUri = _settings.PicBaseUrl;
        var azureStorageEnabled = _settings.AzureStorageEnabled;
        item.FillProductUrl(baseUri, azureStorageEnabled: azureStorageEnabled);

        if (item != null)
        {
            return new CatalogItemResponse()
            {
                AvailableStock = item.AvailableStock,
                Description = item.Description,
                Id = item.Id,
                MaxStockThreshold = item.MaxStockThreshold,
                Name = item.Name,
                OnReorder = item.OnReorder,
                PictureFileName = item.PictureFileName,
                PictureUri = item.PictureUri,
                Price = (double)item.Price,
                RestockThreshold = item.RestockThreshold
            };
        }

        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);

后面还有 117 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。