2. API Client Library - Gluzberg/cart-client GitHub Wiki
Content
Initialization
- The API Client library is defined in the
CartClient.Services.ICartServiceinterface and implemented in theCartClient.Services.Imp.CartServiceclass. - Please us dependency injection in order to initialize an instance of the service.
-
If case of a console application:
IServiceProvider serviceProvider = new ServiceCollection() .AddSingleton<IConfigurationService, ConfigurationService>() .AddSingleton<ITokenService, TokenService>() .AddSingleton<ICartService, CartService>() .BuildServiceProvider(); ICartService cartService = serviceProvider.GetService<ICartService>(); -
In case of a web application, please add the following to the
ConfigureServicein theStartupclass:services.AddSingleton<IConfigurationService, ConfigurationService>(); services.AddSingleton<ITokenService, TokenService>(); services.AddSingleton<ICartService, CartService>();
-
Methods
Create Cart
Created cart with the corresponding ID if specified, otherwise the cart is created with a random Guid.
-
Signature
ICartInfo CreateCart(Guid? cartID = null); -
Return Values
ICartInfowith the specifiedcartID- in case of successful creationICartInfowith random Id - in case of no cartID specification and successful creationnull- in case the cart with the specified ID already exists or data storage error
Retrieve Cart
Get the cart with the corresponding ID if such exists, otherwise returns null. .
-
Signature
ICartInfo GetCartInfo(Guid cartId); -
Return Values
ICartInfowith the specifiedcartID- in case of successful retrievalnull- in case a cart with the specified ID does not exist or data storage error
Delete Cart
Deletes the cart of the specified ID and returns true if deleting was successful, false otherwise.
-
Signature
bool DeleteCart(Guid cartId); -
Return Values
true- in case of successful deletingfalse- in case a cart with the specified ID does not exist or data storage error
Add Product to Cart
Adds the specified product and returns true if adding was successful, false otherwise.
-
Signature
bool AddProduct(Guid cartId, IProductInfo product); -
Return Values
true- in case of successful product addingfalse- in one of the following cases:- A cart with the specified cart ID does not exist
- A cart with the specified cart ID already has a product with the specified product ID
- Data storage error
Edit Product in Cart
Modifies the specified product and returns true if modifying was successful, false otherwise.
-
Signature
bool EditProduct(Guid cartId, IProductInfo product); -
Return Values
true- in case of successful product editingfalse- in one of the following cases:- A cart with the specified cart ID does not exist
- A cart with the specified cart ID does not have a product with the specified product ID
- Data storage error
Remove Product from Cart
Removes the specified product by ID and returns true if removing was successful, false otherwise.
-
Signature
bool RemoveProduct(Guid cartId, Guid productId); -
Return Values
true- in case of successful product removingfalse- in one of the following cases:- A cart with the specified cart ID does not exist
- A cart with the specified cart ID does not have a product with the specified product ID
- Data storage error