Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ecommerce common standalone library #1

Merged
merged 4 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
namespace Kentico.Xperience.Ecommerce.Common.ContentItemSynchronization;

/// <summary>
/// Parameters for content item creation in synchronization
/// Interface for synchronized content items.
/// </summary>
public class ContentItemAddParams
{
public required ContentItemSynchronizationBase ContentItem { get; set; }

public required string LanguageName { get; set; }

public int UserID { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,29 @@

namespace Kentico.Xperience.Ecommerce.Common.ContentItemSynchronization;

/// <summary>
/// Service for content items management
/// </summary>
public class ContentItemServiceBase : IContentItemService
michalJakubis marked this conversation as resolved.
Show resolved Hide resolved
{
protected readonly IContentItemManagerFactory contentItemManagerFactory;
protected readonly IContentQueryExecutor contentQueryExecutor;
protected readonly IContentQueryResultMapper contentQueryResultMapper;
protected readonly IContentItemManagerFactory ContentItemManagerFactory;

protected readonly IContentQueryExecutor ContentQueryExecutor;

protected readonly IContentQueryResultMapper ContentQueryResultMapper;


public ContentItemServiceBase(
IContentItemManagerFactory contentItemManagerFactory,
IContentQueryExecutor contentQueryExecutor,
IContentQueryResultMapper contentQueryResultMapper)
{
this.contentItemManagerFactory = contentItemManagerFactory;
this.contentQueryExecutor = contentQueryExecutor;
this.contentQueryResultMapper = contentQueryResultMapper;
ContentItemManagerFactory = contentItemManagerFactory;
ContentQueryExecutor = contentQueryExecutor;
ContentQueryResultMapper = contentQueryResultMapper;
}


public async Task<int> AddContentItem(ContentItemAddParams addParams)
martinfbluesoftcz marked this conversation as resolved.
Show resolved Hide resolved
{
ArgumentNullException.ThrowIfNull(addParams);
Expand All @@ -29,7 +36,7 @@ public async Task<int> AddContentItem(ContentItemAddParams addParams)
addParams.LanguageName);
var itemData = new ContentItemData(addParams.ContentItem.ToDict());

var contentItemManager = contentItemManagerFactory.Create(addParams.UserID);
var contentItemManager = ContentItemManagerFactory.Create(addParams.UserID);
int itemID = await contentItemManager.Create(createParams, itemData);
await contentItemManager.TryPublish(itemID, addParams.LanguageName);

Expand All @@ -39,7 +46,7 @@ public async Task<int> AddContentItem(ContentItemAddParams addParams)

public async Task<bool> UpdateContentItem(ContentItemUpdateParams updateParams)
{
var contentItemManager = contentItemManagerFactory.Create(updateParams.UserID);
var contentItemManager = ContentItemManagerFactory.Create(updateParams.UserID);
var versionStatus = updateParams.VersionStatus;

// If content item version is not draft, create draft in order to edit it
Expand All @@ -66,44 +73,46 @@ public async Task<bool> UpdateContentItem(ContentItemUpdateParams updateParams)
};
}


public async Task<IEnumerable<T>> GetContentItems<T>(string contentType, Action<ContentTypeQueryParameters> queryParams)
where T : IContentItemFieldsSource, new()
{
var builder = new ContentItemQueryBuilder()
.ForContentType(contentType, queryParams);

return await contentQueryExecutor.GetResult(builder, contentQueryResultMapper.Map<T>);
return await ContentQueryExecutor.GetResult(builder, ContentQueryResultMapper.Map<T>);
}


public async Task<IEnumerable<T>> GetContentItems<T>(string contentType)
where T : IContentItemFieldsSource, new()
{
return await GetContentItems<T>(contentType, 0);
}
where T : IContentItemFieldsSource, new() => await GetContentItems<T>(contentType, 0);


public async Task<IEnumerable<T>> GetContentItems<T>(string contentType, int linkedItemsLevel)
where T : IContentItemFieldsSource, new()
{
var builder = new ContentItemQueryBuilder()
.ForContentType(contentType, config => config.WithLinkedItems(linkedItemsLevel));

return await contentQueryExecutor.GetResult(builder, contentQueryResultMapper.Map<T>);
return await ContentQueryExecutor.GetResult(builder, ContentQueryResultMapper.Map<T>);
}


public async Task DeleteContentItem(int contentItemID, string languageName, int userID)
{
var contentItemManager = contentItemManagerFactory.Create(userID);
var contentItemManager = ContentItemManagerFactory.Create(userID);
await contentItemManager.Delete(contentItemID, languageName);
}


public async Task DeleteContentItems(IEnumerable<int> contentItemIDs, string languageName, int userID)
{
if (!contentItemIDs.Any())
{
return;
}

var contentItemManager = contentItemManagerFactory.Create(userID);
var contentItemManager = ContentItemManagerFactory.Create(userID);

foreach (int contentItemID in contentItemIDs)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace Kentico.Xperience.Ecommerce.Common.ContentItemSynchronization;

/// <summary>
/// Synchronization base for content items
/// Synchronization base for content items.
/// </summary>
public abstract class ContentItemSynchronizationBase
{
Expand All @@ -22,7 +22,7 @@ public abstract class ContentItemSynchronizationBase


/// <summary>
/// Content item display name with max length of <see cref="NAME_LENGTH"/>
/// Content item display name with max length of <see cref="NAME_LENGTH"/>.
/// </summary>
public string DisplayName =>
DisplayNameInternal.Length > NAME_LENGTH ? (DisplayNameInternal?[..NAME_LENGTH] ?? string.Empty) : DisplayNameInternal;
Expand All @@ -32,7 +32,7 @@ public abstract class ContentItemSynchronizationBase
/// Generate dictionary where keys are property names and values are property values.
/// <see cref="ContentItemSynchronizationBase"/> properties are excluded.
/// </summary>
/// <returns></returns>
/// <returns>Dictionary where key = property name and value = property value.</returns>
public virtual Dictionary<string, object?> ToDict()
{
var baseProperties = typeof(ContentItemSynchronizationBase)
Expand Down Expand Up @@ -112,6 +112,7 @@ protected bool ReferenceModified(IEnumerable<IContentItemFieldsSource> contentIt
return false;
}


protected void SetPropsIfDiff<T>(T source, T dest, string key, Dictionary<string, object?> props)
{
if ((source is null && dest is not null) || (source is not null && !source.Equals(dest)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
namespace Kentico.Xperience.Ecommerce.Common.ContentItemSynchronization;

/// <summary>
/// Parameters for content item update in synchronization
/// Parameters for content item update in synchronization.
/// </summary>
public class ContentItemUpdateParams
{
public required Dictionary<string, object?> ContentItemParams { get; set; }

public required int ContentItemID { get; set; }

public required string LanguageName { get; set; }

public required int UserID { get; set; }

public required VersionStatus VersionStatus { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
namespace Kentico.Xperience.Ecommerce.Common.ContentItemSynchronization;

/// <summary>
///
/// Interface for synchronized content items.
/// </summary>
public interface IContentItemBase : IContentItemFieldsSource
{
string ContentTypeName { get; }

string DisplayName { get; }

string ShopifyObjectID { get; }

int ContentItemIdentifier { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using CMS.ContentEngine;

namespace Kentico.Xperience.Ecommerce.Common.ContentItemSynchronization;

/// <summary>
/// Interface for content items management
/// </summary>
public interface IContentItemService
{
/// <summary>
/// Add and publish content item.
/// </summary>
/// <param name="addParams"></param>
/// <returns>ID of created content item.</returns>
Task<int> AddContentItem(ContentItemAddParams addParams);


/// <summary>
/// Get content items of given content type and query params.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="contentType"></param>
/// <param name="queryParams"></param>
/// <returns>Collection of content items.</returns>
Task<IEnumerable<T>> GetContentItems<T>(string contentType, Action<ContentTypeQueryParameters> queryParams)
where T : IContentItemFieldsSource, new();


/// <summary>
/// Get content items of given content type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="contentType"></param>
/// <returns>Collection of content items.</returns>
Task<IEnumerable<T>> GetContentItems<T>(string contentType)
where T : IContentItemFieldsSource, new();


/// <summary>
/// Get content items of given content type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="contentType"></param>
/// <param name="linkedItemsLevel">>Max. level for linked items</param>
/// <returns>Collection of content items.</returns>
Task<IEnumerable<T>> GetContentItems<T>(string contentType, int linkedItemsLevel)
where T : IContentItemFieldsSource, new();


/// <summary>
/// Updates content item.
/// </summary>
/// <param name="updateParams"></param>
/// <returns>True when update succeeds, else False.</returns>
Task<bool> UpdateContentItem(ContentItemUpdateParams updateParams);


/// <summary>
/// Deletes content item.
/// </summary>
/// <param name="contentItemID"></param>
/// <param name="languageName"></param>
/// <param name="userID"></param>
/// <returns></returns>
Task DeleteContentItem(int contentItemID, string languageName, int userID);


/// <summary>
/// Delete content items.
/// </summary>
/// <param name="contentItemIDs">Content type IDs</param>
/// <param name="languageName"></param>
/// <param name="userID"></param>
/// <returns></returns>
Task DeleteContentItems(IEnumerable<int> contentItemIDs, string languageName, int userID);
}

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Kentico.Xperience.Ecommerce.Common.ContentItemSynchronization.Interfaces;
namespace Kentico.Xperience.Ecommerce.Common.ContentItemSynchronization;

/// <summary>
/// Interface to set external identifier for api objects
/// Interface to set external identifier for api objects.
/// </summary>
/// <typeparam name="TType"></typeparam>
public interface IItemIdentifier<out TType>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
using CMS.ContentEngine;

namespace Kentico.Xperience.Ecommerce.Common.ContentItemSynchronization.Interfaces;
namespace Kentico.Xperience.Ecommerce.Common.ContentItemSynchronization;

public interface ISynchronizationItem<TContentItem>
where TContentItem : IContentItemBase, new()
{
/// <summary>
/// Fills modifiedProps with modified properties compared to contentItem
/// Fills modifiedProps with modified properties compared to contentItem.
/// </summary>
/// <param name="contentItem"></param>
/// <param name="modifiedProps"></param>
/// <returns>True if any property was modified. Otherwise False</returns>
/// <returns>True if any property was modified. Otherwise False.</returns>
public bool GetModifiedProperties(TContentItem contentItem, out Dictionary<string, object?> modifiedProps);
}


/// <summary>
/// Interface for synchronization between data in Dto and content items
/// Interface for synchronization between data in Dto and content items.
/// </summary>
/// <typeparam name="TDto"></typeparam>
/// <typeparam name="TContentItem"></typeparam>
Expand All @@ -31,10 +31,10 @@ public interface ISynchronizationItem<TDto, in TContentItem>


/// <summary>
/// Fills modifiedProps with modified properties compared to contentItem
/// Fills modifiedProps with modified properties compared to contentItem.
/// </summary>
/// <param name="contentItem"></param>
/// <param name="modifiedProps"></param>
/// <returns>True if any property was modified. Otherwise False</returns>
/// <returns>True if any property was modified. Otherwise False.</returns>
bool GetModifiedProperties(TContentItem contentItem, out Dictionary<string, object?> modifiedProps);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
using CMS.ContentEngine;
using CMS.Core;

using Kentico.Xperience.Ecommerce.Common.ContentItemSynchronization.Interfaces;

using Path = CMS.IO.Path;

namespace Kentico.Xperience.Ecommerce.Common.ContentItemSynchronization;

/// <summary>
/// Common service base functionality for synchronization of content items.
/// </summary>
/// <param name="httpClientFactory"></param>
public abstract class SynchronizationServiceCommon(IHttpClientFactory httpClientFactory)
{
protected (IEnumerable<TStoreItem> ToCreate, IEnumerable<(TStoreItem StoreItem, TContentItem ContentItem)> ToUpdate,
Expand All @@ -33,6 +35,7 @@ public abstract class SynchronizationServiceCommon(IHttpClientFactory httpClient
return (toCreate, toUpdate, toDelete);
}


protected async Task<ContentItemAssetMetadataWithSource> CreateAssetMetadata(string url, string name)
{
byte[] bytes;
Expand Down
Loading