Initial commit - ERP
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using Commerce.Contracts.DTOs;
|
||||
|
||||
namespace Commerce.Domain.Entities;
|
||||
|
||||
public class Product
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public double Price { get; set; }
|
||||
public double Discount { get; set; }
|
||||
public DateTime Created { get; set; }
|
||||
public string CodeName { get; set; } = string.Empty;
|
||||
public bool IsBundle { get; set; }
|
||||
public string? CategoryName { get; set; }
|
||||
public Guid? CategoryId { get; set; }
|
||||
|
||||
public string[] Photos { get; set; } = Array.Empty<string>();
|
||||
|
||||
[ForeignKey("CategoryId")]
|
||||
public virtual Category Category { get; set; } = null!;
|
||||
|
||||
public virtual List<Product> ChildProducts { get; set; } = new();
|
||||
public virtual List<Product>? ParentBundles { get; set; }
|
||||
|
||||
// New nested structure for JSONB
|
||||
public List<ProductTranslation> Translations { get; set; } = new();
|
||||
|
||||
public static ProductVM ToVM(Product product, string? language = null)
|
||||
{
|
||||
var translations = product.Translations?.ToList() ?? new List<ProductTranslation>();
|
||||
List<ProductTranslationVM> selectedTranslations;
|
||||
|
||||
if (string.IsNullOrEmpty(language))
|
||||
{
|
||||
selectedTranslations = translations
|
||||
.Select(t => new ProductTranslationVM
|
||||
{
|
||||
Language = t.Language,
|
||||
Info = new ProductInfoVM
|
||||
{
|
||||
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<ProductTranslationVM>
|
||||
{
|
||||
new ProductTranslationVM
|
||||
{
|
||||
Language = matchedTranslation.Language,
|
||||
Info = new ProductInfoVM
|
||||
{
|
||||
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<ProductTranslationVM>();
|
||||
}
|
||||
|
||||
return new ProductVM
|
||||
{
|
||||
Id = product.Id,
|
||||
Price = product.Price,
|
||||
Discount = product.Discount,
|
||||
Created = product.Created,
|
||||
CodeName = product.CodeName,
|
||||
CategoryName = product.CategoryName,
|
||||
IsBundle = product.IsBundle,
|
||||
CategoryId = product.CategoryId,
|
||||
Photos = product.Photos,
|
||||
Translations = selectedTranslations,
|
||||
ChildProducts = product.ChildProducts?.Select(x => Product.ToVM(x, language)).ToList() ?? new List<ProductVM>(),
|
||||
};
|
||||
}
|
||||
|
||||
public static Product ToEntity(ProductVM productVM)
|
||||
{
|
||||
return new Product
|
||||
{
|
||||
Id = productVM.Id ?? Guid.NewGuid(),
|
||||
Price = productVM.Price ?? 0,
|
||||
Discount = productVM.Discount ?? 0,
|
||||
Created = productVM.Created ?? DateTime.UtcNow,
|
||||
CodeName = productVM.CodeName ?? string.Empty,
|
||||
CategoryName = productVM.CategoryName,
|
||||
IsBundle = productVM.IsBundle,
|
||||
CategoryId = productVM.CategoryId ?? Guid.Empty,
|
||||
Photos = productVM.Photos ?? Array.Empty<string>(),
|
||||
Translations = productVM.Translations?.Select(t => new ProductTranslation
|
||||
{
|
||||
Language = t.Language,
|
||||
Info = new ProductInfo
|
||||
{
|
||||
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(),
|
||||
ChildProducts = new List<Product>(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public class ProductTranslation
|
||||
{
|
||||
public string Language { get; set; } = string.Empty;
|
||||
public ProductInfo Info { get; set; } = new();
|
||||
}
|
||||
|
||||
public class ProductInfo
|
||||
{
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string Description { get; set; } = string.Empty;
|
||||
public List<TechnicalDetail> TechnicalDetails { get; set; } = new();
|
||||
}
|
||||
|
||||
public class TechnicalDetail
|
||||
{
|
||||
public string Key { get; set; } = string.Empty;
|
||||
public string Value { get; set; } = string.Empty;
|
||||
}
|
||||
Reference in New Issue
Block a user