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
6 changes: 2 additions & 4 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
<DisableImplicitNuGetFallbackFolder>true</DisableImplicitNuGetFallbackFolder>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Kentico.Xperience.Admin" Version="28.4.2" />
<PackageVersion Include="Kentico.Xperience.WebApp" Version="28.4.2" />
<PackageVersion Include="kentico.xperience.azurestorage" Version="28.4.2" />
<PackageVersion Include="kentico.xperience.imageprocessing" Version="28.4.2" />
<PackageVersion Include="Kentico.Xperience.Core" Version="28.2.1" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageVersion Include="SonarAnalyzer.CSharp" Version="9.23.0.88079" />
</ItemGroup>
</Project>
43 changes: 43 additions & 0 deletions Kentico.Xperience.Ecommerce.Common.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33516.290
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kentico.Xperience.Ecommerce.Common", "src\Kentico.Xperience.Ecommerce.Common\Kentico.Xperience.Ecommerce.Common.csproj", "{0A775ADD-E5FB-4E81-ABF1-A7689476E788}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{22D98C99-36D2-4591-BF21-B29393898DDF}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
.gitignore = .gitignore
.markdownlint.json = .markdownlint.json
LICENSE.md = LICENSE.md
README.md = README.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{12AA74B2-41DB-4CAD-AE3D-CB8188B5702B}"
ProjectSection(SolutionItems) = preProject
Directory.Build.props = Directory.Build.props
Directory.build.targets = Directory.build.targets
Directory.Packages.props = Directory.Packages.props
global.json = global.json
nuget.config = nuget.config
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0A775ADD-E5FB-4E81-ABF1-A7689476E788}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0A775ADD-E5FB-4E81-ABF1-A7689476E788}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0A775ADD-E5FB-4E81-ABF1-A7689476E788}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0A775ADD-E5FB-4E81-ABF1-A7689476E788}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1B2A20BC-F57B-4AAE-B1C2-E4DC52EFCAC5}
EndGlobalSection
EndGlobal
19 changes: 0 additions & 19 deletions Kentico.Xperience.RepoTemplate.sln

This file was deleted.

Empty file removed src/.gitkeep
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using System.Reflection;
using System.Text;

using CMS.ContentEngine;
using CMS.Helpers;

namespace Kentico.Xperience.Ecommerce.Common.ContentItemSynchronization;

/// <summary>
/// Synchronization base for content items
/// </summary>
public abstract class ContentItemSynchronizationBase
{
private const string CODE_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789";
private const int CODE_LENGTH = 8;
private const int NAME_LENGTH = 100;

protected abstract string DisplayNameInternal { get; }


public abstract string ContentTypeName { get; }


/// <summary>
/// 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;


/// <summary>
/// Generate dictionary where keys are property names and values are property values.
/// <see cref="ContentItemSynchronizationBase"/> properties are excluded.
/// </summary>
/// <returns></returns>
martinfbluesoftcz marked this conversation as resolved.
Show resolved Hide resolved
public virtual Dictionary<string, object?> ToDict()
{
var baseProperties = typeof(ContentItemSynchronizationBase)
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Select(p => p.Name);

var type = GetType();

// Get only properties that are not from IContentItemBase
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy)
.Where(p => !baseProperties.Contains(p.Name));

Dictionary<string, object?> result = [];

foreach (var property in properties)
{
string propertyName = property.Name;
object? propertyValue = property.GetValue(this);

result.Add(propertyName, propertyValue);
}

return result;
}


#pragma warning disable CS1574 // XML comment has cref attribute that could not be resolved
/// <summary>
/// Get content item code name or generate if does not exist.
/// Generated code name consists of <see cref="DisplayName"/> that is transformed to code name using <see cref="ValidationHelper.GetCodeName()"/>
/// and random 8 characters long alphanumeric string. Maximum length will be <see cref="NAME_LENGTH"/>
/// </summary>
/// <returns>
/// Generated code name
/// </returns>
#pragma warning restore CS1574 // XML comment has cref attribute that could not be resolved
public string GenerateCodeName()
{
var random = Random.Shared;

string? codeName = ValidationHelper.GetCodeName(DisplayName);

if (codeName.Length + CODE_LENGTH >= NAME_LENGTH)
{
codeName = codeName[..(NAME_LENGTH - CODE_LENGTH - 1)];
}

var sb = new StringBuilder(codeName);
sb.Append('-');
for (int i = 0; i < CODE_LENGTH; i++)
{
sb.Append(CODE_CHARS[random.Next(CODE_CHARS.Length)]);
}

return sb.ToString();
}


protected bool ReferenceModified(IEnumerable<IContentItemFieldsSource> contentItems, IEnumerable<ContentItemReference> contentItemReferences)
{
if (contentItems.Count() != contentItemReferences.Count())
{
return true;
}

for (int i = 0; i < contentItemReferences.Count(); i++)
{
var referenceObject = contentItemReferences.ElementAtOrDefault(i);
var contentItem = contentItems.ElementAtOrDefault(i);

if (referenceObject?.Identifier != contentItem?.SystemFields?.ContentItemGUID)
{
return true;
}
}

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)))
{
props.TryAdd(key, dest);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using CMS.ContentEngine;

namespace Kentico.Xperience.Ecommerce.Common.ContentItemSynchronization;

/// <summary>
///
martinfbluesoftcz marked this conversation as resolved.
Show resolved Hide resolved
/// </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,29 @@
using CMS.ContentEngine;

namespace Kentico.Xperience.Ecommerce.Common.ContentItemSynchronization;

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);

Task<IEnumerable<T>> GetContentItems<T>(string contentType, Action<ContentTypeQueryParameters> queryParams)
where T : IContentItemFieldsSource, new();

Task<IEnumerable<T>> GetContentItems<T>(string contentType)
where T : IContentItemFieldsSource, new();

Task<IEnumerable<T>> GetContentItems<T>(string contentType, int linkedItemsLevel)
where T : IContentItemFieldsSource, new();

Task<bool> UpdateContentItem(ContentItemUpdateParams updateParams);

Task DeleteContentItem(int contentItemID, string languageName, int userID);

Task DeleteContentItems(IEnumerable<int> contentItemIDs, string languageName, int userID);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Kentico.Xperience.Ecommerce.Common.ContentItemSynchronization.Interfaces;
martinfbluesoftcz marked this conversation as resolved.
Show resolved Hide resolved

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

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

public interface ISynchronizationItem<TContentItem>
where TContentItem : IContentItemBase, new()
{
/// <summary>
/// Fills modifiedProps with modified properties compared to contentItem
martinfbluesoftcz marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <param name="contentItem"></param>
/// <param name="modifiedProps"></param>
/// <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
/// </summary>
/// <typeparam name="TDto"></typeparam>
/// <typeparam name="TContentItem"></typeparam>
public interface ISynchronizationItem<TDto, in TContentItem>
where TDto : class
where TContentItem : IContentItemFieldsSource
{
/// <summary>
/// Dto item, typically object from Ecommerce platform API
/// </summary>
public TDto Item { get; set; }


/// <summary>
/// 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>
bool GetModifiedProperties(TContentItem contentItem, out Dictionary<string, object?> modifiedProps);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Kentico.Xperience.Ecommerce.Common.ContentItemSynchronization;

/// <summary>
/// Parameters for content item creation in synchronization
/// </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
@@ -0,0 +1,15 @@
using CMS.ContentEngine;

namespace Kentico.Xperience.Ecommerce.Common.ContentItemSynchronization;

/// <summary>
/// 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; }
}
Loading
Loading