Beispiel #1
0
        private void FixupAttach(Attach previousValue)
        {
            if (previousValue != null && ReferenceEquals(previousValue.Content, this))
            {
                previousValue.Content = null;
            }

            if (Attach != null)
            {
                Attach.Content = this;
            }
        }
        // TODO: при ретрансляции постов принимать ИД групп/юзеров, где пост прикрепить/открепить
        private Content AttachDetach(Guid contentId, Guid? currUserId, AttachDetachTarget target, AttachOrDetach wtf)
        {
            var content = DataService.PerThread.ContentSet.SingleOrDefault(x => x.Id == contentId);
            if (content == null)
                throw new BusinessLogicException("Нет контента с указанным идентификатором");

            var attach = content.Attach;

            if (currUserId.HasValue)
            {
                if (content.GroupId.HasValue)
                {
                    var gm = GroupService.UserInGroup(currUserId.Value, content.Group.Id);

                    if (gm == null)
                        throw new BusinessLogicException("Вы не имеете права");
                    if (gm.State != (byte)GroupMemberState.Moderator)
                        throw new BusinessLogicException("Вы не имеете права");
                }
                else
                {
                    if (content.AuthorId.HasValue)
                    {
                        if (content.AuthorId.Value != currUserId.Value)
                            throw new BusinessLogicException("Вы не имеете права");
                    }
                    else
                        throw new BusinessLogicException("Вы не имеете права");
                }
            }

            if (attach == null)
            {
                switch (wtf)
                {
                    case AttachOrDetach.Attach:
                        attach = new Attach { Content = content };
                        DataService.PerThread.AttachSet.AddObject(attach);
                        break;

                    case AttachOrDetach.Detach:
                        return content;
                }
            }

            switch (target)
            {
                case AttachDetachTarget.Group:
                    switch (wtf)
                    {
                        case AttachOrDetach.Attach:
                            attach.Groups.Add(content.Group);
                            break;

                        case AttachOrDetach.Detach:
                            attach.Groups.Remove(content.Group);
                            break;
                    }
                    break;

                case AttachDetachTarget.User:
                    switch (wtf)
                    {
                        case AttachOrDetach.Attach:
                            attach.Users.Add(content.Author);
                            break;

                        case AttachOrDetach.Detach:
                            attach.Users.Remove(content.Author);
                            break;
                    }
                    break;
            }

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

            return content;
        }