-
Notifications
You must be signed in to change notification settings - Fork 1
/
OurDocumentTypeUsageApiController.cs
71 lines (61 loc) · 2.29 KB
/
OurDocumentTypeUsageApiController.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
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core;
using Umbraco.Web.Editors;
using Umbraco.Web.Mvc;
using Umbraco.Web.WebApi;
namespace Our.DocumentTypeUsage
{
[PluginController("OurDocumentTypeUsage")]
public class OurDocumentTypeUsageApiController : UmbracoAuthorizedJsonController
{
private static OurDocumentTypeUsageSummaryViewModel _singleton = null;
public OurDocumentTypeUsageSummaryViewModel Get()
{
if (_singleton == null)
{
return Get(true);
}
return _singleton;
}
public OurDocumentTypeUsageSummaryViewModel Get(bool refresh)
{
var context = ApplicationContext.Current;
var serviceContext = context.Services;
var contentService = serviceContext.ContentService;
var contentTypeService = serviceContext.ContentTypeService;
var list = contentTypeService.GetAllContentTypes()
.OrderBy(o => o.Level).ThenBy(t => t.Name)
.Select(
contentType =>
new OurDocumentTypeUsageViewModel
{
Id = contentType.Id,
Name = contentType.Name,
ParentId = contentType.ParentId,
Count = contentService.GetContentOfContentType(contentType.Id).Count(),
})
.ToList();
int countDocumentTypes = list.Count();
foreach (var item in list)
{
if (item.ParentId != -1)
{
if (list.Any(d => d.Id == item.ParentId))
{
list.First(d => d.Id == item.ParentId).Children.Add(item);
}
}
}
var model = new OurDocumentTypeUsageSummaryViewModel
{
List = list.Where(d => d.ParentId == -1),
ListDocumentTypesNotInUse = list.Where(d => d.Count == 0 && !d.Children.Any()),
ContentNodeCount = list.Sum(c => c.Count),
DocumentTypeCount = countDocumentTypes
};
_singleton = model;
return model;
}
}
}