diff --git a/src/Umbraco.Web.UI.Client/src/common/resources/contenttype.resource.js b/src/Umbraco.Web.UI.Client/src/common/resources/contenttype.resource.js index 8408705dd7..60e13644d8 100644 --- a/src/Umbraco.Web.UI.Client/src/common/resources/contenttype.resource.js +++ b/src/Umbraco.Web.UI.Client/src/common/resources/contenttype.resource.js @@ -89,6 +89,35 @@ function contentTypeResource($q, $http, umbRequestHelper) { "GetById", [{ id: id }])), 'Failed to retrieve content type'); + }, + + getScaffold: function () { + + return umbRequestHelper.resourcePromise( + $http.get( + umbRequestHelper.getApiUrl( + "contentTypeApiBaseUrl", + "GetScaffold")), + 'Failed to retrieve content type scaffold'); + }, + + /** + * @ngdoc method + * @name umbraco.resources.contentTypeResource#save + * @methodOf umbraco.resources.contentTypeResource + * + * @description + * Saves or update a content type + * + * @param {Object} content data type object to create/update + * @returns {Promise} resourcePromise object. + * + */ + save: function (contentType) { + + return umbRequestHelper.resourcePromise( + $http.post(umbRequestHelper.getApiUrl("contentTypeApiBaseUrl", "PostSave"), contentType), + 'Failed to save data for content type id ' + contentType.id); } }; diff --git a/src/Umbraco.Web/Editors/ContentTypeController.cs b/src/Umbraco.Web/Editors/ContentTypeController.cs index 6bbab0a6d3..8aa5a7fc73 100644 --- a/src/Umbraco.Web/Editors/ContentTypeController.cs +++ b/src/Umbraco.Web/Editors/ContentTypeController.cs @@ -14,6 +14,7 @@ using Umbraco.Web.WebApi.Filters; using Constants = Umbraco.Core.Constants; using Newtonsoft.Json; using Umbraco.Core.PropertyEditors; +using System; namespace Umbraco.Web.Editors { @@ -70,6 +71,12 @@ namespace Umbraco.Web.Editors return dto; } + public Umbraco.Web.Models.ContentEditing.ContentTypeDisplay GetScaffold() + { + var ct = new ContentType(-1); + var dto = Mapper.Map(ct); + return dto; + } public ContentPropertyDisplay GetPropertyTypeScaffold(int id) { @@ -136,6 +143,54 @@ namespace Umbraco.Web.Editors return basics; } + + public ContentTypeDisplay PostSave(ContentTypeDisplay contentType) + { + + var ctService = ApplicationContext.Services.ContentTypeService; + + ///TODO: warn on content type alias conflicts + ///TODO: warn on property alias conflicts + + ///TODO: Validate the submitted model + + var ctId = Convert.ToInt32(contentType.Id); + + if (ctId > 0) + { + //its an update to an existing + IContentType found = ctService.GetContentType(ctId); + if(found == null) + throw new HttpResponseException( HttpStatusCode.NotFound ); + + Mapper.Map(contentType, found); + ctService.Save(found); + + //map the saved item back to the content type (it should now get id etc set) + Mapper.Map(found, contentType); + return contentType; + } + else + { + //ensure alias is set + if (string.IsNullOrEmpty(contentType.Alias)) + contentType.Alias = contentType.Name.ToSafeAlias(); + + contentType.Id = null; + + //save as new + IContentType newCt = new ContentType(-1); + Mapper.Map(contentType, newCt); + + ctService.Save(newCt); + + //map the saved item back to the content type (it should now get id etc set) + Mapper.Map(newCt, contentType); + return contentType; + } + + } + // TODO: This should really be centralized and used anywhere globalization applies. internal string TranslateItem(string text) {