Skip to content

Commit

Permalink
Fixes #315
Browse files Browse the repository at this point in the history
(cherry picked from commit 90c66ef)
  • Loading branch information
niklasstich committed Aug 22, 2023
1 parent c15e5f5 commit 7f75892
Showing 1 changed file with 41 additions and 37 deletions.
78 changes: 41 additions & 37 deletions AuthoringTool/Mapping/ViewModelEntityMappingProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,14 @@
using Presentation.PresentationLogic.LearningSpace.SpaceLayout;
using Presentation.PresentationLogic.LearningWorld;
using Presentation.PresentationLogic.Topic;

namespace AuthoringTool.Mapping;

/// <summary>
/// Configures mappings between ViewModels and Entity classes.
/// </summary>
public class ViewModelEntityMappingProfile : Profile
{
public static Action<IMapperConfigurationExpression> Configure => cfg =>
{
cfg.AddProfile(new ViewModelEntityMappingProfile());
cfg.AddCollectionMappersOnce();
};

private ViewModelEntityMappingProfile()
{
DisableConstructorMapping();
Expand All @@ -38,6 +33,12 @@ private ViewModelEntityMappingProfile()
CreateTopicMap();
}

public static Action<IMapperConfigurationExpression> Configure => cfg =>
{
cfg.AddProfile(new ViewModelEntityMappingProfile());
cfg.AddCollectionMappersOnce();
};

private void CreateTopicMap()
{
CreateMap<Topic, TopicViewModel>()
Expand All @@ -60,25 +61,25 @@ private void CreateLearningSpaceLayoutMap()
.IncludeBase<ILearningSpaceLayout, LearningSpaceLayoutViewModel>()
.ReverseMap()
.IncludeBase<ILearningSpaceLayoutViewModel, LearningSpaceLayout>();

}

private static void MapSpaceLayoutElements(ILearningSpaceLayout source, LearningSpaceLayoutViewModel destination, ResolutionContext ctx)
private static void MapSpaceLayoutElements(ILearningSpaceLayout source, LearningSpaceLayoutViewModel destination,
ResolutionContext ctx)
{
//gather view models for all elements that are in source but not in destination
var sourceNewElementsViewModels = source.LearningElements
.Where(x => !SameIdAtSameIndex(destination, x))
.Select(tup =>
new KeyValuePair<int, ILearningElementViewModel>(tup.Key,
ctx.Mapper.Map<LearningElementViewModel>(tup.Value)));

//remove all elements from destination that are not in source
foreach (var (key, _) in destination.LearningElements.Where(x =>
!source.LearningElements.Any(y => y.Key == x.Key && y.Value.Id == x.Value.Id)))
{
destination.LearningElements.Remove(key);
}

//map all elements that are in source and destination already into the respective destination element
foreach (var (key, value) in destination.LearningElements)
{
Expand Down Expand Up @@ -125,7 +126,7 @@ private void CreateInterfaceMaps()
.As<LearningSpaceViewModel>();
CreateMap<LearningPathway, ILearningPathWayViewModel>()
.As<LearningPathwayViewModel>();

CreateMap<LearningSpaceViewModel, ILearningSpace>()
.EqualityComparison((vm, intf) => vm.Id.Equals(intf.Id))
.As<LearningSpace>();
Expand All @@ -137,7 +138,7 @@ private void CreateInterfaceMaps()
.As<LearningSpace>();
CreateMap<ILearningSpace, ILearningSpaceViewModel>()
.As<LearningSpaceViewModel>();

CreateMap<LearningWorld, ILearningWorldViewModel>()
.EqualityComparison((e, intf) => e.Id.Equals(intf.Id))
.As<LearningWorldViewModel>();
Expand All @@ -148,7 +149,7 @@ private void CreateInterfaceMaps()
.As<LearningWorldViewModel>();
CreateMap<ILearningWorldViewModel, ILearningWorld>()
.As<LearningWorld>();

CreateMap<LearningElementViewModel, ILearningElement>().As<LearningElement>();
CreateMap<LearningElementViewModel, LearningElement>()
.ForMember(x => x.Parent, opt => opt.Ignore())
Expand All @@ -169,13 +170,17 @@ private void CreateLearningElementMap()
{
CreateMap<LearningElement, LearningElementViewModel>()
.ForMember(x => x.Parent, opt => opt.Ignore())
.ForMember(x => x.LearningContent, opt => opt.Ignore())
.AfterMap((entity, vm, context) =>
vm.LearningContent = context.Mapper.Map<ILearningContentViewModel>(entity.LearningContent))
.EqualityComparison((x, y) => x.Id == y.Id)
.ReverseMap()
.EqualityComparison((x, y) => x.Id == y.Id)
.ForMember(x => x.Parent, opt => opt.Ignore());
CreateMap<ILearningElementViewModel, LearningElement>()
.EqualityComparison((x, y) => x.Id == y.Id)
.ForMember(x => x.Parent, opt => opt.Ignore())
.ForMember(x => x.LearningContent, opt => opt.DoNotUseDestinationValue())
.ReverseMap()
.EqualityComparison((x, y) => x.Id == y.Id)
.ForMember(x => x.Parent, opt => opt.Ignore());
Expand Down Expand Up @@ -209,9 +214,8 @@ private void CreateLearningSpaceMap()
{
element.Parent = d;
}

});
CreateMap<ILearningSpaceViewModel, LearningSpace>()
CreateMap<ILearningSpaceViewModel, LearningSpace>()
.IncludeBase<IObjectInPathWayViewModel, IObjectInPathWay>()
.IncludeBase<ILearningSpaceViewModel, ILearningSpace>()
.EqualityComparison((x, y) => x.Id == y.Id)
Expand All @@ -224,23 +228,22 @@ private void CreateLearningSpaceMap()
{
element.Parent = d;
}

});
CreateMap<ILearningSpace, LearningSpaceViewModel>()
.ForMember(x => x.InBoundObjects, opt => opt.Ignore())
.ForMember(x => x.OutBoundObjects, opt => opt.Ignore())
.ForMember(x => x.ContainedLearningElements, opt => opt.Ignore())
.ForMember(x => x.UnsavedChanges, opt => opt.Ignore())
.IncludeBase<IObjectInPathWay, IObjectInPathWayViewModel>()
.IncludeBase<ILearningSpace, ILearningSpaceViewModel>()
.EqualityComparison((x, y) => x.Id == y.Id)
.AfterMap((s, d) =>
{
foreach (var element in d.ContainedLearningElements)
{
element.Parent = d;
}
});
CreateMap<ILearningSpace, LearningSpaceViewModel>()
.ForMember(x => x.InBoundObjects, opt => opt.Ignore())
.ForMember(x => x.OutBoundObjects, opt => opt.Ignore())
.ForMember(x => x.ContainedLearningElements, opt => opt.Ignore())
.ForMember(x => x.UnsavedChanges, opt => opt.Ignore())
.IncludeBase<IObjectInPathWay, IObjectInPathWayViewModel>()
.IncludeBase<ILearningSpace, ILearningSpaceViewModel>()
.EqualityComparison((x, y) => x.Id == y.Id)
.AfterMap((s, d) =>
{
foreach (var element in d.ContainedLearningElements)
{
element.Parent = d;
}
});
}

