Skip to content

Commit 2979bf3

Browse files
Add ShoppingListService
1 parent 46dc192 commit 2979bf3

1 file changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.commercetools.sync.services;
2+
3+
import io.sphere.sdk.client.SphereClient;
4+
import io.sphere.sdk.commands.UpdateAction;
5+
import io.sphere.sdk.shoppinglists.ShoppingList;
6+
import io.sphere.sdk.shoppinglists.ShoppingListDraft;
7+
8+
import javax.annotation.Nonnull;
9+
import javax.annotation.Nullable;
10+
import java.util.List;
11+
import java.util.Map;
12+
import java.util.Optional;
13+
import java.util.Set;
14+
import java.util.concurrent.CompletionStage;
15+
16+
public interface ShoppingListService {
17+
18+
/**
19+
* Filters out the keys which are already cached and fetches only the not-cached shopping list keys from the CTP
20+
* project defined in an injected {@link SphereClient} and stores a mapping for every shopping list to id in
21+
* the cached map of keys -> ids and returns this cached map.
22+
*
23+
* <p>Note: If all the supplied keys are already cached, the cached map is returned right away with no request to
24+
* CTP.
25+
*
26+
* @param keys the shopping list keys to fetch and cache the ids for.
27+
*
28+
* @return {@link CompletionStage}&lt;{@link Map}&gt; in which the result of it's completion contains a map of all
29+
* shopping list keys -&gt; ids
30+
*/
31+
@Nonnull
32+
CompletionStage<Map<String, String>> cacheKeysToIds(@Nonnull Set<String> keys);
33+
34+
/**
35+
* Given a {@code key}, this method first checks if a cached map of shopping list keys -&gt; ids is not empty.
36+
* If not, it returns a completed future that contains an optional that contains what this key maps to in
37+
* the cache. If the cache is empty, the method populates the cache with the mapping of all shopping list keys
38+
* to ids in the CTP project, by querying the CTP project for all shopping lists.
39+
*
40+
* <p>After that, the method returns a {@link CompletionStage}&lt;{@link Optional}&lt;{@link String}&gt;&gt;
41+
* in which the result of it's completion could contain an
42+
* {@link Optional} with the id inside of it or an empty {@link Optional} if no {@link ShoppingList} was
43+
* found in the CTP project with this key.
44+
*
45+
* @param key the key by which a {@link ShoppingList} id should be fetched from the CTP
46+
* project.
47+
* @return {@link CompletionStage}&lt;{@link Optional}&lt;{@link String}&gt;&gt; in which the result of its
48+
* completion could contain an {@link Optional} with the id inside of it or an empty {@link Optional} if no
49+
* {@link ShoppingList} was found in the CTP project with this key.
50+
*/
51+
@Nonnull
52+
CompletionStage<Optional<String>> fetchCachedStateId(@Nullable final String key);
53+
54+
/**
55+
* Given a {@link Set} of state keys, this method fetches a set of all the states, matching given set of
56+
* keys in the CTP project, defined in an injected {@link SphereClient}. A mapping of the key to the id
57+
* of the fetched states is persisted in an in-memory map.
58+
*
59+
* @param shoppingListKeys set of shopping list keys to fetch matching shopping lists by.
60+
* @return {@link CompletionStage}&lt;{@link Map}&gt; in which the result of it's completion contains a {@link Set}
61+
* of all matching shopping lists.
62+
*/
63+
@Nonnull
64+
CompletionStage<Set<ShoppingList>> fetchMatchingShoppingListsByKeys(@Nonnull final Set<String> shoppingListKeys);
65+
66+
/**
67+
* Given a shopping list key, this method fetches a shopping list that matches given key in the CTP project defined
68+
* in a potentially injected {@link SphereClient}. If there is no matching shopping list, an empty {@link Optional}
69+
* will be returned in the returned future. A mapping of the key to the id of the fetched shopping list is persisted
70+
* in an in -memory map.
71+
*
72+
* @param key the key of the shopping list to fetch.
73+
* @return {@link CompletionStage}&lt;{@link Optional}&gt; in which the result of it's completion contains an
74+
* {@link Optional} that contains the matching {@link ShoppingList} if exists, otherwise empty.
75+
*/
76+
@Nonnull
77+
CompletionStage<Optional<ShoppingList>> fetchShoppingList(@Nullable final String key);
78+
79+
/**
80+
* Given a resource draft of type {@link ShoppingListDraft}, this method attempts to create a resource
81+
* {@link ShoppingList} based on it in the CTP project defined by the sync options.
82+
*
83+
* <p>A completion stage containing an empty option and the error callback will be triggered in those cases:
84+
* <ul>
85+
* <li>the draft has a blank key</li>
86+
* <li>the create request fails on CTP</li>
87+
* </ul>
88+
*
89+
* <p>On the other hand, if the resource gets created successfully on CTP, then the created resource's id and
90+
* key are cached and the method returns a {@link CompletionStage} in which the result of it's completion
91+
* contains an instance {@link Optional} of the resource which was created.
92+
*
93+
* @param shoppingListDraft the resource draft to create a resource based off of.
94+
* @return a {@link CompletionStage} containing an optional with the created resource if successful otherwise an
95+
* empty optional.
96+
*/
97+
@Nonnull
98+
CompletionStage<Optional<ShoppingList>> createShoppingList(@Nonnull final ShoppingListDraft shoppingListDraft);
99+
100+
/**
101+
* Given a {@link ShoppingList} and a {@link List}&lt;{@link UpdateAction}&lt;{@link ShoppingList}&gt;&gt;, this
102+
* method issues an update request with these update actions on this {@link ShoppingList} in the CTP project defined
103+
* in a potentially injected {@link SphereClient}. This method returns {
104+
* @link CompletionStage}&lt;{@link ShoppingList}&gt; in which the result of it's completion contains an instance of
105+
* the {@link ShoppingList} which was updated in the CTP project.
106+
*
107+
* @param shoppingList the {@link ShoppingList} to update.
108+
* @param updateActions the update actions to update the {@link ShoppingList} with.
109+
* @return {@link CompletionStage}&lt;{@link ShoppingList}&gt; containing as a result of it's completion an instance
110+
* of the {@link ShoppingList} which was updated in the CTP project or a
111+
* {@link io.sphere.sdk.models.SphereException}.
112+
*/
113+
@Nonnull
114+
CompletionStage<ShoppingList> updateShoppingList(@Nonnull final ShoppingList shoppingList,
115+
@Nonnull final List<UpdateAction<ShoppingList>> updateActions);
116+
117+
}

0 commit comments

Comments
 (0)