예제 #1
0
        //[ValidateAntiForgeryToken]
        public async Task <IActionResult> OnPostAsync(int id, ContentPostModel model, IFormCollection formCollection)
        {
            try
            {
                var content = await _contentRepository.GetContent(id);

                var latestUnpublishedVersion = (await _contentVersionRepository.GetAll(id)).Where(w => w != null && w.Id > content.PublishedVersion).OrderByDescending(w => w.Id).FirstOrDefault();
                var contentVersion           = await _contentVersionRepository.GetContent(model.SavedVersion);

                var currentVersion = await _contentVersionRepository.GetContent(model.SavedVersion);

                if (string.IsNullOrWhiteSpace(model.UrlSlug) && ParentId.HasValue)
                {
                    model.UrlSlug = Name.ReplaceDiacritics();
                }

                if (!string.IsNullOrWhiteSpace(model.UrlSlug))
                {
                    model.UrlSlug = model.UrlSlug.ReplaceDiacritics();
                }

                if (model.Id == model.ParentId)
                {
                    Id              = content.Id;
                    ParentId        = contentVersion?.ParentId ?? content.ParentId;
                    Name            = contentVersion?.Name ?? content.Name;
                    UrlSlug         = contentVersion?.UrlSlug ?? content.UrlSlug;
                    Properties      = _editorReflectionService.GetModelProperties((IContent)contentVersion ?? content, out var modelType, formCollection);
                    ContentTypeGuid = modelType.GetPageDataValues().Guid;
                    AllowedGroups   = contentVersion != null ? contentVersion.AllowedGroups : content.AllowedGroups;

                    ModelState.AddModelError(nameof(IContent.ParentId), $"{nameof(IContent.ParentId)} can't be set to the current content id");

                    TryValidateModel(model);
                    if (Request.IsAjaxRequest())
                    {
                        return(new JsonResult(new { Result = false }));
                    }

                    return(Page());
                }

                var createNewVersion = false;
                if (contentVersion == null)
                {
                    createNewVersion = true;
                    contentVersion   = _mapper.Map <ContentVersion>(content);
                }
                else
                {
                    if (content.PublishedVersion == contentVersion.Id)
                    {
                        createNewVersion = true;
                    }

                    if (latestUnpublishedVersion != null && latestUnpublishedVersion.Id > contentVersion.Id && latestUnpublishedVersion.Id > content.PublishedVersion)
                    {
                        contentVersion    = _mapper.Map <ContentVersion>(content);
                        contentVersion.Id = latestUnpublishedVersion.Id;
                        createNewVersion  = false;
                    }
                }

                contentVersion.Name          = model.Name;
                contentVersion.UrlSlug       = model.UrlSlug;
                contentVersion.ParentId      = model.ParentId;
                contentVersion.Order         = content.Order;
                contentVersion.AllowedGroups = model.AllowedGroups.IsNullOrEmpty() ? null : model.AllowedGroups.ToList();
                _editorReflectionService.AddPropertiesToModel(contentVersion, formCollection);

                if (string.IsNullOrEmpty(contentVersion.ContentTypeGuid))
                {
                    contentVersion.ContentTypeGuid = _typeMappings.GetContentType(model.ContentTypeGuid).GetPageDataValues().Guid;
                }

                var cv = _mapper.Map <ContentVersion>(contentVersion);
                cv.ContentId = content.Id;

                if (cv.CompareTo(currentVersion) == 0)
                {
                    try
                    {
                        ContentVersion version;
                        if (createNewVersion)
                        {
                            version = await _contentVersionRepository.Create(cv);
                        }
                        else
                        {
                            version = await _contentVersionRepository.Update(cv);
                        }

                        content.SavedVersion = version.Id;
                        await _contentRepository.Update(content);

                        currentVersion = version;
                    }
                    catch (Exception ex)
                    {
                        Id              = content.Id;
                        ParentId        = contentVersion?.ParentId ?? content.ParentId;
                        Name            = contentVersion?.Name ?? content.Name;
                        UrlSlug         = contentVersion?.UrlSlug ?? content.UrlSlug;
                        Properties      = _editorReflectionService.GetModelProperties((IContent)contentVersion ?? content, out var modelType, formCollection);
                        ContentTypeGuid = modelType.GetPageDataValues().Guid;
                        AllowedGroups   = contentVersion != null ? contentVersion.AllowedGroups : content.AllowedGroups;

                        ModelState.AddModelError("Error", ex.Message);
                        TryValidateModel(model);

                        if (Request.IsAjaxRequest())
                        {
                            return(new JsonResult(new { Result = false }));
                        }

                        return(Page());
                    }
                }

                if (Request.IsAjaxRequest())
                {
                    return(new JsonResult(new { Result = true, CurrentVersion = currentVersion }));
                }

                return(RedirectToPage("Edit", new { id = content.Id }));
            }
            catch (Exception ex)
            {
                throw;
            }
        }
예제 #2
0
        public async Task <IActionResult> OnPostAsync(int id, ContentPostModel model, IFormCollection formCollection)
        {
            try
            {
                var content = new Content {
                    ContentTypeGuid = model.ContentTypeGuid
                };

                var modelType = _typeMappings.GetContentType(model.ContentTypeGuid);
                var pageType  = modelType.BaseType;
                var cModel    = EditorReflectionService.GetDefault(pageType);
                ((Content)cModel).ContentTypeGuid = model.ContentTypeGuid;
                content.ModelAsJson = JsonConvert.SerializeObject(cModel, Formatting.None, new JsonSerializerSettings {
                    TypeNameHandling = TypeNameHandling.All,
                    TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Full
                });
                content.ContentTypeGuid = model.ContentTypeGuid;

                if (string.IsNullOrWhiteSpace(model.UrlSlug) && model.ParentId.HasValue)
                {
                    model.UrlSlug = model.Name.ReplaceDiacritics();
                }

                if (!string.IsNullOrWhiteSpace(model.UrlSlug))
                {
                    model.UrlSlug = model.UrlSlug.ReplaceDiacritics();
                }

                if (model.Id == model.ParentId)
                {
                    ParentId        = content.ParentId;
                    Name            = content.Name;
                    UrlSlug         = content.UrlSlug;
                    Properties      = _editorReflectionService.GetModelProperties(content, out var _, formCollection);
                    ContentTypeGuid = model.ContentTypeGuid;

                    ModelState.AddModelError(nameof(IContent.ParentId), $"{nameof(IContent.ParentId)} can't be set to the current content id");

                    TryValidateModel(model);
                    return(Page());
                }

                content.Name     = model.Name;
                content.UrlSlug  = model.UrlSlug;
                content.ParentId = model.ParentId;
                _editorReflectionService.AddPropertiesToModel(content, formCollection);

                try
                {
                    await _contentRepository.Create(content);

                    var cv = _mapper.Map <ContentVersion>(content);
                    cv.ContentId = content.Id;
                    var version = await _contentVersionRepository.Create(cv);

                    content.SavedVersion = version.Id;
                    await _contentRepository.Update(content);
                }
                catch (Exception ex)
                {
                    ParentId        = content.ParentId;
                    Name            = content.Name;
                    UrlSlug         = content.UrlSlug;
                    Properties      = _editorReflectionService.GetModelProperties(content, out var _, formCollection);
                    ContentTypeGuid = model.ContentTypeGuid;

                    ModelState.AddModelError("Error", ex.Message);
                    TryValidateModel(model);
                    return(Page());
                }

                return(RedirectToPage("Edit", new { id = content.Id }));
            }
            catch
            {
                throw;
            }
        }