-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathMediaMapDefinition.cs
59 lines (50 loc) · 2.53 KB
/
MediaMapDefinition.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using Umbraco.Cms.Api.Management.Mapping.Content;
using Umbraco.Cms.Api.Management.ViewModels.Media;
using Umbraco.Cms.Api.Management.ViewModels.Media.Collection;
using Umbraco.Cms.Api.Management.ViewModels.MediaType;
using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Mapping;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Extensions;
namespace Umbraco.Cms.Api.Management.Mapping.Media;
public class MediaMapDefinition : ContentMapDefinition<IMedia, MediaValueModel, MediaVariantResponseModel>, IMapDefinition
{
private readonly CommonMapper _commonMapper;
public MediaMapDefinition(PropertyEditorCollection propertyEditorCollection, CommonMapper commonMapper)
: base(propertyEditorCollection)
=> _commonMapper = commonMapper;
public void DefineMaps(IUmbracoMapper mapper)
{
mapper.Define<IMedia, MediaResponseModel>((_, _) => new MediaResponseModel(), Map);
mapper.Define<IMedia, MediaCollectionResponseModel>((_, _) => new MediaCollectionResponseModel(), Map);
}
// Umbraco.Code.MapAll -Urls
private void Map(IMedia source, MediaResponseModel target, MapperContext context)
{
target.Id = source.Key;
target.MediaType = context.Map<MediaTypeReferenceResponseModel>(source.ContentType)!;
target.Values = MapValueViewModels(source.Properties);
target.Variants = MapVariantViewModels(source);
target.IsTrashed = source.Trashed;
}
// Umbraco.Code.MapAll
private void Map(IMedia source, MediaCollectionResponseModel target, MapperContext context)
{
target.Id = source.Key;
target.MediaType = context.Map<MediaTypeCollectionReferenceResponseModel>(source.ContentType)!;
target.SortOrder = source.SortOrder;
target.Creator = _commonMapper.GetOwnerName(source, context);
// If there's a set of property aliases specified in the collection configuration, we will check if the current property's
// value should be mapped. If it isn't one of the ones specified in 'includeProperties', we will just return the result
// without mapping the value.
var includedProperties = context.GetIncludedProperties();
IEnumerable<IProperty> properties = source.Properties;
if (includedProperties is not null)
{
properties = properties.Where(property => includedProperties.Contains(property.Alias));
}
target.Values = MapValueViewModels(properties);
target.Variants = MapVariantViewModels(source);
}
}