예제 #1
0
        public void AddListUsers([FromBody] CreateGroup createobj)
        {
            Group group = new Group();

            group.Name     = createobj.name;
            group.IsActive = true;
            Group tempGroup = _context.Groups.Where(g => g.Name.Equals(group.Name)).FirstOrDefault <Group>();

            if (tempGroup == null)
            {
                _context.Groups.Add(group);
                _context.SaveChanges();
                group = _context.Groups.Where(g => g.Name.Equals(group.Name)).FirstOrDefault <Group>();
            }
            else
            {
                group          = tempGroup;
                group.IsActive = true;
            }


            List <UserGroup> userGroups = new List <UserGroup>();

            for (int i = 0; i < createobj.users.Length; i++)
            {
                string    standard  = "";
                UserGroup userGroup = new UserGroup();
                User      dbUser    = _context.Users.FirstOrDefault(x => x.Token == createobj.users.ElementAt(i).Token);
                userGroup.GroupId = group.Id;
                userGroup.UserId  = dbUser.Id;
                userGroups.Add(userGroup);
                _notificationMessages.GroupNotification(_context, group, dbUser);
            }
            _context.UserGroups.AddRange(userGroups);
            _context.SaveChanges();
        }