Пример #1
0
        public async Task <ActionResult> Edit(HanEdit model)
        {
            var hg = _db.HanGroups.Include("blogs").Include("members").SingleOrDefault(m => m.HanGroupID == model.ID);

            if (hg == null)
            {
                return(NotFound());
            }
            byte[] imgbtye = null;
            if (model.HanImage != null)
            {
                imgbtye = Convert.FromBase64String(model.HanImage);
                if (imgbtye.Length > 4194304)
                {
                    ModelState.AddModelError("", "头像必须是图片,且小于4MB。");
                }
            }
            if (ModelState.IsValid)
            {
                string[] newBlogIDstr = null;
                if (model.blogIDs == null)
                {
                    newBlogIDstr = new string[] { };
                }
                else
                {
                    newBlogIDstr = model.blogIDs.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                }
                var newBlogIDs = new List <int>(newBlogIDstr.Length);
                foreach (var blogid in newBlogIDstr)
                {
                    int id;
                    if (int.TryParse(blogid, out id))
                    {
                        newBlogIDs.Add(id);
                    }
                }
                var actual = _db.Blogs.Where(blog => newBlogIDs.Contains(blog.BlogID)).Select(blog => blog.BlogID).ToList();
                if (actual.Count != newBlogIDs.Count)
                {
                    newBlogIDs = newBlogIDs.Intersect(actual).ToList();
                }
                var      blogIDs    = hg.blogs.Select(h => h.BlogID);
                var      Members    = hg.members.Select(h => h.Username);
                string[] newmembers = null;
                if (string.IsNullOrWhiteSpace(model.Members))
                {
                    newmembers = new string[] { }
                }
                ;
                else
                {
                    newmembers = model.Members.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                }
                var errnames = string.Empty;
                var errids   = string.Empty;
                var adminm   = hg.members.Where(hm => hm.GroupLvl == 1).ToList();
                var m        = new List <HanGroupMember>();
                if (imgbtye != null)
                {
                    if (!string.IsNullOrEmpty(hg.Logo))
                    {
                        await _uploadUtil.DeleteFileAsync(hg.Logo);
                    }
                    hg.Logo = _uploadUtil.GenerateImageName(hg.GroupUri);
                    await _uploadUtil.saveImageAsync(imgbtye, hg.Logo);
                }
                hg.Intro = model.Intro;
                hg.Title = model.Title;

                var existnames = _udb.Users.Where(u => newmembers.Contains(u.UserName)).Select(u => u.UserName).ToList();
                foreach (var n in newmembers)
                {
                    var name = n.Trim();
                    if (existnames.Contains(name))
                    {
                        var member = new HanGroupMember();
                        member.HanGroupID = hg.HanGroupID;
                        member.Username   = name;
                        member.GroupLvl   = 2;
                        m.Add(member);
                    }
                    else
                    {
                        errnames += name + ',';
                    }
                }
                var comp = new HanGroupMemberComparer();
                hg.members = adminm.Union(hg.members.Where(hm => hm.GroupLvl == 2).Intersect(m, comp).Union(m, comp), comp).ToList();
                //member = Group1 Union newGroup2
                var b = new List <HanGroupBlog>();
                foreach (var blogid in newBlogIDs)
                {
                    var blog = new HanGroupBlog();
                    blog.BlogID     = blogid;
                    blog.HanGroupID = hg.HanGroupID;
                    b.Add(blog);
                }
                var comparer = new HanGroupBlogComparer();
                hg.blogs = hg.blogs.Intersect(b, comparer).Union(b, comparer).ToList();
                _db.SaveChanges();
                return(Json(new { success = true }));
            }
            string error = string.Empty;

            foreach (var err in ModelState.Values.SelectMany(v => v.Errors))
            {
                error += "<li>" + err.ErrorMessage + "</li>";
            }
            return(Json(new { success = false, err = "<ul>" + error + "</ul>" }));
        }