109 lines
4.1 KiB
C#
109 lines
4.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Commerce.Contracts.DTOs;
|
|
|
|
namespace Commerce.Domain.Entities;
|
|
|
|
public class Category
|
|
{
|
|
public Guid Id { get; set; }
|
|
public string[] Photos { get; set; } = Array.Empty<string>();
|
|
public Guid? ParentCategoryId { get; set; }
|
|
public Category? ParentCategory { get; set; }
|
|
public ICollection<Category> ChildCategories { get; set; } = new List<Category>();
|
|
public bool IsBundle { get; set; }
|
|
|
|
// New nested structure for JSONB
|
|
public List<CategoryTranslation> Translations { get; set; } = new();
|
|
|
|
public static CategoryVM ToVM(Category category, string? language = null)
|
|
{
|
|
var translations = category.Translations?.ToList() ?? new List<CategoryTranslation>();
|
|
List<CategoryTranslationVM> selectedTranslations;
|
|
|
|
if (string.IsNullOrEmpty(language))
|
|
{
|
|
selectedTranslations = translations
|
|
.Select(t => new CategoryTranslationVM
|
|
{
|
|
Language = t.Language,
|
|
Info = new CategoryInfoVM
|
|
{
|
|
Name = t.Info.Name,
|
|
Description = t.Info.Description,
|
|
TechnicalDetails = t.Info.TechnicalDetails?.Select(td => new TechnicalDetailVM { Key = td.Key, Value = td.Value }).ToList() ?? new()
|
|
}
|
|
}).ToList();
|
|
}
|
|
else
|
|
{
|
|
var matchedTranslation = translations
|
|
.FirstOrDefault(t => t.Language == language)
|
|
?? translations.FirstOrDefault(t => t.Language == "en");
|
|
|
|
selectedTranslations = matchedTranslation != null
|
|
? new List<CategoryTranslationVM>
|
|
{
|
|
new CategoryTranslationVM
|
|
{
|
|
Language = matchedTranslation.Language,
|
|
Info = new CategoryInfoVM
|
|
{
|
|
Name = matchedTranslation.Info.Name,
|
|
Description = matchedTranslation.Info.Description,
|
|
TechnicalDetails = matchedTranslation.Info.TechnicalDetails?.Select(td => new TechnicalDetailVM { Key = td.Key, Value = td.Value }).ToList() ?? new()
|
|
}
|
|
}
|
|
}
|
|
: new List<CategoryTranslationVM>();
|
|
}
|
|
|
|
return new CategoryVM
|
|
{
|
|
Id = category.Id,
|
|
Photos = category.Photos,
|
|
ParentCategoryId = category.ParentCategoryId,
|
|
IsBundle = category.IsBundle,
|
|
Translations = selectedTranslations,
|
|
ChildCategories = category.ChildCategories?.Select(x => Category.ToVM(x, language)).ToList() ?? new List<CategoryVM>(),
|
|
};
|
|
}
|
|
|
|
public static Category ToEntity(CategoryVM categoryVM)
|
|
{
|
|
return new Category
|
|
{
|
|
Id = categoryVM.Id ?? Guid.NewGuid(),
|
|
Photos = categoryVM.Photos ?? Array.Empty<string>(),
|
|
ParentCategoryId = categoryVM.ParentCategoryId,
|
|
IsBundle = categoryVM.IsBundle,
|
|
Translations = categoryVM.Translations?.Select(t => new CategoryTranslation
|
|
{
|
|
Language = t.Language,
|
|
Info = new CategoryInfo
|
|
{
|
|
Name = t.Info.Name,
|
|
Description = t.Info.Description,
|
|
TechnicalDetails = t.Info.TechnicalDetails?.Select(td => new TechnicalDetail { Key = td.Key, Value = td.Value }).ToList() ?? new()
|
|
}
|
|
}).ToList() ?? new(),
|
|
ChildCategories = categoryVM.ChildCategories?.Select(x => Category.ToEntity(x)).ToList() ?? new List<Category>(),
|
|
};
|
|
}
|
|
}
|
|
|
|
public class CategoryTranslation
|
|
{
|
|
public string Language { get; set; } = string.Empty;
|
|
public CategoryInfo Info { get; set; } = new();
|
|
}
|
|
|
|
public class CategoryInfo
|
|
{
|
|
public string Name { get; set; } = string.Empty;
|
|
public string Description { get; set; } = string.Empty;
|
|
public List<TechnicalDetail> TechnicalDetails { get; set; } = new();
|
|
}
|