コード例 #1
0
        public async Task <PKMember> AddMember(string identifier, string potentialHid, string potentialName, MemberPatch patch)
        {
            // See if we can find a member that matches this one
            // if not, roll a new hid and we'll insert one with that
            // (we can't trust the hid given in the member, it might let us overwrite another system's members)
            var    existingMember = FindExistingMemberInSystem(potentialHid, potentialName);
            string newHid         = existingMember?.Hid ?? await _conn.QuerySingleAsync <string>("find_free_member_hid", commandType : CommandType.StoredProcedure);

            // Upsert member data and return the ID
            QueryBuilder qb = QueryBuilder.Upsert("members", "hid")
                              .Constant("hid", "@Hid")
                              .Constant("system", "@System");

            if (patch.Name.IsPresent)
            {
                qb.Variable("name", "@Name");
            }
            if (patch.DisplayName.IsPresent)
            {
                qb.Variable("display_name", "@DisplayName");
            }
            if (patch.Description.IsPresent)
            {
                qb.Variable("description", "@Description");
            }
            if (patch.Color.IsPresent)
            {
                qb.Variable("color", "@Color");
            }
            if (patch.AvatarUrl.IsPresent)
            {
                qb.Variable("avatar_url", "@AvatarUrl");
            }
            if (patch.ProxyTags.IsPresent)
            {
                qb.Variable("proxy_tags", "@ProxyTags");
            }
            if (patch.Birthday.IsPresent)
            {
                qb.Variable("birthday", "@Birthday");
            }
            if (patch.KeepProxy.IsPresent)
            {
                qb.Variable("keep_proxy", "@KeepProxy");
            }

            var newMember = await _conn.QueryFirstAsync <PKMember>(qb.Build("returning *"),
                                                                   new
            {
                Hid         = newHid,
                System      = _systemId,
                Name        = patch.Name.Value,
                DisplayName = patch.DisplayName.Value,
                Description = patch.Description.Value,
                Color       = patch.Color.Value,
                AvatarUrl   = patch.AvatarUrl.Value,
                KeepProxy   = patch.KeepProxy.Value,
                ProxyTags   = patch.ProxyTags.Value,
                Birthday    = patch.Birthday.Value
            });

            // Log this member ID by the given identifier
            _knownMembers[identifier] = newMember.Id;
            return(newMember);
        }