public async Task <PKMember> AddMember(string identifier, PKMember member) { // 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(member.Hid, member.Name); 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") .Variable("name", "@Name") .Variable("keep_proxy", "@KeepProxy"); if (member.DisplayName != null) { qb.Variable("display_name", "@DisplayName"); } if (member.Description != null) { qb.Variable("description", "@Description"); } if (member.Color != null) { qb.Variable("color", "@Color"); } if (member.AvatarUrl != null) { qb.Variable("avatar_url", "@AvatarUrl"); } if (member.ProxyTags != null) { qb.Variable("proxy_tags", "@ProxyTags"); } if (member.Birthday != null) { qb.Variable("birthday", "@Birthday"); } var newMember = await _conn.QueryFirstAsync <PKMember>(qb.Build("returning *"), new { Hid = newHid, System = _systemId, member.Name, member.DisplayName, member.Description, member.Color, member.AvatarUrl, member.KeepProxy, member.ProxyTags, member.Birthday }); // Log this member ID by the given identifier _knownMembers[identifier] = newMember.Id; return(newMember); }
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.Pronouns.IsPresent) { qb.Variable("pronouns", "@Pronouns"); } 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"); } // don't overwrite message count on existing members if (existingMember == null) { if (patch.MessageCount.IsPresent) { qb.Variable("message_count", "@MessageCount"); } } 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, Pronouns = patch.Pronouns.Value, Color = patch.Color.Value, AvatarUrl = patch.AvatarUrl.Value, KeepProxy = patch.KeepProxy.Value, ProxyTags = patch.ProxyTags.Value, Birthday = patch.Birthday.Value, MessageCount = patch.MessageCount.Value, }); // Log this member ID by the given identifier _knownMembers[identifier] = newMember.Id; return(newMember); }
public static UpdateQueryBuilder Upsert(string table, string conflictField) => new UpdateQueryBuilder(QueryBuilder.Upsert(table, conflictField));