public ActionResult Group(MvcGroupModel model)
 {
     var group = new GroupDetailModel
     {
         Id = model.Id,
         Name = model.Name,
         Leaders = model.Leaders == null ? new List<SmsPersonModel>() : model.Leaders.Select(l => new SmsPersonModel { Id = Convert.ToInt32(l) }).ToList(),
         Members = model.Members == null ? new List<SmsPersonModel>() : model.Members.Select(m => new SmsPersonModel { Id = Convert.ToInt32(m) }).ToList(),
     };
     _message.Save(group);
     return Redirect(Url.Action("Index", "Messaging"));
 }
示例#2
0
        public void Save(GroupDetailModel model)
        {
            SmsGroup dbGroup;
            if (model.Id == default(int))
            {
                dbGroup = new SmsGroup
                {
                    Name = model.Name
                };
                _uow.SmsGroups.Add(dbGroup);
            }
            else
            {
                dbGroup = _uow
                        .SmsGroups
                        .FindAll()
                        .Include("SmsGroupMembers.SmsPerson")
                        .Include("SmsGroupLeaders.SmsPerson")
                        .First(g => g.Id == model.Id);

                dbGroup.Name = model.Name;
            }
            _uow.SaveChanges();

            //To Add
            var leadersToAdd = model.Leaders.Where(l => !dbGroup.SmsGroupLeaders.Any(x => x.SmsPersonId == l.Id)).ToList();
            foreach (var toAdd in leadersToAdd)
            {
                _uow.SmsGroupLeaders.Add(new SmsGroupLeader
                {
                    SmsGroupId = dbGroup.Id,
                    SmsPersonId = toAdd.Id,
                });
            }

            var membersToAdd = model.Members.Where(l => !dbGroup.SmsGroupMembers.Any(x => x.SmsPersonId == l.Id)).ToList();
            foreach (var toAdd in membersToAdd)
            {
                _uow.SmsGroupMembers.Add(new SmsGroupMember
                {
                    SmsGroupId = dbGroup.Id,
                    SmsPersonId = toAdd.Id,
                });
            }

            //To Delete
            var leadersToDelete = dbGroup.SmsGroupLeaders.Where(l => !model.Leaders.Any(x => x.Id == l.SmsPersonId)).ToList();
            foreach (var toDelete in leadersToDelete)
            {
                _uow.SmsGroupLeaders.Remove(toDelete);
            }

            var membersToDelete = dbGroup.SmsGroupMembers.Where(l => !model.Members.Any(x => x.Id == l.SmsPersonId)).ToList();
            foreach (var toDelete in membersToDelete)
            {
                _uow.SmsGroupMembers.Remove(toDelete);
            }
            _uow.SaveChanges();
        }