学海无涯

导航

客户端URL路径组织模式

namespace WebMVC.Infrastructure;

public static class API
{

    public static class Purchase
    {
        public static string AddItemToBasket(string baseUri) => $"{baseUri}/basket/items";
        public static string UpdateBasketItem(string baseUri) => $"{baseUri}/basket/items";

        public static string GetOrderDraft(string baseUri, string basketId) => $"{baseUri}/order/draft/{basketId}";
    }

    public static class Basket
    {
        public static string GetBasket(string baseUri, string basketId) => $"{baseUri}/{basketId}";
        public static string UpdateBasket(string baseUri) => baseUri;
        public static string CheckoutBasket(string baseUri) => $"{baseUri}/checkout";
        public static string CleanBasket(string baseUri, string basketId) => $"{baseUri}/{basketId}";
    }

    public static class Order
    {
        public static string GetOrder(string baseUri, string orderId)
        {
            return $"{baseUri}/{orderId}";
        }

        public static string GetAllMyOrders(string baseUri)
        {
            return baseUri;
        }

        public static string AddNewOrder(string baseUri)
        {
            return $"{baseUri}/new";
        }

        public static string CancelOrder(string baseUri)
        {
            return $"{baseUri}/cancel";
        }

        public static string ShipOrder(string baseUri)
        {
            return $"{baseUri}/ship";
        }
    }

    public static class Catalog
    {
        public static string GetAllCatalogItems(string baseUri, int page, int take, int? brand, int? type)
        {
            var filterQs = "";

            if (type.HasValue)
            {
                var brandQs = (brand.HasValue) ? brand.Value.ToString() : string.Empty;
                filterQs = $"/type/{type.Value}/brand/{brandQs}";

            }
            else if (brand.HasValue)
            {
                var brandQs = (brand.HasValue) ? brand.Value.ToString() : string.Empty;
                filterQs = $"/type/all/brand/{brandQs}";
            }
            else
            {
                filterQs = string.Empty;
            }

            return $"{baseUri}items{filterQs}?pageIndex={page}&pageSize={take}";
        }

        public static string GetAllBrands(string baseUri)
        {
            return $"{baseUri}catalogBrands";
        }

        public static string GetAllTypes(string baseUri)
        {
            return $"{baseUri}catalogTypes";
        }
    }
}

  使用

namespace Microsoft.eShopOnContainers.WebMVC.Services;

using Microsoft.eShopOnContainers.WebMVC.ViewModels;

public class OrderingService : IOrderingService
{
    private HttpClient _httpClient;
    private readonly string _remoteServiceBaseUrl;
    private readonly IOptions<AppSettings> _settings;


    public OrderingService(HttpClient httpClient, IOptions<AppSettings> settings)
    {
        _httpClient = httpClient;
        _settings = settings;

        _remoteServiceBaseUrl = $"{settings.Value.PurchaseUrl}/o/api/v1/orders";
    }

    async public Task<Order> GetOrder(ApplicationUser user, string id)
    {
        var uri = API.Order.GetOrder(_remoteServiceBaseUrl, id);

        var responseString = await _httpClient.GetStringAsync(uri);

        var response = JsonSerializer.Deserialize<Order>(responseString, new JsonSerializerOptions
        {
            PropertyNameCaseInsensitive = true
        });

        return response;
    }

  

 

posted on 2022-09-24 11:36  宁静致远.  阅读(24)  评论(0编辑  收藏  举报