Skip to content

Commit

Permalink
Fixed store view issue
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalii-bezuhlyi committed Nov 6, 2024
1 parent d5bfdd3 commit 889b250
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Apps.Magento/Actions/BlockActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Apps.Magento.Actions;
[ActionList]
public class BlockActions(InvocationContext invocationContext, IFileManagementClient fileManagementClient) : AppInvocable(invocationContext)
{
[Action("Get all blocks", Description = "Get all blocks")]
[Action("Search blocks", Description = "Retrieve all blocks that match the specified criteria")]
public async Task<BlocksResponse> GetAllBlocksAsync([ActionParameter] StoreViewOptionalIdentifier storeViewIdentifier,
[ActionParameter] FilterBlockRequest filterRequest)
{
Expand Down
2 changes: 1 addition & 1 deletion Apps.Magento/Actions/CategoryActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Apps.Magento.Actions;
[ActionList]
public class CategoryActions(InvocationContext invocationContext) : AppInvocable(invocationContext)
{
[Action("Get all categories", Description = "Get all categories")]
[Action("Search categories", Description = "Retrieve all categories that match the specified criteria")]
public async Task<CategoriesResponse> GetAllCategoriesAsync(
[ActionParameter] StoreViewOptionalIdentifier storeViewIdentifier)
{
Expand Down
2 changes: 1 addition & 1 deletion Apps.Magento/Actions/PageActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Apps.Magento.Actions;
[ActionList]
public class PageActions(InvocationContext invocationContext, IFileManagementClient fileManagementClient) : AppInvocable(invocationContext)
{
[Action("Get all pages", Description = "Get all pages")]
[Action("Search pages", Description = "Retrieve all pages that match the specified criteria")]
public async Task<PagesResponse> GetAllPagesAsync([ActionParameter] StoreViewOptionalIdentifier storeViewIdentifier,
[ActionParameter] FilterPageRequest filterRequest)
{
Expand Down
35 changes: 24 additions & 11 deletions Apps.Magento/Actions/ProductActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Apps.Magento.Actions;
public class ProductActions(InvocationContext invocationContext, IFileManagementClient fileManagementClient)
: AppInvocable(invocationContext)
{
[Action("Get all products", Description = "Get all products")]
[Action("Search products", Description = "Retrieve all products that match the specified criteria")]
public async Task<ProductsResponse> GetAllProductsAsync(
[ActionParameter] StoreViewOptionalIdentifier storeViewIdentifier,
[ActionParameter] FilterProductRequest filterRequest)
Expand Down Expand Up @@ -104,7 +104,7 @@ public async Task<ProductResponse> CreateProductAsync(

[Action("Update product", Description = "Update product by specified SKU")]
public async Task<ProductResponse> UpdateProductBySkuAsync(
[ActionParameter] StoreViewOptionalIdentifier storeViewIdentifier,
[ActionParameter] StoreViewWithAllOptionalIdentifier storeViewIdentifier,
[ActionParameter] ProductIdentifier identifier,
[ActionParameter] UpdateProductRequest updateProductRequest)
{
Expand All @@ -116,7 +116,16 @@ public async Task<ProductResponse> UpdateProductBySkuAsync(
}
}

var product = await GetProductBySkuAsync(storeViewIdentifier, identifier);
var product = await GetProductBySkuAsync(new StoreViewOptionalIdentifier { StoreView = storeViewIdentifier.StoreView == "all" ? "default" : storeViewIdentifier.StoreView }, identifier);

if(updateProductRequest.CustomAttributeKeys != null && updateProductRequest.CustomAttributeValues != null)
{
if(updateProductRequest.CustomAttributeKeys.Count() != updateProductRequest.CustomAttributeValues.Count())
{
throw new ArgumentException("Custom attribute keys and values count must be equal");
}
}

var mergedCustomAttributes = updateProductRequest.CustomAttributeKeys != null &&
updateProductRequest.CustomAttributeValues != null
? updateProductRequest.CustomAttributeKeys.Zip(updateProductRequest.CustomAttributeValues,
Expand All @@ -138,18 +147,22 @@ public async Task<ProductResponse> UpdateProductBySkuAsync(
{
category_links = new List<object>()
},
custom_attributes = mergedCustomAttributes
custom_attributes = mergedCustomAttributes.Select(x => new
{
attribute_code = x.AttributeCode,
value = x.Value
}).ToList()
}
};

var request = new ApiRequest($"/rest/{storeViewIdentifier}/V1/products/{identifier.Sku}", Method.Put, Creds)
.AddBody(body);
return await Client.ExecuteWithErrorHandling<ProductResponse>(request);
}

[Action("Update product as HTML", Description = "Update product by specified SKU with HTML content")]
[Action("Update product from HTML", Description = "Update product by specified SKU with HTML content")]
public async Task<ProductResponse> UpdateProductBySkuAsHtmlAsync(
[ActionParameter] StoreViewOptionalIdentifier storeViewIdentifier,
[ActionParameter] StoreViewWithAllOptionalIdentifier storeViewIdentifier,
[ActionParameter] UpdateProductAsHtmlRequest updateProductAsHtmlRequest)
{
var htmlStream = await fileManagementClient.DownloadAsync(updateProductAsHtmlRequest.File);
Expand All @@ -161,7 +174,7 @@ public async Task<ProductResponse> UpdateProductBySkuAsHtmlAsync(
var productSku = updateProductAsHtmlRequest.ProductSku ?? htmlModel.ResourceId ??
throw new ArgumentException(
"Couldn't find product SKU in the HTML document. Please specify it manually in the optional input.");
var product = await GetProductBySkuAsync(storeViewIdentifier, new ProductIdentifier { Sku = productSku });
var product = await GetProductBySkuAsync(new StoreViewOptionalIdentifier { StoreView = storeViewIdentifier.StoreView == "all" ? "default" : storeViewIdentifier.StoreView }, new ProductIdentifier { Sku = productSku });

foreach (var customAttribute in productModel.CustomAttributes)
{
Expand Down Expand Up @@ -220,11 +233,11 @@ public async Task<ProductResponse> UpdateProductBySkuAsHtmlAsync(
}

[Action("Delete product", Description = "Delete product by specified SKU")]
public async Task DeleteProductBySkuAsync([ActionParameter] StoreViewOptionalIdentifier storeViewIdentifier,
[ActionParameter] ProductIdentifier identifier)
public async Task DeleteProductBySkuAsync([ActionParameter] ProductIdentifier identifier)
{
var endpoint = $"/rest/default/V1/products/{identifier.Sku}";
await Client.ExecuteWithErrorHandling(
new ApiRequest($"/rest/{storeViewIdentifier}/V1/products/{identifier.Sku}", Method.Delete, Creds));
new ApiRequest(endpoint, Method.Delete, Creds));
}

[Action("Add custom attribute", Description = "Add custom attribute to product by specified SKU")]
Expand Down
2 changes: 1 addition & 1 deletion Apps.Magento/Apps.Magento.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<Product>Adobe Commerce - Magento [Beta]</Product>
<Description>Magento is the leading platform for open commerce innovation. It’s designed to be flexible and scalable, able to support businesses of all sizes – from small startups to large enterprises.</Description>
<Version>1.0.3</Version>
<Version>1.0.4</Version>
<AssemblyName>Apps.Magento</AssemblyName>
<RootNamespace>Apps.Magento</RootNamespace>
<PackageId>Apps.Magento</PackageId>
Expand Down
2 changes: 1 addition & 1 deletion Apps.Magento/Connections/ConnectionValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public async ValueTask<ConnectionValidationResponse> ValidateConnection(

try
{
await apiClient.ExecuteWithErrorHandling(new ApiRequest("/rest/default/V1/analytics/link", Method.Get, credentialsProviders));
await apiClient.ExecuteWithErrorHandling(new ApiRequest("/rest/V1/analytics/link", Method.Get, credentialsProviders));
return new ConnectionValidationResponse
{
IsValid = true
Expand Down
2 changes: 1 addition & 1 deletion Apps.Magento/DataSources/StoreViewDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Apps.Magento.DataSources;

public class StoreViewDataSource(InvocationContext invocationContext) : AppInvocable(invocationContext), IAsyncDataSourceHandler
{
public async Task<Dictionary<string, string>> GetDataAsync(DataSourceContext context, CancellationToken cancellationToken)
public virtual async Task<Dictionary<string, string>> GetDataAsync(DataSourceContext context, CancellationToken cancellationToken)
{
var request = new ApiRequest("/rest/default/V1/store/storeViews", Method.Get, Creds);
var storeViews = await Client.ExecuteWithErrorHandling<List<StoreViewDto>>(request);
Expand Down
19 changes: 19 additions & 0 deletions Apps.Magento/DataSources/StoreViewWithAllDataSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Blackbird.Applications.Sdk.Common.Dynamic;
using Blackbird.Applications.Sdk.Common.Invocation;

namespace Apps.Magento.DataSources;

public class StoreViewWithAllDataSource(InvocationContext invocationContext) : StoreViewDataSource(invocationContext)
{
public override async Task<Dictionary<string, string>> GetDataAsync(DataSourceContext context, CancellationToken cancellationToken)
{
var result = await base.GetDataAsync(context, cancellationToken);

if (result.All(x => x.Key != "all"))
{
result.Add("all", "All");
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Apps.Magento.Models.Identifiers;

public class StoreViewOptionalIdentifier
public class StoreViewOptionalIdentifier
{
[Display("Store view", Description = "If you will not specify it it will be default"), DataSource(typeof(StoreViewDataSource))]
public string? StoreView { get; set; }

public override string ToString()
{
return StoreView ?? "default";
return StoreView ?? String.Empty;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Apps.Magento.DataSources;
using Blackbird.Applications.Sdk.Common;
using Blackbird.Applications.Sdk.Common.Dynamic;

namespace Apps.Magento.Models.Identifiers;

public class StoreViewWithAllOptionalIdentifier
{
[Display("Store view", Description = "If you will not specify it it will be default"), DataSource(typeof(StoreViewWithAllDataSource))]
public string? StoreView { get; set; }

public override string ToString()
{
return StoreView ?? "default";
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Apps.Magento.DataSources;
using Blackbird.Applications.Sdk.Common;
using Blackbird.Applications.Sdk.Common.Dynamic;

namespace Apps.Magento.Models.Requests.Products;

public class GetProductAsHtmlRequest
{
[Display("Custom attributes", Description = "Specify custom attributes to include in the HTML file. By default, only name, meta_title, meta_description, meta_keyword and description are included.")]
[Display("Custom attributes", Description = "Specify custom attributes to include in the HTML file. By default, only name, meta_title, meta_description, meta_keyword and description are included."), DataSource(typeof(ProductAttributeDataSource))]
public IEnumerable<string>? CustomAttributes { get; set; }
}
5 changes: 3 additions & 2 deletions Apps.Magento/Models/Responses/Products/ProductResponse.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Apps.Magento.Utils;
using Blackbird.Applications.Sdk.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace Apps.Magento.Models.Responses.Products;

Expand Down Expand Up @@ -183,10 +184,10 @@ public class Content

public class CustomAttribute
{
[Display("Attribute code")]
[Display("Attribute code"), JsonProperty("attribute_code")]
public string AttributeCode { get; set; } = string.Empty;

[Display("Value"), JsonConverter(typeof(StringOrUndefinedConverter))]
[Display("Value"), JsonProperty("value"), JsonConverter(typeof(StringOrUndefinedConverter))]
public string Value { get; set; } = string.Empty;
}

Expand Down
7 changes: 6 additions & 1 deletion Apps.Magento/Utils/HtmlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ public static string GenerateProductHtmlContent(ProductResponse product, List<st
"meta_title", "meta_description", "meta_keyword", "description"
};

var attributesToInclude = customAttributes?.ToHashSet() ?? defaultAttributes;
var attributesToInclude = new HashSet<string>(defaultAttributes);
if (customAttributes != null)
{
attributesToInclude.UnionWith(customAttributes);
}

foreach (var customAttribute in product.CustomAttributes)
{
if (attributesToInclude.Contains(customAttribute.AttributeCode))
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Adobe Commerce is the leading platform for open commerce innovation. It’s desi

### Blocks

- **Get all blocks** - Get all blocks.
- **Search blocks** - Retrieve all blocks that match the specified search criteria.
- **Get block** - Get block by specified ID.
- **Get block as HTML** - Get block by specified ID as HTML.
- **Create block** - Create block with specified data.
Expand All @@ -61,7 +61,7 @@ Adobe Commerce is the leading platform for open commerce innovation. It’s desi

### Pages

- **Get all pages** - Get all pages.
- **Search pages** - Retrieve all pages that match the specified search criteria.
- **Get page** - Get page by specified ID.
- **Get page as HTML** - Get page by specified ID as HTML.
- **Create page** - Create page with specified data.
Expand All @@ -71,7 +71,7 @@ Adobe Commerce is the leading platform for open commerce innovation. It’s desi

### Products

- **Get all products** - Get all products.
- **Search products** - Retrieve all products that match the specified search criteria.
- **Get product** - Get product by specified SKU.
- **Get product as HTML** - Get product by specified SKU as HTML. Using optional parameters you can specify `Custom attributes` that you want to include in the HTML.
- **Create product** - Create product with specified data.
Expand Down

0 comments on commit 889b250

Please sign in to comment.