コード例 #1
0
        public void OnGet(int parentId, string guid)
        {
            var content = new Content {
                ContentTypeGuid = guid
            };

            var modelType = _typeMappings.GetContentType(guid);
            var pageType  = modelType.BaseType;
            var model     = EditorReflectionService.GetDefault(pageType);

            ((Content)model).ContentTypeGuid = guid;
            content.ModelAsJson = JsonConvert.SerializeObject(model, Formatting.None, new JsonSerializerSettings {
                TypeNameHandling = TypeNameHandling.All,
                TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Full
            });

            ParentId   = parentId;
            Properties = _editorReflectionService.GetModelProperties(content, out var _);
            //ContentModel = JsonConvert.DeserializeObject(content.ModelAsJson, content.ModelType) as IContent;
            ContentTypeGuid = guid;
        }
コード例 #2
0
ファイル: Edit.cshtml.cs プロジェクト: Floydan/EZms.UI
        //[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;
            }
        }