Exemplo n.º 1
0
        public async Task <IActionResult> Create(PostViewModel model)
        {
            if (ModelState.IsValid)
            {
                var entity = model.ToEntity();

                // parse slug
                entity.Slug = entity.GetSeName();

                // parse slug
                entity.Slug = entity.GetSeName();

                // categories
                await UpdatePostCategories(model, entity);

                // tags
                await UpdatePostTags(model, entity);

                // author
                var currentUser = await _userManager.GetUserAsync(User);

                entity.UserId = currentUser.Id;


                await _postManager.CreateAsync(entity);

                AlertSuccess("添加成功。");

                return(RedirectToAction(nameof(List)));
            }

            PrepareViewModel(model, null);

            return(View(model));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Post(PostViewModel model)
        {
            var duty = await _dutyManager.GetDutyAsync(model.Id);

            if (duty == null)
            {
                return(NotFound());
            }

            var user = await GetCurrentUser();

            if (duty.ClassRoomId != user.ClassRoomId)
            {
                return(Unauthorized());
            }

            if (ModelState.IsValid)
            {
                var post = new DutyPost
                {
                    DutyId  = duty.Id,
                    Content = model.Content,
                    UserId  = user.Id,
                };

                if (await _postManager.CreateAsync(post))
                {
                    var callbackUrl = Url.Page(
                        "/Duty/Details",
                        pageHandler: null,
                        values: new { id = duty.Id },
                        protocol: Request.Scheme);

                    //_mailingCron.Proc(new MassMailing()
                    //{
                    //    ClassRoomId = user.ClassRoomId,
                    //    Distribution = DistributeType.CLASS,
                    //    Email = new Email()
                    //    {
                    //        Title = "Commentaire de devoir",
                    //        Template = "new-post",
                    //        Attachment = new List<KeyValuePair<string, string>>()
                    //        {
                    //            new KeyValuePair<string, string>("{author}", user.GetFullName()),
                    //            new KeyValuePair<string, string>("{content}", duty.Content.Replace("\n", "<br>")),
                    //            new KeyValuePair<string, string>("{subject}", duty.Subject.Name),
                    //            new KeyValuePair<string, string>("{url}", callbackUrl),
                    //        }
                    //    }
                    //});
                }

                return(RedirectToAction("Details", new { id = duty.Id }));
            }

            ViewBag.Duty = duty;
            return(View(model));
        }
        private async Task <bool> AddPost(BlogMlExtendedPost extPost)
        {
            var p = new Post();

            p.Title                = extPost.BlogPost.Title;
            p.CreationTime         = extPost.BlogPost.DateCreated;
            p.PublishedTime        = extPost.BlogPost.DateCreated;
            p.LastModificationTime = extPost.BlogPost.DateModified;
            p.Content              = extPost.BlogPost.Content.UncodedText;
            p.Description          = extPost.BlogPost.Excerpt.UncodedText;
            p.IsDraft              = !extPost.BlogPost.Approved;
            p.ViewsCount           = GetValue(extPost.BlogPost.Views);

            if (extPost.BlogPost.HasExcerpt)
            {
                p.Description = extPost.BlogPost.Excerpt.UncodedText;
            }

            if (!string.IsNullOrEmpty(extPost.PostUrl))
            {
                // looking for a Slug with patterns such as:
                //    /some-slug.aspx
                //    /some-slug.html
                //    /some-slug
                //
                Match slugMatch = Regex.Match(extPost.PostUrl, @"/([^/\.]+)(?:$|\.[\w]{1,10}$)", RegexOptions.IgnoreCase);
                if (slugMatch.Success)
                {
                    p.Slug = slugMatch.Groups[1].Value.Trim();
                }
            }

            if (string.IsNullOrEmpty(p.Slug))
            {
                p.Slug = p.GetSeName();
            }

            // skip if exists
            if (await _postManager.FindBySlugAsync(p.Slug) != null)
            {
                return(false);
            }

            if (extPost.BlogPost.Authors != null && extPost.BlogPost.Authors.Count > 0)
            {
                // p.UserId = extPost.BlogPost.Authors[0].Ref;
                p.UserId = _author.Id;
            }


            if (extPost.Categories != null && extPost.Categories.Count > 0)
            {
                foreach (var item in extPost.Categories)
                {
                    p.Categories.Add(new PostCategory()
                    {
                        CategoryId = item.Id, PostId = item.Id
                    });
                }
            }


            if (extPost.Tags != null && extPost.Tags.Count > 0)
            {
                foreach (var item in extPost.Tags)
                {
                    var tag = await _tagsManager.CreateOrUpdateAsync(item);

                    p.Tags.Add(new PostTags()
                    {
                        PostId = p.Id, TagsId = tag.Id
                    });
                }
            }

            await _postManager.CreateAsync(p);

            if (extPost.Comments != null && extPost.Comments.Count > 0)
            {
                foreach (var comment in extPost.Comments)
                {
                    comment.PostId = p.Id;
                    try
                    {
                        await _commentManager.CreateAsync(comment);
                    }
                    catch (Exception)
                    {
                    }
                }

                p.CommentsCount = extPost.Comments.Count;

                await _postManager.UpdateAsync(p);
            }

            return(true);
        }
        /// <summary>
        /// metaWeblog.newPost method
        /// </summary>
        /// <param name="blogId">
        /// always 1000 in BlogEngine since it is a singlar blog instance
        /// </param>
        /// <param name="userName">
        /// login username
        /// </param>
        /// <param name="password">
        /// login password
        /// </param>
        /// <param name="sentPost">
        /// struct with post details
        /// </param>
        /// <param name="publish">
        /// mark as published?
        /// </param>
        /// <returns>
        /// postID as string
        /// </returns>
        internal async Task <string> NewPost(string blogId, string userName, string password, MWAPost sentPost, bool publish)
        {
            var currentUser = await GetVerifyUserAsync(userName, password);

            //if (!_permissionChecker.IsValid(currentUser, PermissionKeys.PostCreate))
            //{
            //    throw new MetaWeblogException("11", "User authentication failed");
            //}

            var post = new Post
            {
                UserId      = currentUser.Id,
                Title       = sentPost.title,
                Content     = sentPost.description,
                IsDraft     = !publish,
                Slug        = sentPost.slug,
                Description = sentPost.excerpt,
            };

            string authorName = String.IsNullOrEmpty(sentPost.author) ? userName : sentPost.author;

            var user = await _userManager.FindByEmailAsync(authorName);

            if (user != null)
            {
                post.UserId = user.Id;
            }

            if (sentPost.commentPolicy != string.Empty)
            {
                post.EnableComment = sentPost.commentPolicy == "1";
            }

            post.Categories.Clear();
            foreach (var item in sentPost.categories.Where(c => c != null && c.Trim() != string.Empty))
            {
                Category cat;
                if (LookupCategoryGuidByName(item, out cat))
                {
                    post.Categories.Add(new PostCategory()
                    {
                        CategoryId = cat.Id, PostId = post.Id
                    });
                }
                else
                {
                    // Allowing new categories to be added.  (This breaks spec, but is supported via WLW)
                    var newcat = new Category()
                    {
                        Name         = item,
                        DisplayOrder = 1,
                    };

                    post.Categories.Add(new PostCategory()
                    {
                        Category = newcat, PostId = post.Id
                    });
                }
            }

            post.Tags.Clear();
            foreach (var item in sentPost.tags.Where(item => item != null && item.Trim() != string.Empty))
            {
                var tag = await _tagsManager.CreateOrUpdateAsync(item);

                post.Tags.Add(new PostTags()
                {
                    TagsId = tag.Id, PostId = post.Id
                });
            }

            post.CreationTime = sentPost.postDate == new DateTime() ? DateTime.Now : sentPost.postDate;

            await _postManager.CreateAsync(post);

            return(post.Id.ToString());
        }