Example #1
0
        public Post UpdatePost(Guid postId, Guid?editorId, string newTitle, string newText, string newTagsString, bool isDraft)
        {
            var post = DataService.PerThread.ContentSet.OfType <Post>().SingleOrDefault(p => p.Id == postId);

            if (post == null)
            {
                throw new BusinessLogicException("Не найден пост с указанным идентификатором");
            }

            if (!editorId.HasValue)
            {
                post.State = isDraft ? (byte)ContentState.Draft : (byte)ContentState.Approved;
            }
            else
            {
                if (post.GroupId.HasValue)
                {
                    var group = DataService.PerThread.GroupSet.SingleOrDefault(x => x.Id == post.GroupId.Value);
                    if (group == null)
                    {
                        throw new BusinessLogicException("Указан неверный идентификатор группы");
                    }

                    var gm = GroupService.UserInGroup(editorId.Value, group, true);

                    if (isDraft)
                    {
                        post.State = (byte)ContentState.Draft;
                    }
                    else
                    {
                        if (group.PrivacyEnum.HasFlag(GroupPrivacy.ContentModeration))
                        {
                            if (gm.State == (byte)GroupMemberState.Moderator)
                            {
                                post.State = (byte)ContentState.Approved;
                            }
                            else
                            {
                                if (!post.AuthorId.HasValue)
                                {
                                    throw new BusinessLogicException("Вы не являетесь автором поста");
                                }
                                if (post.AuthorId.Value != editorId.Value)
                                {
                                    throw new BusinessLogicException("Вы не являетесь автором поста");
                                }

                                post.State = (byte)ContentState.Premoderated;
                            }
                        }
                        else
                        {
                            if (post.IsExternal)
                            {
                                post.State = gm.State == (byte)GroupMemberState.Moderator
                                                 ? (byte)ContentState.Approved
                                                 : (byte)ContentState.Premoderated;
                            }
                            else
                            {
                                post.State = (byte)ContentState.Approved;
                            }
                        }
                    }
                }
                else
                {
                    if (!post.AuthorId.HasValue)
                    {
                        throw new BusinessLogicException("Вы не являетесь автором поста");
                    }
                    if (post.AuthorId.Value != editorId.Value)
                    {
                        throw new BusinessLogicException("Вы не являетесь автором поста");
                    }

                    post.State = isDraft ? (byte)ContentState.Draft : (byte)ContentState.Approved;
                }
            }

            if (!post.PublishDate.HasValue)
            {
                post.PublishDate = DateTime.Now;
            }

            post.Title = newTitle;
            post.Text  = newText;
            post.Tags.Clear();

            var tags = TagsHelper.ConvertStringToTagList(newTagsString, post.GroupId);

            foreach (var tag in tags)
            {
                if (!post.Tags.Contains(tag))
                {
                    post.Tags.Add(tag);
                }
            }

            DataService.PerThread.SaveChanges();
            UpdateContentCache(post);

            return(post);
        }
Example #2
0
        public Post CreatePost(Guid?groupId, Guid?authorId, string title, string text, string tagsString, bool isDraft, bool isExternal)
        {
            var post = new Post
            {
                AuthorId     = authorId,
                GroupId      = groupId,
                IsExternal   = isExternal,
                CreationDate = DateTime.Now,
                PublishDate  = !isDraft ? DateTime.Now : (DateTime?)null,
                Tags         = new List <Tag>(),
                Title        = title,
                Text         = text
            };

            foreach (var tag in TagsHelper.ConvertStringToTagList(tagsString, post.GroupId))
            {
                post.Tags.Add(tag);
            }

            if (!authorId.HasValue)
            {
                if (!groupId.HasValue)
                {
                    throw new BusinessLogicException("Укажите принадлежность поста к группе или пользователю");
                }

                post.State = isDraft ? (byte)ContentState.Draft : (byte)ContentState.Approved;
            }
            else
            {
                if (groupId.HasValue)
                {
                    var group = DataService.PerThread.GroupSet.SingleOrDefault(x => x.Id == groupId.Value);
                    if (group == null)
                    {
                        throw new BusinessLogicException("Указан неверный идентификатор группы");
                    }

                    var gm = GroupService.UserInGroup(authorId.Value, group, true);

                    if (isDraft)
                    {
                        post.State = (byte)ContentState.Draft;
                    }
                    else
                    {
                        if (group.PrivacyEnum.HasFlag(GroupPrivacy.ContentModeration))
                        {
                            post.State = gm.State == (byte)GroupMemberState.Moderator
                                             ? (byte)ContentState.Approved
                                             : (byte)ContentState.Premoderated;
                        }
                        else
                        {
                            if (isExternal)
                            {
                                post.State = gm.State == (byte)GroupMemberState.Moderator
                                                 ? (byte)ContentState.Approved
                                                 : (byte)ContentState.Premoderated;
                            }
                            else
                            {
                                post.State = (byte)ContentState.Approved;
                            }
                        }
                    }
                }
                else
                {
                    post.State = isDraft ? (byte)ContentState.Draft : (byte)ContentState.Approved;
                }
            }

            DataService.PerThread.ContentSet.AddObject(post);
            DataService.PerThread.SaveChanges();
            UpdateContentCache(post);

            return(post);
        }