示例#1
0
        object delete_category_user(string id)
        {
            var site = (Site)jc["site"];

            using (ILinqContext <CategoryUsers> cx = CategoryUsers.CreateContext())
            {
                var relation = (from q in cx
                                where q.SiteId == site.Id && q.Id == id
                                select q).FirstOrDefault();

                if (relation == null)
                {
                    return new { code = -1, msg = "指定的栏目与用户的关系不存在" }
                }
                ;

                //TODO 之前创建的文章的作者如何处理?

                cx.Remove(relation);
                cx.SubmitChanges();
            }

            return(new { code = 1, msg = "删除成功" });
        }

        #endregion
    }
示例#2
0
        object add_category_user(string userId, bool all, string[] categoryIds)
        {
            #region 校验参数

            var site = (Site)jc["site"];
            var user = User.Get(userId);

            if (user == null)
            {
                return new { code = -1, msg = "指定的用户不存在" }
            }
            ;
            if (!all && categoryIds.Length == 0)
            {
                return new { code = -2, msg = "要添加的栏目不能为空" }
            }
            ;

            #region 处理栏目

            List <Category>       categories = null;
            IQueryable <Category> query      = null;

            if (all)
            {
                query = (from q in Category.CreateContext()
                         where q.SiteId == site.Id
                         select q);
            }
            else
            {
                query = (from q in Category.CreateContext()
                         where q.SiteId == site.Id && new List <string>(categoryIds).Contains(q.Id)
                         select q);
            }

            categories = query.ToList();

            #endregion

            if (categories.Count == 0)
            {
                return new { code = -3, msg = "指定的栏目不存在" }
            }
            ;

            #endregion

            using (ILinqContext <CategoryUsers> cx = CategoryUsers.CreateContext())
            {
                var relations = (from q in CategoryUsers.CreateContext()
                                 where q.SiteId == site.Id && q.UserId == user.Id
                                 select q).ToList();

                foreach (var item in categories)
                {
                    //若存在,则不再增加
                    if (relations.FirstOrDefault(a => { return(a.CategoryId == item.Id); }) != null)
                    {
                        continue;
                    }

                    var relation = new CategoryUsers();

                    relation.Id          = StringUtil.UniqueId();
                    relation.CategoryId  = item.Id;
                    relation.UserId      = user.Id;
                    relation.SiteId      = site.Id;
                    relation.DateCreated = DateTime.Now;

                    cx.Add(relation, true);
                }

                cx.SubmitChanges(true);
            }

            return(new { code = 1, msg = "保存成功" });
        }