147 lines
5.5 KiB
C#
147 lines
5.5 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Web.Http;
|
|
using System.Web.Http.ModelBinding;
|
|
using Umbraco.Core.Logging;
|
|
using Umbraco.Core.Models;
|
|
using Umbraco.Core.Models.Editors;
|
|
using Umbraco.Web.Models.ContentEditing;
|
|
using Umbraco.Web.Models.Mapping;
|
|
using Umbraco.Web.Mvc;
|
|
using Umbraco.Web.WebApi;
|
|
using Umbraco.Web.WebApi.Binders;
|
|
using Umbraco.Web.WebApi.Filters;
|
|
|
|
namespace Umbraco.Web.Editors
|
|
{
|
|
/// <summary>
|
|
/// The API controller used for editing content
|
|
/// </summary>
|
|
[PluginController("UmbracoApi")]
|
|
[ValidationFilter]
|
|
public class ContentController : UmbracoAuthorizedApiController
|
|
{
|
|
private readonly ContentModelMapper _contentModelMapper;
|
|
|
|
/// <summary>
|
|
/// Constructor
|
|
/// </summary>
|
|
public ContentController()
|
|
: this(UmbracoContext.Current, new ContentModelMapper(UmbracoContext.Current.Application, new ProfileModelMapper()))
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Constructor
|
|
/// </summary>
|
|
/// <param name="umbracoContext"></param>
|
|
/// <param name="contentModelMapper"></param>
|
|
internal ContentController(UmbracoContext umbracoContext, ContentModelMapper contentModelMapper)
|
|
: base(umbracoContext)
|
|
{
|
|
_contentModelMapper = contentModelMapper;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove the xml formatter... only support JSON!
|
|
/// </summary>
|
|
/// <param name="controllerContext"></param>
|
|
protected override void Initialize(global::System.Web.Http.Controllers.HttpControllerContext controllerContext)
|
|
{
|
|
base.Initialize(controllerContext);
|
|
controllerContext.Configuration.Formatters.Remove(controllerContext.Configuration.Formatters.XmlFormatter);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the content json for the content id
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public ContentItemDisplay GetById(int id)
|
|
{
|
|
var foundContent = Services.ContentService.GetById(id);
|
|
if (foundContent == null)
|
|
{
|
|
ModelState.AddModelError("id", string.Format("content with id: {0} was not found", id));
|
|
var errorResponse = Request.CreateErrorResponse(
|
|
HttpStatusCode.NotFound,
|
|
ModelState);
|
|
throw new HttpResponseException(errorResponse);
|
|
}
|
|
return _contentModelMapper.ToContentItemDisplay(foundContent);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets an empty content item for the
|
|
/// </summary>
|
|
/// <param name="contentTypeAlias"></param>
|
|
/// <param name="parentId"></param>
|
|
/// <returns></returns>
|
|
public ContentItemDisplay GetEmpty(string contentTypeAlias, int parentId)
|
|
{
|
|
var contentType = Services.ContentTypeService.GetContentType(contentTypeAlias);
|
|
if (contentType == null)
|
|
{
|
|
throw new HttpResponseException(HttpStatusCode.NotFound);
|
|
}
|
|
|
|
var emptyContent = new Content("Empty", parentId, contentType);
|
|
return _contentModelMapper.ToContentItemDisplay(emptyContent);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Saves content
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[ContentItemValidationFilter(typeof(ContentItemValidationHelper<IContent>))]
|
|
[FileUploadCleanupFilter]
|
|
public ContentItemDisplay PostSave(
|
|
[ModelBinder(typeof(ContentItemBinder))]
|
|
ContentItemSave<IContent> contentItem)
|
|
{
|
|
//If we've reached here it means:
|
|
// * Our model has been bound
|
|
// * and validated
|
|
// * any file attachments have been saved to their temporary location for us to use
|
|
// * we have a reference to the DTO object and the persisted object
|
|
|
|
//Now, we just need to save the data
|
|
|
|
//Save the property values
|
|
foreach (var p in contentItem.ContentDto.Properties)
|
|
{
|
|
//get the dbo property
|
|
var dboProperty = contentItem.PersistedContent.Properties[p.Alias];
|
|
|
|
//create the property data to send to the property editor
|
|
var d = new Dictionary<string, object>();
|
|
//add the files if any
|
|
var files = contentItem.UploadedFiles.Where(x => x.PropertyId == p.Id).ToArray();
|
|
if (files.Any())
|
|
{
|
|
d.Add("files", files);
|
|
}
|
|
var data = new ContentPropertyData(p.Value, d);
|
|
|
|
//get the deserialized value from the property editor
|
|
if (p.PropertyEditor == null)
|
|
{
|
|
LogHelper.Warn<ContentController>("No property editor found for property " + p.Alias);
|
|
}
|
|
else
|
|
{
|
|
dboProperty.Value = p.PropertyEditor.ValueEditor.DeserializeValue(data, dboProperty.Value);
|
|
}
|
|
}
|
|
|
|
//save the item
|
|
Services.ContentService.Save(contentItem.PersistedContent);
|
|
|
|
//return the updated model
|
|
return _contentModelMapper.ToContentItemDisplay(contentItem.PersistedContent);
|
|
}
|
|
|
|
}
|
|
} |