diff --git a/storeapi/base/impl/WinMockContext.cpp b/storeapi/base/impl/WinMockContext.cpp index 322cceeec..a4d39decc 100644 --- a/storeapi/base/impl/WinMockContext.cpp +++ b/storeapi/base/impl/WinMockContext.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include "WinRTHelpers.hpp" @@ -35,9 +36,6 @@ namespace { IAsyncOperation call(winrt::hstring relativePath, UrlParams const& params = {}); -/// Creates a product from a JsonObject containing the relevant information. -WinMockContext::Product fromJson(JsonObject const& obj); - /// Translates a textual representation of a purchase transaction result into an /// instance of the PurchaseStatus enum. StoreApi::PurchaseStatus translate(winrt::hstring const& purchaseStatus); @@ -69,7 +67,7 @@ std::vector WinMockContext::GetProducts( result.reserve(products.Size()); for (const IJsonValue& product : products) { JsonObject p = product.GetObject(); - result.emplace_back(fromJson(p)); + result.emplace_back(WinMockContext::Product{p}); } return result; @@ -131,6 +129,22 @@ void WinMockContext::Product::PromptUserForPurchase( }); } +WinMockContext::Product::Product(JsonObject const& json) + : storeID{winrt::to_string(json.GetNamedString(L"StoreID"))}, + title{winrt::to_string(json.GetNamedString(L"Title"))}, + description{winrt::to_string(json.GetNamedString(L"Description"))}, + productKind{winrt::to_string(json.GetNamedString(L"ProductKind"))}, + expirationDate{}, + isInUserCollection{json.GetNamedBoolean(L"IsInUserCollection")} + +{ + std::chrono::system_clock::time_point tp{}; + std::stringstream ss{ + winrt::to_string(json.GetNamedString(L"ExpirationDate"))}; + ss >> std::chrono::parse("%FT%T%Tz", tp); + expirationDate = tp; +} + namespace { // Returns the mock server endpoint address and port by reading the environment // variable UP4W_MS_STORE_MOCK_ENDPOINT or localhost:9 if the variable is unset. @@ -177,20 +191,6 @@ IAsyncOperation call(winrt::hstring relativePath, co_return JsonObject::Parse(contents); } -WinMockContext::Product fromJson(JsonObject const& obj) { - std::chrono::system_clock::time_point tp{}; - std::stringstream ss{winrt::to_string(obj.GetNamedString(L"ExpirationDate"))}; - ss >> std::chrono::parse("%FT%T%Tz", tp); - - return WinMockContext::Product{ - winrt::to_string(obj.GetNamedString(L"StoreID")), - winrt::to_string(obj.GetNamedString(L"Title")), - winrt::to_string(obj.GetNamedString(L"Description")), - winrt::to_string(obj.GetNamedString(L"ProductKind")), - tp, - obj.GetNamedBoolean(L"IsInUserCollection")}; -} - StoreApi::PurchaseStatus translate(winrt::hstring const& purchaseStatus) { if (purchaseStatus == L"Succeeded") { return PurchaseStatus::Succeeded; diff --git a/storeapi/base/impl/WinMockContext.hpp b/storeapi/base/impl/WinMockContext.hpp index cf088a041..7dc0cfc54 100644 --- a/storeapi/base/impl/WinMockContext.hpp +++ b/storeapi/base/impl/WinMockContext.hpp @@ -13,6 +13,10 @@ #include "../Purchase.hpp" +namespace winrt::Windows::Data::Json { +class JsonObject; +} + namespace StoreApi::impl { class WinMockContext { @@ -50,17 +54,10 @@ class WinMockContext { void PromptUserForPurchase(PurchaseCallback callback) const; public: + /// Creates a product from a JsonObject obtained from a call to the mock + /// server containing the relevant information. + explicit Product(winrt::Windows::Data::Json::JsonObject const& json); Product() = default; - Product(std::string&& storeID, std::string&& title, - std::string&& description, std::string&& productKind, - std::chrono::system_clock::time_point expirationDate, - bool isInUserCollection) - : storeID(std::move(storeID)), - title(std::move(title)), - description(std::move(description)), - productKind(std::move(productKind)), - expirationDate(std::move(expirationDate)), - isInUserCollection(isInUserCollection) {} }; // Returns a collection of products matching the supplied [kinds] and [ids].