-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIncomingRelationshipViewComponent.cs
More file actions
102 lines (87 loc) · 5.82 KB
/
IncomingRelationshipViewComponent.cs
File metadata and controls
102 lines (87 loc) · 5.82 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*==============================================================================================================================
| Author Ignia, LLC
| Client Ignia, LLC
| Project Topics Library
\=============================================================================================================================*/
using OnTopic.Repositories;
namespace OnTopic.Editor.AspNetCore.Attributes.IncomingRelationshipAttribute {
/*============================================================================================================================
| CLASS: INCOMING RELATIONSHIP (VIEW COMPONENT)
\---------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Delivers a view model for an incoming relationship attribute type.
/// </summary>
public class IncomingRelationshipViewComponent : ViewComponent {
/*==========================================================================================================================
| PRIVATE VARIABLES
\-------------------------------------------------------------------------------------------------------------------------*/
private readonly ITopicRepository _topicRepository;
/*==========================================================================================================================
| CONSTRUCTOR
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Initializes a new instance of a <see cref="IncomingRelationshipViewComponent"/> with necessary dependencies.
/// </summary>
public IncomingRelationshipViewComponent(ITopicRepository topicRepository) : base() {
_topicRepository = topicRepository;
}
/*==========================================================================================================================
| METHOD: INVOKE
\-------------------------------------------------------------------------------------------------------------------------*/
/// <summary>
/// Assembles the view model for the <see cref="IncomingRelationshipViewComponent"/>.
/// </summary>
public IViewComponentResult Invoke(
EditingTopicViewModel currentTopic,
IncomingRelationshipAttributeDescriptorViewModel attribute,
string htmlFieldPrefix
) {
/*------------------------------------------------------------------------------------------------------------------------
| Validate parameters
\-----------------------------------------------------------------------------------------------------------------------*/
Contract.Requires(currentTopic, nameof(currentTopic));
Contract.Requires(attribute, nameof(attribute));
Contract.Requires(attribute.Key, nameof(attribute.Key));
/*------------------------------------------------------------------------------------------------------------------------
| Set HTML prefix
\-----------------------------------------------------------------------------------------------------------------------*/
ViewData.TemplateInfo.HtmlFieldPrefix = htmlFieldPrefix;
/*------------------------------------------------------------------------------------------------------------------------
| Establish view model
\-----------------------------------------------------------------------------------------------------------------------*/
var model = new IncomingRelationshipAttributeViewModel(currentTopic, attribute);
/*------------------------------------------------------------------------------------------------------------------------
| Set incoming relationships
>-------------------------------------------------------------------------------------------------------------------------
| ### NOTE JJC20200929: This would be a lot cleaner using the ITopicMappingService. But that would introduce an additional
| dependency on the StandardEditorComposer, which would be a breaking change. We can reevaluate this in the future if
| other view components would benefit from this.
\-----------------------------------------------------------------------------------------------------------------------*/
var topic = _topicRepository.Load(currentTopic.UniqueKey);
Contract.Assume(topic, $"The target topic with the unique key '{currentTopic.UniqueKey}' could not be found.");
foreach(var relatedTopic in topic.IncomingRelationships.GetValues(attribute.RelationshipKey?? attribute.Key)) {
if (!String.IsNullOrWhiteSpace(attribute.AttributeKey)) {
var attributeValue = relatedTopic.Attributes.GetValue(attribute.AttributeKey, "");
if (attribute.AttributeKey.Equals("ContentType", StringComparison.OrdinalIgnoreCase)) {
attributeValue = relatedTopic.ContentType;
}
if (!attributeValue.Equals(attribute.AttributeValue, StringComparison.OrdinalIgnoreCase)) {
continue;
}
}
var relatedViewModel = new CoreTopicViewModel {
Key = relatedTopic.Key,
ContentType = relatedTopic.ContentType,
UniqueKey = relatedTopic.GetUniqueKey(),
WebPath = relatedTopic.GetWebPath(),
Title = relatedTopic.Title
};
model.RelatedTopics.Add(relatedViewModel);
}
/*------------------------------------------------------------------------------------------------------------------------
| Return view with view model
\-----------------------------------------------------------------------------------------------------------------------*/
return View(model);
}
} // Class
} // Namespace