Skip to content
This repository was archived by the owner on Feb 10, 2024. It is now read-only.

Commit 760897e

Browse files
committed
fix api controller and UnpublishedContent
1 parent ac73f60 commit 760897e

File tree

4 files changed

+39
-51
lines changed

4 files changed

+39
-51
lines changed

src/Our.Umbraco.DocTypeGridEditor9/Models/UnpublishedContent.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,21 @@ internal class UnpublishedContent : IPublishedContent
2020
private readonly Lazy<IPublishedContent> parent;
2121
private readonly Lazy<Dictionary<string, IPublishedProperty>> properties;
2222

23-
public UnpublishedContent(int id, ServiceContext serviceContext, PropertyEditorCollection propertyEditorCollection, IPublishedContentTypeFactory publishedContentTypeFactory)
24-
: this(serviceContext.ContentService.GetById(id), serviceContext, propertyEditorCollection, publishedContentTypeFactory)
23+
public UnpublishedContent(int id, IContentService contentService, IContentTypeService contentTypeService, IDataTypeService dataTypeService, PropertyEditorCollection propertyEditorCollection, IPublishedContentTypeFactory publishedContentTypeFactory)
24+
: this(contentService.GetById(id), contentService, contentTypeService, dataTypeService, propertyEditorCollection, publishedContentTypeFactory)
2525
{ }
2626

27-
public UnpublishedContent(IContent content, ServiceContext serviceContext, PropertyEditorCollection propertyEditorCollection, IPublishedContentTypeFactory publishedContentTypeFactory)
27+
public UnpublishedContent(IContent content, IContentService contentService, IContentTypeService contentTypeService, IDataTypeService dataTypeService, PropertyEditorCollection propertyEditorCollection, IPublishedContentTypeFactory publishedContentTypeFactory)
2828
: base()
2929
{
3030

3131
this.content = content;
32-
var contentType = serviceContext.ContentTypeService.Get(this.content.ContentType.Id);
32+
var contentType = contentTypeService.Get(this.content.ContentType.Id);
3333

3434
//this.children = new Lazy<IEnumerable<IPublishedContent>>(() => this.content.Children().Select(x => new UnpublishedContent(x, serviceContext)).ToList());
3535
this.contentType = new Lazy<IPublishedContentType>(() => publishedContentTypeFactory.CreateContentType(contentType));
36-
this.parent = new Lazy<IPublishedContent>(() => new UnpublishedContent(serviceContext.ContentService.GetById(this.content.ParentId), serviceContext, propertyEditorCollection, publishedContentTypeFactory));
37-
this.properties = new Lazy<Dictionary<string, IPublishedProperty>>(() => MapProperties(serviceContext, propertyEditorCollection));
36+
this.parent = new Lazy<IPublishedContent>(() => new UnpublishedContent(contentService.GetById(this.content.ParentId), contentService, contentTypeService, dataTypeService, propertyEditorCollection, publishedContentTypeFactory));
37+
this.properties = new Lazy<Dictionary<string, IPublishedProperty>>(() => MapProperties(dataTypeService, propertyEditorCollection));
3838
//this.urlName = new Lazy<string>(() => this.content.Name.ToUrlSegment());
3939
//this.writerName = new Lazy<string>(() => this.content.GetWriterProfile(userService.Value).Name);
4040
}
@@ -108,7 +108,7 @@ public IPublishedProperty GetProperty(string alias)
108108
return this.properties.Value.TryGetValue(alias, out IPublishedProperty property) ? property : null;
109109
}
110110

