-
Notifications
You must be signed in to change notification settings - Fork 19
/
SelectorDemoUsageViewComponent.cs
180 lines (163 loc) · 6.67 KB
/
SelectorDemoUsageViewComponent.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using content_selectors.Entities.SelectorDemoUsage;
using content_selectors.ViewModels.SelectorDemoUsage;
using Microsoft.AspNetCore.Mvc;
using Progress.Sitefinity.AspNetCore.RestSdk;
using Progress.Sitefinity.AspNetCore.ViewComponents;
using Progress.Sitefinity.AspNetCore.Web;
using Progress.Sitefinity.Renderer.Designers.Attributes;
using Progress.Sitefinity.Renderer.Entities.Content;
using Progress.Sitefinity.RestSdk;
using Progress.Sitefinity.RestSdk.Dto;
using Progress.Sitefinity.RestSdk.Exceptions;
namespace content_selectors.ViewComponents
{
/// <summary>
/// The view component for working with content selectors.
/// </summary>
[SitefinityWidget]
public class SelectorDemoUsageViewComponent : ViewComponent
{
private IRestClient restClient;
private IRenderContext renderContext;
/// <summary>
/// Initializes a new instance of the <see cref="SelectorDemoUsageViewComponent"/> class.
/// </summary>
public SelectorDemoUsageViewComponent(IRestClient restClient, IRenderContext renderContext)
{
this.restClient = restClient;
this.renderContext = renderContext;
}
/// <summary>
/// Invokes the view component.
/// </summary>
/// <param name="context">The view component context.</param>
/// <returns>The view component result.</returns>
public async Task<IViewComponentResult> InvokeAsync(IViewComponentContext<SelectorDemoEntity> context)
{
var viewModels = new List<ItemCollection>();
if (context.Entity.Page.HasSelectedItems())
{
var page = await this.restClient.GetItems<SdkItem>(context.Entity.Page);
viewModels.Add(new ItemCollection()
{
GroupTitle = "Page",
Items = page.Items.ToArray(),
});
}
if (context.Entity.Images.HasSelectedItems())
{
var images = await this.restClient.GetItems<SdkItem>(context.Entity.Images);
viewModels.Add(new ItemCollection()
{
GroupTitle = "Image",
Items = images.Items.ToArray()
});
}
if (context.Entity.Albums.HasSelectedItems())
{
var albums = await this.restClient.GetItems<SdkItem>(context.Entity.Albums);
// get folders
context.Entity.Albums.Content[0].Type = KnownContentTypes.Folders;
var folders = await this.restClient.GetItems<SdkItem>(context.Entity.Albums);
viewModels.Add(new ItemCollection()
{
GroupTitle = "Albums",
Items = albums.Items.Concat(folders.Items).ToArray()
});
}
if (context.Entity.DocumentLibrary.HasSelectedItems())
{
var documentLibrary = await this.restClient.GetItems<SdkItem>(context.Entity.DocumentLibrary);
// get folders
context.Entity.DocumentLibrary.Content[0].Type = KnownContentTypes.Folders;
var folders = await this.restClient.GetItems<SdkItem>(context.Entity.DocumentLibrary);
viewModels.Add(new ItemCollection()
{
GroupTitle = "Document library",
Items = documentLibrary.Items.Concat(folders.Items).ToArray()
});
}
if (context.Entity.Tags.HasSelectedItems())
{
var tags = await this.restClient.GetItems<SdkItem>(context.Entity.Tags);
viewModels.Add(new ItemCollection()
{
GroupTitle = "Tags",
Items = tags.Items.ToArray()
});
}
// combine same types of items into a single request - e.g. news + news
// do not use with different types of items - e.g. news + images
var bulkResponse = await this.restClient.GetItemsBulk<SdkItem>(new[] { context.Entity.News, context.Entity.NewsLive });
if (context.Entity.News.HasSelectedItems())
{
var newsItems = bulkResponse[context.Entity.News];
viewModels.Add(new ItemCollection()
{
GroupTitle = "News items draft",
Items = newsItems.ToArray(),
});
}
if (context.Entity.NewsLive.HasSelectedItems())
{
var newsItemsLive = bulkResponse[context.Entity.NewsLive];
viewModels.Add(new ItemCollection()
{
GroupTitle = "News items live",
Items = newsItemsLive.ToArray(),
});
}
try
{
if (context.Entity.GeographicalRegions.HasSelectedItems())
{
var geoRegions = await this.restClient.GetItems<SdkItem>(context.Entity.GeographicalRegions).ConfigureAwait(true);
viewModels.Add(new ItemCollection()
{
GroupTitle = "Geographical Regions",
Items = geoRegions.Items.ToArray(),
});
}
}
catch (ErrorCodeException err)
{
if (this.renderContext.IsEdit)
{
viewModels.Add(new ItemCollection()
{
GroupTitle = $"Geographical Regions - {err.Message}",
Items = Array.Empty<SdkItem>(),
});
}
}
try
{
if (context.Entity.PressReleases.HasSelectedItems())
{
var pressReleases = await this.restClient.GetItems<SdkItem>(context.Entity.PressReleases);
viewModels.Add(new ItemCollection()
{
GroupTitle = "Press Releases",
Items = pressReleases.Items.ToArray(),
});
}
}
catch (Exception err)
{
if (this.renderContext.IsEdit)
{
viewModels.Add(new ItemCollection()
{
GroupTitle = $"Press Releases - {err.Message}",
Items = Array.Empty<SdkItem>(),
});
}
}
return this.View(viewModels);
}
}
}