public User GetOrCreateContact(string accountId, long characterEntityId, string characterName, string server, string channelName)
{
using (var conn = this.Connection)
{
var user = new User();
user.AccountId = accountId;
user.CharacterId = characterEntityId;
user.Name = characterName;
user.Server = server;
user.ChannelName = channelName;
user.Status = ContactStatus.Online;
user.ChatOptions = ChatOptions.NotifyOnFriendLogIn;
user.LastLogin = DateTime.Now;
// 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())
{
user.Id = reader.GetInt32("contactId");
user.Status = (ContactStatus)reader.GetByte("status");
user.ChatOptions = (ChatOptions)reader.GetUInt32("chatOptions");
user.Nickname = reader.GetStringSafe("nickname") ?? "";
user.LastLogin = reader.GetDateTimeSafe("lastLogin");
if (!Enum.IsDefined(typeof(ContactStatus), user.Status) || user.Status == ContactStatus.None)
user.Status = ContactStatus.Online;
this.UpdateLastLogin(user);
return user;
}
}
}
// 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.Set("status", (byte)user.Status);
cmd.Set("chatOptions", (uint)user.ChatOptions);
cmd.Set("nickname", "");
cmd.Set("lastLogin", user.LastLogin);
cmd.Execute();
user.Id = (int)cmd.LastId;
return user;
}
}
}