//////////  Groups //////////////

        public bool AddSCIMGroup(string groupId, SCIMGroup group)
        {
            bool response = true;

            _cache.Set(groupId, group, expiration);
            return(response);
        }
        // Groups
        public SCIMGroup createGroup(SCIMGroup group)
        {
            SCIMGroup tempGroup = new SCIMGroup();

            try
            {
                if (group.id == null)
                {
                    group.id = Guid.NewGuid().ToString();
                }

                tempGroup = _cacheService.GetSCIMGroup(group.id);
                if (tempGroup != null)
                {
                    // duplicate user
                    throw new OnPremUserManagementException("Group id already exists");
                }
                else
                {
                    _cacheService.AddSCIMGroup(group.id, group);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return(group);
        }
        public bool RemoveMemberFromSCIMGroup(string groupId, Member member)
        {
            bool      response  = true;
            SCIMGroup tempGroup = new SCIMGroup();

            if (_cache.Contains(groupId))
            {
                tempGroup = (SCIMGroup)_cache[groupId];
                _cache.Remove(groupId);

                // tempGroup.members.Remove(member);

                foreach (var item in tempGroup.members)
                {
                    if (item.value == member.value)
                    {
                        tempGroup.members.Remove(item);
                        break;
                    }
                }

                _cache.Set(groupId, tempGroup, expiration);
            }

            return(response);
        }
        /////////////////  Group membership   /////////////

        public bool AddMemberToSCIMGroup(string groupId, Member member)
        {
            bool      response  = true;
            bool      added     = false;
            SCIMGroup tempGroup = new SCIMGroup();

            if (_cache.Contains(groupId))
            {
                tempGroup = (SCIMGroup)_cache[groupId];
                _cache.Remove(groupId);

                foreach (var item in tempGroup.members)
                {
                    if (item.value == member.value)
                    {
                        tempGroup.members.Remove(item);
                        tempGroup.members.Add(member);
                        added = true;
                        break;
                    }
                }

                if (!added)
                {
                    tempGroup.members.Add(member);
                }

                _cache.Set(groupId, tempGroup, expiration);
            }

            return(response);
        }
 public IActionResult PostGroup([FromBody] SCIMGroup group)
 {
     _logger.LogDebug("PostGroup " + group.displayName);
     try
     {
         group = _connector.createGroup(group);
         string uri = "https://default.com";
         return(Created(uri, group));
     }
     catch (Exception e)
     {
         return(StatusCode(StatusCodes.Status500InternalServerError, e));
     }
 }
 public IHttpActionResult PostGroup([FromBody] SCIMGroup group)
 {
     logger.Debug("PostGroup " + group.displayName);
     try
     {
         group = connector.createGroup(group);
         string uri = Url.Link("DefaultAPI", new { id = group.id });
         return(Created <SCIMGroup>(uri, group));
     }
     catch (Exception e)
     {
         return(InternalServerError(e));
     }
 }
        public bool UpdateSCIMGroup(string groupId, SCIMGroup group)
        {
            bool      response = true;
            SCIMGroup temp     = new SCIMGroup();

            if (_cache.Contains(groupId))
            {
                _cache.Remove(groupId);
                _cache.Set(groupId, group, expiration);
            }
            else
            {
                _cache.Set(groupId, group, expiration);
            }
            return(response);
        }
        public SCIMGroup getGroup(string id)
        {
            SCIMGroup group = null;

            try
            {
                group = _cacheService.GetSCIMGroup(id);
            }
            catch (Exception e)
            {
                throw new OnPremUserManagementException("getGroup", e);
            }
            if (group == null)
            {
                throw new EntityNotFoundException(id);
            }
            return(group);
        }
        public IActionResult Delete(String id)
        {
            _logger.LogDebug("Delete id " + id);
            try
            {
                SCIMGroup group = _connector.getGroup(id);
                if (group == null)
                {
                    return(NotFound());
                }

                _connector.deleteGroup(id);
                return(StatusCode(StatusCodes.Status204NoContent));
            }
            catch (Exception e)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, e));
            }
        }
        public IHttpActionResult Delete(String id)
        {
            logger.Debug("Delete id " + id);
            try
            {
                SCIMGroup group = connector.getGroup(id);
                if (group == null)
                {
                    return(NotFound());
                }

                connector.deleteGroup(id);
                return(StatusCode(HttpStatusCode.NoContent));
            }
            catch (Exception e)
            {
                return(InternalServerError(e));
            }
        }
        //
        public SCIMGroup updateGroup(SCIMGroup group)
        {
            if (group == null)
            {
                throw new ArgumentNullException("group");
            }
            SCIMGroup existingGroup;

            existingGroup = _cacheService.GetSCIMGroup(group.id);
            if (existingGroup == null)
            {
                throw new OnPremUserManagementException("Group Not Found for Update");
            }
            else
            {
                _cacheService.UpdateSCIMGroup(group.id, group);
            }

            return(group);
        }
        public IActionResult Put(string id, [FromBody] SCIMGroup group)
        {
            SCIMGroup scimGroupOut = new SCIMGroup();

            if (id == null)
            {
                _logger.LogError("Error at PUT Group, id missing ");
                return(BadRequest());
            }
            else
            {
                _logger.LogDebug("Enter Put " + group.displayName + " Id " + id);
            }

            try
            {
                group.id     = id;
                scimGroupOut = _connector.updateGroup(group);
                if (string.IsNullOrEmpty(scimGroupOut.id))
                {
                    _logger.LogError("Exit error update group id " + id);
                    SCIMException updateException = new SCIMException();
                    updateException.ErrorMessage = "error update group id " + id;
                    updateException.ErrorSummary = "error update group id " + id;
                    return(StatusCode(StatusCodes.Status500InternalServerError));
                }
                else
                {
                    //return Ok();
                    _logger.LogDebug("Exit Successfully updated  group " + scimGroupOut.displayName);
                    return(Ok(scimGroupOut));
                }
            }

            catch (Exception e)
            {
                _logger.LogDebug("Exit Error at PUT User");
                _logger.LogError(e.ToString());
                return(StatusCode(StatusCodes.Status500InternalServerError, e));
            }
        }