using FluentValidation; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Infrastructure; using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; using Raven.Client.Documents; using Raven.Client.Documents.Session; using System; using System.Collections.Generic; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; using zero.Commerce.Api; 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; using zero.Web.Filters; namespace zero.Debug.Controllers { public class MigrationController : Controller { private IDocumentStore Raven { get; set; } public MigrationController(IDocumentStore raven) { Raven = raven; } [HttpGet] public async Task SharedEntities() { async Task Handle() where T : IZeroIdEntity { return await SaveAll(); } 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> SaveAll() where T : IZeroIdEntity { HashSet changedIds = new HashSet(); IList items; using (IAsyncDocumentSession session = Raven.OpenAsyncSession()) { items = await session.Query().ToListAsync(); } foreach (T item in items) { if (await Save(item)) { changedIds.Add(item.Id); } } return changedIds; } async Task Save(T model) where T : IZeroIdEntity { IAppAwareEntity appAwareEntity = model as IAppAwareEntity; IZeroEntity zeroEntity = model as IZeroEntity; bool hasChange = false; // set app id if (appAwareEntity != null && appAwareEntity.AppId.IsNullOrEmpty()) { hasChange = true; appAwareEntity.AppId = "shared"; } // set unset Raven Ids foreach (ObjectTraverser.Result item in ObjectTraverser.FindAttribute(model)) { string id = item.Property.GetValue(item.Parent, null) as string; if (String.IsNullOrWhiteSpace(id)) { hasChange = true; item.Property.SetValue(item.Parent, item.Item.Length.HasValue ? IdGenerator.Create(item.Item.Length.Value) : IdGenerator.Create()); } } string userId = HttpContext.User.Claims.FirstOrDefault(x => x.Type == Constants.Auth.Claims.UserId)?.Value; // set default properties if (zeroEntity != null && zeroEntity.CreatedById == null) { hasChange = true; zeroEntity.CreatedDate = zeroEntity.CreatedDate == default ? DateTimeOffset.Now : zeroEntity.CreatedDate; zeroEntity.CreatedById = userId; } if (zeroEntity != null && model is ITranslation) { zeroEntity.Name = ((ITranslation)model).Key; } // update name alias and last modified if (zeroEntity != null && zeroEntity.Alias.IsNullOrEmpty()) { hasChange = true; zeroEntity.Alias = Safenames.Alias(zeroEntity.Name); } if (zeroEntity != null && zeroEntity.LastModifiedById == default) { hasChange = true; zeroEntity.LastModifiedById = userId; } if (!hasChange) { return false; } using IAsyncDocumentSession session = Raven.OpenAsyncSession(); await session.StoreAsync(model); await session.SaveChangesAsync(); return true; } } }