Пример #1
0
        // 128=~256=Eli Russo~512=~2048=~8192=Christine Gerkhardt~32768=~65536=~131072=~1048576=~
        public bool UpdateAttendance(int evid, int activeRoles, string staffStr)
        {
            try
            {
                // Check for and delete people assigned to un-needed roles.
                List <int> RoleList = Enum.GetValues(typeof(Attendance.AttendanceRoles)).Cast <int>().Where(r => (int)r >= (int)Attendance.AttendanceRoles.Sch_Host).Select(x => x).ToList();

                foreach (var r in RoleList)
                {
                    if ((r & activeRoles) == 0)
                    {
                        // Staff Role now not needed
                        Attendance delAtt = _context.Attendance.AsNoTracking().FirstOrDefault(a => (a.EventId == evid) && (a.RoleType == r));
                        if (delAtt != null)
                        {
                            _context.Attendance.Remove(delAtt);
                            _context.SaveChanges();
                        }
                    }
                }

                if (!string.IsNullOrEmpty(staffStr))
                {
                    List <KeyValuePair <int, string> > kvpList = new List <KeyValuePair <int, string> >();
                    var pairs = staffStr.Split("~");
                    if (pairs.Length == 0)
                    {
                        return(false);
                    }
                    foreach (var p in pairs)
                    {
                        var flds = p.Split("=");
                        if (flds.Length >= 1)
                        {
                            var keyStr = flds[0].Trim();
                            if (!string.IsNullOrEmpty(keyStr))
                            {
                                if (int.TryParse(keyStr, out int key))
                                {
                                    var val = (flds.Length == 2) ? flds[1].Trim() : "";
                                    var kvp = new KeyValuePair <int, string>(key, val);
                                    kvpList.Add(kvp);
                                }
                            }
                        }
                    }

                    kvpList.ForEach(kv =>
                    {
                        Attendance att = _context.Attendance.AsNoTracking().FirstOrDefault(a => (a.EventId == evid) && (a.RoleType == kv.Key));
                        if (att != null)
                        {
                            if (string.IsNullOrEmpty(kv.Value))
                            {
                                // Delete current
                                _context.Attendance.Remove(att);
                                _context.SaveChanges();
                            }
                            else
                            {
                                // Update Current
                                List <Person> persList = Person.PeopleFromNameHint(_context, kv.Value);
                                if (persList.Count == 1)
                                {
                                    att.PersonId = persList[0].Id;

                                    _context.Attendance.Attach(att);
                                    _context.Entry(att).State = EntityState.Modified;
                                    _context.SaveChanges();
                                }
                            }
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(kv.Value))
                            {
                                // Add new Attendance
                                var newAtt      = new Attendance();
                                newAtt.EventId  = evid;
                                newAtt.RoleType = kv.Key;

                                List <Person> persList = Person.PeopleFromNameHint(_context, kv.Value);
                                newAtt.PersonId        = (persList.Count == 1) ? persList[0].Id : Person.AddPersonFromNameHint(_context, kv.Value);
                                if (newAtt.PersonId > 0)
                                {
                                    _context.Attendance.Add(newAtt);
                                    _context.SaveChanges();
                                }
                            }
                        }
                    });
                }
            }
            catch
            {
                return(false);
            }
            return(true);
        }