111-
private Dictionary<string, IPublishedProperty> MapProperties(ServiceContext services, PropertyEditorCollection dataEditors)
111+
private Dictionary<string, IPublishedProperty> MapProperties(IDataTypeService dataTypeService, PropertyEditorCollection dataEditors)
112112
{
113113
var contentType = this.contentType.Value;
114114
var properties = this.content.Properties;
@@ -124,7 +124,7 @@ private Dictionary<string, IPublishedProperty> MapProperties(ServiceContext serv
124124
dataEditors.TryGet(propertyType.DataType.EditorAlias, out var editor);
125125
if (editor != null)
126126
{
127-
value = editor.GetValueEditor().ConvertDbToString(property.PropertyType, value, services.DataTypeService);
127+
value = editor.GetValueEditor().ConvertDbToString(property.PropertyType, value, dataTypeService);
128128
}
129129
}
130130

src/Our.Umbraco.DocTypeGridEditor9/Web/Controllers/DocTypeGridEditorApiController.cs

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
using Microsoft.AspNetCore.Http.Extensions;
22
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.AspNetCore.Mvc.ModelBinding;
4+
using Microsoft.AspNetCore.Mvc.Rendering;
5+
using Microsoft.AspNetCore.Mvc.ViewFeatures;
36
using Our.Umbraco.DocTypeGridEditor9.Helpers;
7+
using Our.Umbraco.DocTypeGridEditor9.Models;
48
using Our.Umbraco.DocTypeGridEditor9.Web.Models;
59
using System;
610
using System.Collections.Generic;
@@ -15,6 +19,7 @@
1519
using Umbraco.Cms.Core;
1620
using Umbraco.Cms.Core.Models;
1721
using Umbraco.Cms.Core.Models.PublishedContent;
22+
using Umbraco.Cms.Core.PropertyEditors;
1823
using Umbraco.Cms.Core.PublishedCache;
1924
using Umbraco.Cms.Core.Routing;
2025
using Umbraco.Cms.Core.Services;
@@ -37,6 +42,10 @@ public class DocTypeGridEditorApiController : UmbracoAuthorizedJsonController
3742
private readonly IPublishedContentQuery _contentQuery;
3843
private readonly IPublishedRouter _router;
3944
private readonly DocTypeGridEditorHelper _dtgeHelper;
45+
private readonly ServiceContext _serviceContext;
46+
private readonly IPublishedContentTypeFactory _publishedContentTypeFactory;
47+
private readonly IPublishedModelFactory _publishedModelFactory;
48+
private readonly PropertyEditorCollection _propertyEditorCollection;
4049

4150
public DocTypeGridEditorApiController(IUmbracoContextAccessor umbracoContext,
4251
IContentTypeService contentTypeService,
@@ -45,6 +54,9 @@ public DocTypeGridEditorApiController(IUmbracoContextAccessor umbracoContext,
4554
IShortStringHelper shortStringHelper,
4655
IPublishedContentQuery contentQuery,
4756
IPublishedRouter router,
57+
ServiceContext serviceContext,
58+
IPublishedContentTypeFactory publishedContentTypeFactory,
59+
PropertyEditorCollection propertyEditorCollection,
4860
DocTypeGridEditorHelper dtgeHelper)
4961
{
5062
_umbracoContext = umbracoContext;
@@ -55,10 +67,13 @@ public DocTypeGridEditorApiController(IUmbracoContextAccessor umbracoContext,
5567
_contentQuery = contentQuery;
5668
_router = router;
5769
_dtgeHelper = dtgeHelper;
70+
_serviceContext = serviceContext;
71+
_publishedContentTypeFactory = publishedContentTypeFactory;
72+
_propertyEditorCollection = propertyEditorCollection;
5873
}
5974

6075
[HttpGet]
61-
public object GetContentTypeAliasByGuid([ModelBinder] Guid guid)
76+
public object GetContentTypeAliasByGuid([FromQuery] Guid guid)
6277
{
6378
return new
6479
{
@@ -67,7 +82,7 @@ public object GetContentTypeAliasByGuid([ModelBinder] Guid guid)
6782
}
6883

6984
[HttpGet]
70-
public IEnumerable<object> GetContentTypesOld(string[] allowedContentTypes)
85+
public IEnumerable<object> GetContentTypes([FromQuery]string[] allowedContentTypes)
7186
{
7287
var allContentTypes = _contentTypeService.GetAll().ToList();
7388
var contentTypes = allContentTypes
@@ -96,35 +111,7 @@ public IEnumerable<object> GetContentTypesOld(string[] allowedContentTypes)
96111
}
97112

98113
[HttpGet]
99-
public IEnumerable<object> GetContentTypes()
100-
{
101-
var allContentTypes = _contentTypeService.GetAll().ToList();
102-
var contentTypes = allContentTypes
103-
.Where(x => x.IsElement && x.VariesByCulture() == false)
104-
.OrderBy(x => x.Name)
105-
.ToList();
106-
107-
var blueprints = _contentService.GetBlueprintsForContentTypes(contentTypes.Select(x => x.Id).ToArray()).ToArray();
108-
109-
return contentTypes
110-
.Select(x => new
111-
{
112-
id = x.Id,
113-
guid = x.Key,
114-
name = x.Name,
115-
alias = x.Alias,
116-
description = x.Description,
117-
icon = x.Icon,
118-
blueprints = blueprints.Where(bp => bp.ContentTypeId == x.Id).Select(bp => new
119-
{
120-
id = bp.Id,
121-
name = bp.Name
122-
})
123-
});
124-
}
125-
126-
[HttpGet]
127-
public object GetContentType(string contentTypeAlias)
114+
public object GetContentType([FromQuery]string contentTypeAlias)
128115
{
129116
Guid docTypeGuid;
130117
if (Guid.TryParse(contentTypeAlias, out docTypeGuid))
@@ -140,7 +127,7 @@ public object GetContentType(string contentTypeAlias)
140127
}
141128

142129
[HttpGet]
143-
public object GetDataTypePreValues(string dtdId)
130+
public object GetDataTypePreValues([FromQuery]string dtdId)
144131
{
145132
Guid guidDtdId;
146133
int intDtdId;
@@ -173,7 +160,7 @@ public object GetDataTypePreValues(string dtdId)
173160
}
174161

175162
[HttpPost]
176-
public HttpResponseMessage GetPreviewMarkup([FromBody] PreviewData data, int pageId)
163+
public PartialViewResult GetPreviewMarkup([FromForm] PreviewData data, [FromQuery]int pageId)
177164
{
178165
var page = default(IPublishedContent);
179166

@@ -185,7 +172,7 @@ public HttpResponseMessage GetPreviewMarkup([FromBody] PreviewData data, int pag
185172
if (page == null)
186173
{
187174
// If unpublished, then fake PublishedContent
188-
// page = new UnpublishedContent(pageId, Services); TODO: UnpublishedContent
175+
page = new UnpublishedContent(pageId, _contentService, _contentTypeService, _dataTypeService, _propertyEditorCollection, _publishedContentTypeFactory);
189176
}
190177
}
191178

@@ -224,19 +211,18 @@ public HttpResponseMessage GetPreviewMarkup([FromBody] PreviewData data, int pag
224211
ViewPath = data.ViewPath
225212
};
226213

214+
227215
// Render view
216+
228217
var partialName = "~/App_Plugins/DocTypeGridEditor/Render/DocTypeGridEditorPreviewer.cshtml";
229-
var markup = Helpers.ViewHelper.RenderPartial(partialName, model, HttpContext, _umbracoContext.UmbracoContext);
230218

231-
// Return response
232-
var response = new HttpResponseMessage
219+
var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary());
220+
viewData.Model = model;
221+
return new PartialViewResult()
233222
{
234-
Content = new StringContent(markup ?? string.Empty)
223+
ViewName = partialName,
224+
ViewData = viewData
235225
};
236-
237-
response.Content.Headers.ContentType = new MediaTypeHeaderValue(MediaTypeNames.Text.Html);
238-
239-
return response;
240226
}
241227
}
242228
}

src/Our.Umbraco.DocTypeGridEditor9/Web/Extensions/HtmlHelperExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Microsoft.AspNetCore.Html;
22
using Microsoft.AspNetCore.Mvc.Rendering;
3-
using Microsoft.AspNetCore.Mvc.ViewFeatures;
43
using Our.Umbraco.DocTypeGridEditor9.Web.Helpers;
54
using System;
65
using System.Collections.Generic;
@@ -132,5 +131,6 @@ public static IHtmlContent RenderDocTypeGridEditorItem(
132131

133132
return helper.PartialAsync(content.ContentType.Alias, content).Result;
134133
}
134+
135135
}
136136
}

src/Our.Umbraco.DocTypeGridEditor9/Web/Helpers/ViewHelper.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using Microsoft.AspNetCore.Mvc.Rendering;
1212
using Microsoft.AspNetCore.Mvc;
1313
using Umbraco.Cms.Core.Web;
14+
using Microsoft.AspNetCore.Mvc.ViewEngines;
15+
using Microsoft.Extensions.DependencyInjection;
1416

1517
namespace Our.Umbraco.DocTypeGridEditor9.Web.Helpers
1618
{

0 commit comments

Comments
 (0)