using JobsJobsJobs.Shared; using Npgsql; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Threading.Tasks; namespace JobsJobsJobs.Server.Data { /// /// Extensions to the NpgslConnection type supporting the manipulation of citizens /// public static class CitizenExtensions { /// /// Populate a citizen object from the given data reader /// /// The data reader from which the values should be obtained /// A populated citizen private static Citizen ToCitizen(NpgsqlDataReader rdr) => new Citizen(CitizenId.Parse(rdr.GetString("id")), rdr.GetString("na_user"), rdr.GetString("display_name"), rdr.GetString("profile_url"), rdr.GetInstant("joined_on"), rdr.GetInstant("last_seen_on")); /// /// Retrieve a citizen by their No Agenda Social user name /// /// The NAS user name /// The citizen, or null if not found public static async Task FindCitizenByNAUser(this NpgsqlConnection conn, string naUser) { using var cmd = conn.CreateCommand(); cmd.CommandText = "SELECT * FROM citizen WHERE na_user = @na_user"; cmd.Parameters.Add(new NpgsqlParameter("@na_user", naUser)); using NpgsqlDataReader rdr = await cmd.ExecuteReaderAsync().ConfigureAwait(false); if (await rdr.ReadAsync().ConfigureAwait(false)) { return ToCitizen(rdr); } return null; } /// /// Add a citizen /// /// The citizen to be added public static async Task AddCitizen(this NpgsqlConnection conn, Citizen citizen) { using var cmd = conn.CreateCommand(); cmd.CommandText = @"INSERT INTO citizen ( na_user, display_name, profile_url, joined_on, last_seen_on, id ) VALUES( @na_user, @display_name, @profile_url, @joined_on, @last_seen_on, @id )"; cmd.Parameters.Add(new NpgsqlParameter("@id", citizen.Id.ToString())); cmd.Parameters.Add(new NpgsqlParameter("@na_user", citizen.NaUser)); cmd.Parameters.Add(new NpgsqlParameter("@display_name", citizen.DisplayName)); cmd.Parameters.Add(new NpgsqlParameter("@profile_url", citizen.ProfileUrl)); cmd.Parameters.Add(new NpgsqlParameter("@joined_on", citizen.JoinedOn)); cmd.Parameters.Add(new NpgsqlParameter("@last_seen_on", citizen.LastSeenOn)); await cmd.ExecuteNonQueryAsync().ConfigureAwait(false); } /// /// Update a citizen after they have logged on (update last seen, sync display name) /// /// The updated citizen public static async Task UpdateCitizenOnLogOn(this NpgsqlConnection conn, Citizen citizen) { using var cmd = conn.CreateCommand(); cmd.CommandText = @"UPDATE citizen SET display_name = @display_name, last_seen_on = @last_seen_on WHERE id = @id"; cmd.Parameters.Add(new NpgsqlParameter("@id", citizen.Id.ToString())); cmd.Parameters.Add(new NpgsqlParameter("@display_name", citizen.DisplayName)); cmd.Parameters.Add(new NpgsqlParameter("@last_seen_on", citizen.LastSeenOn)); await cmd.ExecuteNonQueryAsync().ConfigureAwait(false); } } }