private void CreatePathwayMaps()
Expand Down Expand Up @@ -320,7 +323,7 @@ private void CreateLearningWorldMap()
.Select(x => x.TargetObject).ToList();
}
});

CreateMap<ILearningWorld, LearningWorldViewModel>()
.EqualityComparison((x, y) => x.Id == y.Id)
.ForMember(x => x.AllLearningElements, opt => opt.Ignore())
Expand Down Expand Up @@ -373,21 +376,22 @@ private void CreateLearningWorldMap()
}
});
}

private static void MapUnplacedElements(ILearningWorld source, LearningWorldViewModel destination, ResolutionContext ctx)

private static void MapUnplacedElements(ILearningWorld source, LearningWorldViewModel destination,
ResolutionContext ctx)
{
//gather view models for all elements that are in source but not in destination
var sourceNewElementsViewModels = source.UnplacedLearningElements.ToList()
.FindAll(x => destination.UnplacedLearningElements.All(v => v.Id != x.Id))
.Select(ele => ctx.Mapper.Map<LearningElementViewModel>(ele));

//remove all elements from destination that are not in source
foreach (var ele in destination.UnplacedLearningElements.ToList().Where(x =>
foreach (var ele in destination.UnplacedLearningElements.ToList().Where(x =>
source.UnplacedLearningElements.All(y => y.Id != x.Id)))
{
destination.UnplacedLearningElements.Remove(ele);
}

//map all elements that are in source and destination already into the respective destination element
foreach (var ele in destination.UnplacedLearningElements)
{
Expand Down

0 comments on commit 7f75892

Please sign in to comment.