/// <summary> /// Returns a contact for the given values, either from the db, /// or by creating a new one. /// </summary> /// <param name="accountId"></param> /// <param name="characterEntityId"></param> /// <param name="characterName"></param> /// <param name="server"></param> /// <param name="channelName"></param> /// <returns></returns> public Contact GetOrCreateContact(string accountId, long characterEntityId, string characterName, string server, string channelName) { using (var conn = this.Connection) { var contact = new Contact(); contact.AccountId = accountId; contact.Name = characterName; contact.Server = server; contact.ChannelName = channelName; // Try to get contact from db using (var mc = new MySqlCommand("SELECT * FROM `contacts` WHERE `characterEntityId` = @characterEntityId", conn)) { mc.Parameters.AddWithValue("@characterEntityId", characterEntityId); using (var reader = mc.ExecuteReader()) { if (reader.Read()) { contact.Id = reader.GetInt32("contactId"); return contact; } } } // Create new contact using (var cmd = new InsertCommand("INSERT INTO `contacts` {0}", conn)) { cmd.Set("accountId", accountId); cmd.Set("characterEntityId", characterEntityId); cmd.Set("characterName", characterName); cmd.Set("server", server); cmd.Execute(); contact.Id = (int)cmd.LastId; return contact; } } }
/// <summary> /// Returns all notes for contact. /// </summary> /// <param name="contact"></param> /// <returns></returns> public List<Note> GetNotes(Contact contact) { var result = new List<Note>(); using (var conn = this.Connection) using (var mc = new MySqlCommand("SELECT * FROM `notes` WHERE `receiver` = @receiver", conn)) { mc.Parameters.AddWithValue("@receiver", contact.FullName); using (var reader = mc.ExecuteReader()) { while (reader.Read()) { var note = this.ReadNote(reader); if (note == null) continue; result.Add(note); } } } return result; }
/// <summary> /// Updates contact's last login time. /// </summary> /// <param name="contact"></param> private void UpdateLastLogin(Contact contact) { using (var conn = this.Connection) using (var cmd = new UpdateCommand("UPDATE `contacts` SET {0} WHERE `contactId` = @contactId", conn)) { cmd.AddParameter("@contactId", contact.Id); cmd.Set("lastLogin", contact.LastLogin); cmd.Execute(); } }