From 96ffdab4c6ac7d7a859271bf63a89e58693ce1f0 Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Mon, 12 Oct 2020 12:14:09 +0200 Subject: [PATCH] remove mapper --- .../Entities/Applications/Application.cs | 8 +- zero.Core/Mapper/DefaultMapper.cs | 261 ------------------ zero.Core/Mapper/IMapperConfig.cs | 9 - .../MapperServiceCollectionExtensions.cs | 37 --- zero.Core/Options/MapperOptions.cs | 12 - zero.Core/Options/ZeroOptions.cs | 9 - .../Controllers/AuthenticationController.cs | 2 +- zero.Web/Controllers/BackofficeController.cs | 65 ----- zero.Web/Controllers/MediaController.cs | 19 +- zero.Web/Defaults/ZeroBackofficePlugin.cs | 11 - zero.Web/Mapper/ApplicationMapperConfig.cs | 50 ---- zero.Web/Mapper/CountryMapperConfig.cs | 45 --- zero.Web/Mapper/LanguageMapperConfig.cs | 43 --- zero.Web/Mapper/MediaMapperConfig.cs | 74 ----- zero.Web/Mapper/SpaceMapperConfig.cs | 27 -- zero.Web/Mapper/TranslationMapperConfig.cs | 38 --- zero.Web/Mapper/UserMapperConfig.cs | 95 ------- zero.Web/ZeroBuilder.cs | 5 - zero.Web/ZeroVue.cs | 20 +- 19 files changed, 27 insertions(+), 803 deletions(-) delete mode 100644 zero.Core/Mapper/DefaultMapper.cs delete mode 100644 zero.Core/Mapper/IMapperConfig.cs delete mode 100644 zero.Core/Mapper/MapperServiceCollectionExtensions.cs delete mode 100644 zero.Core/Options/MapperOptions.cs delete mode 100644 zero.Web/Mapper/ApplicationMapperConfig.cs delete mode 100644 zero.Web/Mapper/CountryMapperConfig.cs delete mode 100644 zero.Web/Mapper/LanguageMapperConfig.cs delete mode 100644 zero.Web/Mapper/MediaMapperConfig.cs delete mode 100644 zero.Web/Mapper/SpaceMapperConfig.cs delete mode 100644 zero.Web/Mapper/TranslationMapperConfig.cs delete mode 100644 zero.Web/Mapper/UserMapperConfig.cs diff --git a/zero.Core/Entities/Applications/Application.cs b/zero.Core/Entities/Applications/Application.cs index b41b35e1..1ec1a731 100644 --- a/zero.Core/Entities/Applications/Application.cs +++ b/zero.Core/Entities/Applications/Application.cs @@ -16,10 +16,10 @@ namespace zero.Core.Entities public string Email { get; set; } /// - public string ImageId { get; set; } + public Ref ImageId { get; set; } /// - public string IconId { get; set; } + public Ref IconId { get; set; } /// public Uri[] Domains { get; set; } = new Uri[] { }; @@ -45,12 +45,12 @@ namespace zero.Core.Entities /// /// Image of the application /// - string ImageId { get; set; } + Ref ImageId { get; set; } /// /// Simple image of the application (used as favicon) /// - string IconId { get; set; } + Ref IconId { get; set; } /// /// All assigned domains for this application diff --git a/zero.Core/Mapper/DefaultMapper.cs b/zero.Core/Mapper/DefaultMapper.cs deleted file mode 100644 index 3a5cb9af..00000000 --- a/zero.Core/Mapper/DefaultMapper.cs +++ /dev/null @@ -1,261 +0,0 @@ -using Raven.Client.Documents; -using Raven.Client.Documents.Session; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Threading.Tasks; -using zero.Core.Entities; -using zero.Core.Options; - -namespace zero.Core.Mapper -{ - public class DefaultMapper : IMapper - { - MapCache Maps = new MapCache(); - - IDocumentStore Raven = null; - - IZeroOptions Options = null; - - - public DefaultMapper(IDocumentStore raven, IZeroOptions options) - { - Raven = raven; - Options = options; - - foreach (IMapperConfig config in options.Mapper.GetAllItems()) - { - config.Configure(this); - } - } - - - /// - public void Add() where T : IMapperConfig - { - T config = Activator.CreateInstance(); - config.Configure(this); - } - - - /// - public void Add(Assembly assembly) - { - Type configType = typeof(IMapperConfig); - IEnumerable types = assembly.GetTypes().Where(t => t.IsAssignableFrom(configType)); - - foreach (Type type in types) - { - IMapperConfig config = (IMapperConfig)Activator.CreateInstance(type); - config.Configure(this); - } - } - - - /// - public async Task Map(TSource source) - { - if (source == null) - { - return default; - } - - return await Map(source, Activator.CreateInstance()); - } - - - /// - public async Task Map(TSource source, TTarget target) - { - if (source == null) - { - return target; - } - - if (target == null) - { - return await Map(source); - } - - await Maps.Call(source, target); - - return target; - } - - - /// - public async Task> Map(IEnumerable source) - { - IList target = new List(); - - foreach (TSource item in source) - { - target.Add(await Map(item, Activator.CreateInstance())); - } - - return target; - } - - - /// - public async Task> Map(ListResult source) - { - IList target = new List(); - - foreach (TSource item in source.Items) - { - target.Add(await Map(item, Activator.CreateInstance())); - } - - return new ListResult(target, source.TotalItems, source.Page, source.PageSize) - { - Statistics = source.Statistics - }; - } - - - /// - public async Task> Map(EntityResult source) - { - return new EntityResult() - { - IsSuccess = source.IsSuccess, - Errors = source.Errors, - Model = await Map(source.Model, Activator.CreateInstance()) - }; - } - - - /// - public void CreateMap(Action map) - { - Maps.Add((source, target) => - { - map((TSource)source, (TTarget)target); - return Task.CompletedTask; - }); - } - - - /// - public void CreateMap(Func map) - { - Maps.Add(async (source, target) => - { - await map((TSource)source, (TTarget)target, null); - }); - } - - - /// - /// Internal mappings cache - /// - class MapCache : Dictionary>> - { - int Index = 0; - - Dictionary TypeMappings = new Dictionary(); - - - /// - /// Adds a new action for the mapping - /// - public void Add(Func map) - { - int sourceKey = GetKeyForType(typeof(TSource)); - int targetKey = GetKeyForType(typeof(TTarget)); - - if (!ContainsKey(sourceKey)) - { - Add(sourceKey, new Dictionary>()); - } - - if (!this[sourceKey].ContainsKey(targetKey)) - { - this[sourceKey].Add(targetKey, map); - } - } - - - /// - /// Get action from defined types - /// - public async Task Call(TSource source, TTarget target) - { - int sourceKey = GetKeyForType(typeof(TSource)); - int targetKey = GetKeyForType(typeof(TTarget)); - - if (!ContainsKey(sourceKey) || !this[sourceKey].ContainsKey(targetKey)) - { - return; - } - - Func result = this[sourceKey][targetKey]; - - await result(source, target); - } - - - /// - /// Get stored key for this type - /// - int GetKeyForType(Type type) - { - string name = type.FullName; - - if (TypeMappings.TryGetValue(name, out int key)) - { - return key; - } - - int index = Index++; - TypeMappings.Add(name, index); - return index; - } - } - } - - - public interface IMapper - { - /// - void Add() where T : IMapperConfig; - - /// - void Add(Assembly assembly); - - /// - /// Map an object to the target type - /// - Task Map(TSource source); - - /// - /// Map an object to the target type given an already existing target instance - /// - Task Map(TSource source, TTarget target); - - /// - /// Map a list of objects to the target type - /// - Task> Map(IEnumerable source); - - /// - /// Map a list result containing objects to the target type - /// - Task> Map(ListResult source); - - /// - /// Map an entity result to the target type - /// - Task> Map(EntityResult source); - - /// - /// Create a mapping from source to target object - /// - void CreateMap(Action map); - - /// - void CreateMap(Func map); - } -} \ No newline at end of file diff --git a/zero.Core/Mapper/IMapperConfig.cs b/zero.Core/Mapper/IMapperConfig.cs deleted file mode 100644 index 444f59ee..00000000 --- a/zero.Core/Mapper/IMapperConfig.cs +++ /dev/null @@ -1,9 +0,0 @@ -using Raven.Client.Documents; - -namespace zero.Core.Mapper -{ - public interface IMapperConfig - { - void Configure(IMapper config); - } -} diff --git a/zero.Core/Mapper/MapperServiceCollectionExtensions.cs b/zero.Core/Mapper/MapperServiceCollectionExtensions.cs deleted file mode 100644 index 17141ca0..00000000 --- a/zero.Core/Mapper/MapperServiceCollectionExtensions.cs +++ /dev/null @@ -1,37 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; -using Raven.Client.Documents; -using System; -using zero.Core.Options; - -namespace zero.Core.Mapper -{ - public static class MapperServiceCollectionExtensions - { - public static IServiceCollection AddMapper(this IServiceCollection services) where T : class, IMapper - { - return services.AddSingleton(); - } - - public static IServiceCollection AddMapper(this IServiceCollection services, Action options) where T : class, IMapper, new() - { - return services.AddSingleton(factory => - { - T mapper = new T(); - options(mapper); - return mapper; - }); - } - - public static IServiceCollection AddMapper(this IServiceCollection services) => services.AddMapper(); - - public static IServiceCollection AddMapper(this IServiceCollection services, Action options) - { - return services.AddSingleton(factory => - { - DefaultMapper mapper = new DefaultMapper(factory.GetService(), factory.GetService()); - options(mapper); - return mapper; - }); - } - } -} diff --git a/zero.Core/Options/MapperOptions.cs b/zero.Core/Options/MapperOptions.cs deleted file mode 100644 index 4a6354c2..00000000 --- a/zero.Core/Options/MapperOptions.cs +++ /dev/null @@ -1,12 +0,0 @@ -using zero.Core.Mapper; - -namespace zero.Core.Options -{ - public class MapperOptions : ZeroBackofficeCollection, IZeroCollectionOptions - { - public void Add() where T : IMapperConfig, new() - { - Items.Add(new T()); - } - } -} diff --git a/zero.Core/Options/ZeroOptions.cs b/zero.Core/Options/ZeroOptions.cs index 25dca9f8..d9d93cc7 100644 --- a/zero.Core/Options/ZeroOptions.cs +++ b/zero.Core/Options/ZeroOptions.cs @@ -23,7 +23,6 @@ namespace zero.Core.Options Permissions = new PermissionOptions(); Settings = new SettingsOptions(); Spaces = new SpaceOptions(); - Mapper = new MapperOptions(); } /// @@ -72,9 +71,6 @@ namespace zero.Core.Options /// public SpaceOptions Spaces { get; private set; } - - /// - public MapperOptions Mapper { get; private set; } } @@ -160,10 +156,5 @@ namespace zero.Core.Options /// /// SpaceOptions Spaces { get; } - - /// - /// - /// - MapperOptions Mapper { get; } } } diff --git a/zero.Web/Controllers/AuthenticationController.cs b/zero.Web/Controllers/AuthenticationController.cs index 198ad26b..f62ab895 100644 --- a/zero.Web/Controllers/AuthenticationController.cs +++ b/zero.Web/Controllers/AuthenticationController.cs @@ -26,7 +26,7 @@ namespace zero.Web.Controllers /// public async Task GetUser() { - return await As(await Api.GetUser()); + return Edit(await Api.GetUser()); } diff --git a/zero.Web/Controllers/BackofficeController.cs b/zero.Web/Controllers/BackofficeController.cs index da54a5fb..d0891b70 100644 --- a/zero.Web/Controllers/BackofficeController.cs +++ b/zero.Web/Controllers/BackofficeController.cs @@ -8,7 +8,6 @@ using System.Threading.Tasks; using zero.Core.Api; using zero.Core.Entities; using zero.Core.Identity; -using zero.Core.Mapper; using zero.Core.Options; using zero.Web.Filters; using zero.Web.Models; @@ -19,11 +18,9 @@ namespace zero.Web.Controllers [ServiceFilter(typeof(ModelStateValidationFilterAttribute))] public abstract class BackofficeController : Controller { - IMapper _mapper; IZeroOptions _options; IToken _token; - protected IMapper Mapper => _mapper ?? (_mapper = HttpContext?.RequestServices?.GetService()); protected IZeroOptions Options => _options ?? (_options = HttpContext?.RequestServices?.GetService()); protected IToken Token => _token ?? (_token = HttpContext?.RequestServices?.GetService()); @@ -175,67 +172,5 @@ namespace zero.Web.Controllers return base.File(stream, contentType, filename, true); } - - - - protected async Task As(T model) where TTarget : class, new() where T : IZeroEntity - { - if (model == null) - { - return new StatusCodeResult(404); - } - - if (Mapper == null) - { - // TODO show error with help on how to inject mapper in constructor + base constructor - } - - TTarget result = await Mapper.Map(model); - - if (result is ObsoleteEditModel) - { - ObsoleteEditModel editModel = result as ObsoleteEditModel; - //model.CanEdit = - } - - return Json(result); - } - - - protected async Task As(IEnumerable model) where TTarget : class, new() where T : IZeroEntity - { - if (model == null) - { - return new StatusCodeResult(404); - } - - return Json(await Mapper.Map(model)); - } - - - protected async Task As(ListResult model) where TTarget : class, new() where T : IZeroEntity - { - if (model == null) - { - return new StatusCodeResult(404); - } - - return Json(await Mapper.Map(model)); - } - - protected async Task As(EntityResult model) where TTarget : class, new() where T : IZeroEntity - { - if (model == null) - { - return new StatusCodeResult(404); - } - - return Json(await Mapper.Map(model)); - } - - protected async Task Map(T model) where TTarget : class, new() - { - return await Mapper.Map(model); - } } } diff --git a/zero.Web/Controllers/MediaController.cs b/zero.Web/Controllers/MediaController.cs index a0b43dbc..fcb9e250 100644 --- a/zero.Web/Controllers/MediaController.cs +++ b/zero.Web/Controllers/MediaController.cs @@ -58,15 +58,18 @@ namespace zero.Web.Controllers public async Task GetAll([FromQuery] MediaListQuery query) { - ListResult items = await Mapper.Map(await Api.GetByQuery(query)); - IList hierarchy = null; - IEnumerable folders = new List(); - IMediaFolder folder = null; - - if (query.Page < 2) + ListResult items = (await Api.GetByQuery(query)).MapTo(x => new MediaListModel() { - folders = await MediaFolderApi.GetAll(query.FolderId); - } + Id = x.Id, + IsFolder = false, + Name = x.Name, + Size = x.Size, + Source = x.PreviewSource ?? x.Source, + Type = x.Type + }); + + IList hierarchy = null; + IMediaFolder folder = null; if (!String.IsNullOrEmpty(query.FolderId)) { diff --git a/zero.Web/Defaults/ZeroBackofficePlugin.cs b/zero.Web/Defaults/ZeroBackofficePlugin.cs index ebb99a34..9adb7e90 100644 --- a/zero.Web/Defaults/ZeroBackofficePlugin.cs +++ b/zero.Web/Defaults/ZeroBackofficePlugin.cs @@ -3,13 +3,10 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using zero.Core.Api; using zero.Core.Entities; -using zero.Core.Extensions; using zero.Core.Messages; using zero.Core.Options; using zero.Core.Plugins; -using zero.Core.Utils; using zero.Core.Validation; -using zero.Web.Mapper; using zero.Web.Sections; namespace zero.Web.Defaults @@ -80,14 +77,6 @@ namespace zero.Web.Defaults zero.Settings.AddGroup(); //zero.Settings.AddGroup(); - zero.Mapper.Add(); - zero.Mapper.Add(); - zero.Mapper.Add(); - zero.Mapper.Add(); - zero.Mapper.Add(); - zero.Mapper.Add(); - zero.Mapper.Add(); - zero.Permissions.AddCollection(); zero.Permissions.AddCollection(); zero.Permissions.AddCollection(); diff --git a/zero.Web/Mapper/ApplicationMapperConfig.cs b/zero.Web/Mapper/ApplicationMapperConfig.cs deleted file mode 100644 index f986008d..00000000 --- a/zero.Web/Mapper/ApplicationMapperConfig.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System.Threading.Tasks; -using zero.Core.Entities; -using zero.Core.Mapper; -using zero.Web.Models; - -namespace zero.Web.Mapper -{ - public class ApplicationMapperConfig : IMapperConfig - { - /// - public void Configure(IMapper config) - { - config.CreateMap((source, target) => - { - target.Id = source.Id; - target.Name = source.Name; - target.FullName = source.FullName; - target.Email = source.Email; - target.IsActive = source.IsActive; - target.CreatedDate = source.CreatedDate; - //target.Domains = source.Domains; - target.ImageId = source.ImageId; - target.IconId = source.IconId; - target.Features = source.Features; - }); - - config.CreateMap((source, target) => - { - target.Name = source.Name; - target.FullName = source.FullName; - target.Email = source.Email; - target.IsActive = source.IsActive; - //target.Domains = source.Domains; - target.ImageId = source.ImageId; - target.IconId = source.IconId; - target.Features = source.Features; - }); - - config.CreateMap((source, target) => - { - target.Id = source.Id; - target.Name = source.Name; - target.FullName = source.FullName; - target.IsActive = source.IsActive; - //target.Domains = source.Domains; - target.ImageId = source.ImageId; - }); - } - } -} diff --git a/zero.Web/Mapper/CountryMapperConfig.cs b/zero.Web/Mapper/CountryMapperConfig.cs deleted file mode 100644 index bea476f7..00000000 --- a/zero.Web/Mapper/CountryMapperConfig.cs +++ /dev/null @@ -1,45 +0,0 @@ -using Raven.Client.Documents; -using zero.Core.Entities; -using zero.Core.Mapper; -using zero.Web.Models; - -namespace zero.Web.Mapper -{ - public class CountryMapperConfig : IMapperConfig - { - /// - public void Configure(IMapper config) - { - config.CreateMap((source, target) => - { - target.Id = source.Id; - target.Name = source.Name; - target.IsActive = source.IsActive; - target.CreatedDate = source.CreatedDate; - target.IsPreferred = source.IsPreferred; - //target.LanguageId = source.LanguageId; - target.Code = source.Code; - }); - - config.CreateMap((source, target) => - { - target.Id = source.Id; - target.Name = source.Name; - target.IsActive = source.IsActive; - target.CreatedDate = source.CreatedDate; - target.IsPreferred = source.IsPreferred; - // target.LanguageId = source.LanguageId; - target.Code = source.Code; - }); - - config.CreateMap((source, target) => - { - target.Id = source.Id; - target.Name = source.Name; - target.IsActive = source.IsActive; - target.IsPreferred = source.IsPreferred; - target.Code = source.Code; - }); - } - } -} diff --git a/zero.Web/Mapper/LanguageMapperConfig.cs b/zero.Web/Mapper/LanguageMapperConfig.cs deleted file mode 100644 index f3776f1a..00000000 --- a/zero.Web/Mapper/LanguageMapperConfig.cs +++ /dev/null @@ -1,43 +0,0 @@ -using zero.Core.Entities; -using zero.Core.Mapper; -using zero.Web.Models; - -namespace zero.Web.Mapper -{ - public class LanguageMapperConfig : IMapperConfig - { - /// - public void Configure(IMapper config) - { - config.CreateMap((source, target) => - { - target.Id = source.Id; - target.Name = source.Name; - target.IsActive = source.IsActive; - target.IsDefault = source.IsDefault; - target.InheritedLanguageId = source.InheritedLanguageId; - target.Code = source.Code; - target.CreatedDate = source.CreatedDate; - }); - - config.CreateMap((source, target) => - { - target.Name = source.Name; - target.IsActive = source.IsActive; - target.IsDefault = source.IsDefault; - target.InheritedLanguageId = source.InheritedLanguageId; - target.Code = source.Code; - }); - - config.CreateMap((source, target) => - { - target.Id = source.Id; - target.Name = source.Name; - target.IsActive = source.IsActive; - target.IsDefault = source.IsDefault; - target.InheritedLanguageId = source.InheritedLanguageId; - target.Code = source.Code; - }); - } - } -} diff --git a/zero.Web/Mapper/MediaMapperConfig.cs b/zero.Web/Mapper/MediaMapperConfig.cs deleted file mode 100644 index ee83104e..00000000 --- a/zero.Web/Mapper/MediaMapperConfig.cs +++ /dev/null @@ -1,74 +0,0 @@ -using zero.Core.Entities; -using zero.Core.Mapper; -using zero.Web.Models; - -namespace zero.Web.Mapper -{ - public class MediaMapperConfig : IMapperConfig - { - /// - public void Configure(IMapper config) - { - config.CreateMap((source, target) => - { - target.Id = source.Id; - //target.Name = source.Name; - //target.FullName = source.FullName; - //target.Email = source.Email; - target.IsActive = source.IsActive; - target.CreatedDate = source.CreatedDate; - //target.Domains = source.Domains; - //target.ImageId = source.ImageId; - //target.IconId = source.IconId; - //target.Features = source.Features; - }); - - config.CreateMap((source, target) => - { - //target.Name = source.Name; - //target.FullName = source.FullName; - //target.Email = source.Email; - //target.IsActive = source.IsActive; - //target.Domains = source.Domains; - //target.ImageId = source.ImageId; - //target.IconId = source.IconId; - //target.Features = source.Features; - }); - - config.CreateMap((source, target) => - { - target.Id = source.Id; - target.IsFolder = false; - target.Name = source.Name; - target.Type = source.Type; - target.Size = source.Size; - target.Source = source.PreviewSource ?? source.Source; - }); - - config.CreateMap((source, target) => - { - target.Id = source.Id; - target.Name = source.Name; - target.IsFolder = true; - }); - - - config.CreateMap((source, target) => - { - target.Id = source.Id; - target.Name = source.Name; - target.IsActive = source.IsActive; - target.CreatedDate = source.CreatedDate; - target.ParentId = source.ParentId; - }); - - config.CreateMap((source, target) => - { - target.Name = source.Name; - target.IsActive = source.IsActive; - target.CreatedDate = source.CreatedDate; - target.ParentId = source.ParentId; - }); - } - } -} diff --git a/zero.Web/Mapper/SpaceMapperConfig.cs b/zero.Web/Mapper/SpaceMapperConfig.cs deleted file mode 100644 index 99f16371..00000000 --- a/zero.Web/Mapper/SpaceMapperConfig.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.Linq; -using zero.Core.Entities; -using zero.Core.Identity; -using zero.Core.Extensions; -using zero.Web.Models; -using Raven.Client.Documents; -using zero.Core.Mapper; - -namespace zero.Web.Mapper -{ - public class SpaceMapperConfig : IMapperConfig - { - /// - public void Configure(IMapper config) - { - //config.CreateMap((source, target) => - //{ - // target.Id = source.Id; - // target.Name = source.Name; - // target.IsActive = source.IsActive; - // target.CreatedDate = source.CreatedDate; - // target. - //}); - } - } -} diff --git a/zero.Web/Mapper/TranslationMapperConfig.cs b/zero.Web/Mapper/TranslationMapperConfig.cs deleted file mode 100644 index 35da2104..00000000 --- a/zero.Web/Mapper/TranslationMapperConfig.cs +++ /dev/null @@ -1,38 +0,0 @@ -using zero.Core.Entities; -using zero.Core.Mapper; -using zero.Web.Models; - -namespace zero.Web.Mapper -{ - public class TranslationMapperConfig : IMapperConfig - { - /// - public void Configure(IMapper config) - { - config.CreateMap((source, target) => - { - target.Id = source.Id; - target.CreatedDate = source.CreatedDate; - target.Key = source.Key; - target.Value = source.Value; - target.Display = source.Display; - }); - - config.CreateMap((source, target) => - { - target.IsActive = true; - target.Key = source.Key; - target.Value = source.Value; - target.Display = source.Display; - }); - - config.CreateMap((source, target) => - { - target.Id = source.Id; - target.CreatedDate = source.CreatedDate; - target.Key = source.Key; - target.Value = source.Value; - }); - } - } -} diff --git a/zero.Web/Mapper/UserMapperConfig.cs b/zero.Web/Mapper/UserMapperConfig.cs deleted file mode 100644 index 762d851c..00000000 --- a/zero.Web/Mapper/UserMapperConfig.cs +++ /dev/null @@ -1,95 +0,0 @@ -using System; -using System.Linq; -using zero.Core.Entities; -using zero.Core.Identity; -using zero.Core.Extensions; -using zero.Web.Models; -using Raven.Client.Documents; -using zero.Core.Mapper; - -namespace zero.Web.Mapper -{ - public class UserMapperConfig : IMapperConfig - { - /// - public void Configure(IMapper config) - { - config.CreateMap((source, target) => - { - target.Id = source.Id; - target.Name = source.Name; - target.IsActive = source.IsActive; - target.CreatedDate = source.CreatedDate; - target.IsSuper = source.IsSuper; - target.Email = source.Email; - target.IsEmailConfirmed = source.IsEmailConfirmed; - target.AvatarId = source.AvatarId; - target.LanguageId = source.LanguageId; - target.Roles = source.Roles; - target.Claims = source.Claims; - target.LockoutEnd = source.LockoutEnd; - target.IsLockedOut = source.LockoutEnabled && source.LockoutEnd.HasValue; - }); - - config.CreateMap((source, target) => - { - target.Name = source.Name; - target.IsActive = source.IsActive; - target.IsSuper = source.IsSuper; - target.Email = source.Email; - target.AvatarId = source.AvatarId; - target.LanguageId = source.LanguageId; - target.Roles = source.Roles; - target.Claims = source.Claims; - }); - - config.CreateMap((source, target) => - { - target.Id = source.Id; - target.Name = source.Name; - target.IsActive = source.IsActive && (!source.LockoutEnabled || !source.LockoutEnd.HasValue); - target.Email = source.Email; - target.AvatarId = source.AvatarId; - target.Roles = String.Join(", ", source.Roles); // TODO get name from alias - }); - - config.CreateMap((source, target) => - { - target.Id = source.Id; - target.Name = source.Name; - target.IsActive = source.IsActive; - target.CreatedDate = source.CreatedDate; - target.Description = source.Description; - target.Icon = source.Icon; - target.Claims = source.Claims.Cast().ToList(); - }); - - config.CreateMap((source, target) => - { - target.Name = source.Name; - target.IsActive = source.IsActive; - target.Description = source.Description; - target.Icon = source.Icon; - target.Claims = source.Claims.Cast().ToList(); - }); - - config.CreateMap((source, target) => - { - target.Id = source.Id; - target.Name = source.Name; - target.Alias = source.Alias; - target.CountClaims = source.Claims.Count(x => - { - string[] parts = x.Value.Split(':'); - - if (parts.Length < 2) - { - return true; - } - return parts[1] == PermissionsValue.True || parts[1] == PermissionsValue.Read || parts[1] == PermissionsValue.Update; - }); - target.Icon = source.Icon; - }); - } - } -} diff --git a/zero.Web/ZeroBuilder.cs b/zero.Web/ZeroBuilder.cs index 1540bc89..79ec6aa1 100644 --- a/zero.Web/ZeroBuilder.cs +++ b/zero.Web/ZeroBuilder.cs @@ -20,7 +20,6 @@ using zero.Core.Database.Indexes; using zero.Core.Entities; using zero.Core.Extensions; using zero.Core.Identity; -using zero.Core.Mapper; using zero.Core.Options; using zero.Core.Plugins; using zero.Core.Utils; @@ -101,10 +100,6 @@ namespace zero.Web ValidatorOptions.Global.PropertyNameResolver = ValidatorCamelCasePropertyResolver.ResolvePropertyName; - // add default mapper - Services.AddMapper(); - - // add default services Services.AddScoped(); diff --git a/zero.Web/ZeroVue.cs b/zero.Web/ZeroVue.cs index d12095a0..8a082ed9 100644 --- a/zero.Web/ZeroVue.cs +++ b/zero.Web/ZeroVue.cs @@ -1,10 +1,8 @@ using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Options; using Newtonsoft.Json.Linq; using System.Collections.Generic; using System.IO; using System.Linq; -using System.Reflection; using System.Text; using System.Text.Json; using System.Threading.Tasks; @@ -13,11 +11,9 @@ using zero.Core.Api; using zero.Core.Entities; using zero.Core.Extensions; using zero.Core.Identity; -using zero.Core.Mapper; using zero.Core.Options; using zero.Core.Plugins; using zero.Web.Models; -using zero.Web.Sections; namespace zero.Web { @@ -31,20 +27,17 @@ namespace zero.Web protected IAuthenticationApi AuthenticationApi { get; private set; } - protected IMapper Mapper { get; private set; } - protected IEnumerable Plugins { get; private set; } protected IApplicationContext AppContext { get; private set; } - public ZeroVue(IZeroOptions options, IWebHostEnvironment env, IApplicationsApi applicationsApi, IAuthenticationApi authenticationApi, IMapper mapper, IEnumerable plugins, IApplicationContext appContext) + public ZeroVue(IZeroOptions options, IWebHostEnvironment env, IApplicationsApi applicationsApi, IAuthenticationApi authenticationApi, IEnumerable plugins, IApplicationContext appContext) { Environment = env; Options = options; ApplicationsApi = applicationsApi; AuthenticationApi = authenticationApi; - Mapper = mapper; Plugins = plugins; AppContext = appContext; //zero.path = "@Model.BackofficePath.EnsureEndsWith('/')"; @@ -71,7 +64,16 @@ namespace zero.Web config.AppId = AppContext.AppId; config.SharedAppId = Constants.Database.SharedAppId; - config.User = await Mapper.Map(await AuthenticationApi.GetUser()); + User user = await AuthenticationApi.GetUser(); + config.User = new UserEditModel() + { + Id = user.Id, + AvatarId = user.AvatarId, + Email = user.Email, + IsSuper = user.IsSuper, + Name = user.Name, + Roles = user.Roles + }; return JsonSerializer.Serialize(config, new JsonSerializerOptions() {