src/Web/WebMVC/Services/BasketService.cs

(开头部分) 4KB

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

namespace Microsoft.eShopOnContainers.WebMVC.Services;

using Microsoft.eShopOnContainers.WebMVC.ViewModels;

public class BasketService : IBasketService
{
    private readonly IOptions<AppSettings> _settings;
    private readonly HttpClient _apiClient;
    private readonly ILogger<BasketService> _logger;
    private readonly string _basketByPassUrl;
    private readonly string _purchaseUrl;

    public BasketService(HttpClient httpClient, IOptions<AppSettings> settings, ILogger<BasketService> logger)
    {
        _apiClient = httpClient;
        _settings = settings;
        _logger = logger;

        _basketByPassUrl = $"{_settings.Value.PurchaseUrl}/b/api/v1/basket";
        _purchaseUrl = $"{_settings.Value.PurchaseUrl}/api/v1";
    }

    public async Task<Basket> GetBasket(ApplicationUser user)
    {
        var uri = API.Basket.GetBasket(_basketByPassUrl, user.Id);
        _logger.LogDebug("[GetBasket] -> Calling {Uri} to get the basket", uri);
        var response = await _apiClient.GetAsync(uri);
        _logger.LogDebug("[GetBasket] -> response code {StatusCode}", response.StatusCode);
        var responseString = await response.Content.ReadAsStringAsync();
        return string.IsNullOrEmpty(responseString) ?
            new Basket() { BuyerId = user.Id } :
            JsonSerializer.Deserialize<Basket>(responseString, new JsonSerializerOptions
            {
                PropertyNameCaseInsensitive = true
            });
    }

    public async Task<Basket> UpdateBasket(Basket basket)
    {
        var uri = API.Basket.UpdateBasket(_basketByPassUrl);

        var basketContent = new StringContent(JsonSerializer.Serialize(basket), System.Text.Encoding.UTF8, "application/json");

        var response = await _apiClient.PostAsync(uri, basketContent);

        response.EnsureSuccessStatusCode();

        return basket;
    }

    public async Task Checkout(BasketDTO basket)
    {
        var uri = API.Basket.CheckoutBasket(_basketByPassUrl);
        var basketContent = new StringContent(JsonSerializer.Serialize(basket), System.Text.Encoding.UTF8, "application/json");
            
        _logger.LogInformation("Uri chechout {uri}", uri);

        var response = await _apiClient.PostAsync(uri, basketContent);

        response.EnsureSuccessStatusCode();
后面还有 60 行代码,解锁后查看完整代码

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

AI 解读 AI

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

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