using Microsoft.AspNetCore.Mvc; using Raven.Client.Documents; using Raven.Client.Documents.Session; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using zero.Commerce.Entities; using zero.Core; using zero.Core.Api; using zero.Core.Attributes; using zero.Core.Entities; using zero.Core.Extensions; using zero.Core.Utils; namespace zero.Debug.Controllers { public partial class MigrationController : Controller { [HttpGet] public async Task CreateBlueprints() { IList apps; using (IAsyncDocumentSession session = Raven.OpenAsyncSession()) { apps = await session.Query().ToListAsync(); } async Task Handle() where T : IZeroEntity { return await CreateBlueprintsForAll(apps); } return Json(new { //Applications = await Handle(), //Categories = await Handle(), //Channels = await Handle(), //Countries = await Handle(), //Currencies = await Handle(), //Customers = await Handle(), //Languages = await Handle(), //MailTemplates = await Handle(), //Manufacturers = await Handle(), //Media = await Handle(), //MediaFolders = await Handle(), //NumberTemplates = await Handle(), //OrderDetailStates = await Handle(), //Orders = await Handle(), //Pages = await Handle(), //ProductProperties = await Handle(), //Products = await Handle(), //RecycleBin = await Handle(), ShippingOptions = await Handle(), //SpaceContents = await Handle(), //TaxRates = await Handle(), //Translations = await Handle(), //UserRoles = await Handle(), //Users = await Handle(), }); } async Task> CreateBlueprintsForAll(IList apps) where T : IZeroEntity { HashSet ids = new HashSet(); IList items; using (IAsyncDocumentSession session = Raven.OpenAsyncSession()) { session.Advanced.MaxNumberOfRequestsPerSession = 10000; items = await session.Query().ToListAsync(); foreach (T item in items) { HashSet variants = ExtractBlueprints(item, apps); if (variants.Count > 0) { ids.Add("blueprint." + item.Id); foreach (IZeroIdEntity variant in variants) { ((IZeroEntity)variant).BlueprintId = item.Id; //await UpdateReferences(variant); await session.StoreAsync(variant); ids.Add(variant.Id); } } } await session.SaveChangesAsync(); } using (IAsyncDocumentSession session = Raven.OpenAsyncSession()) { session.Advanced.MaxNumberOfRequestsPerSession = 10000; items = await session.Query().ToListAsync(); //.Where(x => x.BlueprintId != null).ToListAsync(); foreach (T item in items) { await UpdateReferences(item, session); ids.Add(item.Id); } await session.SaveChangesAsync(); } return ids; } async Task UpdateReferences(IZeroIdEntity entity, IAsyncDocumentSession session) { await Task.Delay(1); List> references = ObjectTraverser.Find(entity); foreach (ObjectTraverser.Result reference in references) { if (reference.Item != null && reference.Item.Ids.Length > 0) { Type type = reference.Item.GetType(); Type generic = type.GetGenericArguments().FirstOrDefault(); string collection = session.Advanced.DocumentStore.Conventions.FindCollectionName(generic); for (int idx = 0; idx < reference.Item.Ids.Length; idx++) { string id = reference.Item.Ids[idx]; IZeroEntity xentity = await session.Query(collectionName: collection).FirstOrDefaultAsync(x => x.BlueprintId == id); if (xentity != null) { reference.Item.Ids[idx] = xentity.Id; } } } } } HashSet ExtractBlueprints(T model, IList apps) where T : IZeroIdEntity { HashSet newModels = new HashSet(); IAppAwareEntity appAwareEntity = model as IAppAwareEntity; IZeroEntity zeroEntity = model as IZeroEntity; if (appAwareEntity != null && appAwareEntity.AppId == "shared") { foreach (IApplication app in apps) { IZeroEntity newSpecificEntity = zeroEntity.Clone(); newSpecificEntity.Id = null; ((IAppAwareEntity)newSpecificEntity).AppId = app.Id; newModels.Add(newSpecificEntity); } } return newModels; } } }