public Domain.Entities.Player CreatePlayerWithFacebookID(string username, string facebook_id, string device_id = null) { bool created_player = false; bool created_player_fb_link = false; Domain.Entities.Player player = null; Domain.Entities.FacebookPlayer player_facebook = null; TableServiceContext service_context = _client.GetDataServiceContext(); username = username.ToLower(); facebook_id = facebook_id.ToLower(); try{ if (IsUsernameAvailable(username) && IsFacebookIDAvailable(facebook_id)) { player = new Domain.Entities.Player(username, facebook_id); player.DeviceID = device_id; service_context.AddObject(_table_name, player); service_context.SaveChanges(); created_player = true; player_facebook = new Domain.Entities.FacebookPlayer(facebook_id, username); service_context.AddObject(_facebook_player_table_name, player_facebook); service_context.SaveChanges(); created_player_fb_link = true; return player; } else { throw new ArgumentException("Username or FB ID already registered.", "username or facebook_id"); } } catch{ try { if (service_context != null) { //revert on any error if possible if (created_player) { service_context.DeleteObject(player); service_context.SaveChanges(); } if (created_player_fb_link && player_facebook != null) { service_context.DeleteObject(player_facebook); service_context.SaveChanges(); } } } catch { } throw; } }
public bool LinkPlayersFacebookID(string username, string facebook_id) { try { TableServiceContext service_context = _client.GetDataServiceContext(); var fb_p_query = service_context.CreateQuery<Domain.Entities.FacebookPlayer>(_facebook_player_table_name); var player_query = service_context.CreateQuery<Domain.Entities.Player>(_table_name); string username_of_player_with_fbid = (from fbp in fb_p_query where fbp.PartitionKey == facebook_id.ToLower() && fbp.RowKey == facebook_id.ToLower() select fbp.Username.ToLower()).SingleOrDefault(); if (!string.IsNullOrWhiteSpace(username_of_player_with_fbid) && username.ToLower() !=username_of_player_with_fbid) //this facebook id was once tied to a username { return false; } if (username_of_player_with_fbid.ToLower() == username.ToLower()) { return true; //already linked } Domain.Entities.Player original_player_to_update = (from p in player_query where p.PartitionKey == username.Substring(0, 1).ToUpper() && p.RowKey == username.ToLower() select p).SingleOrDefault(); if (original_player_to_update == null) { return false; } original_player_to_update.FacebookID = facebook_id.ToLower(); service_context.UpdateObject(original_player_to_update); service_context.SaveChanges(); Domain.Entities.FacebookPlayer fb_player = new Domain.Entities.FacebookPlayer(facebook_id, username.ToLower()); service_context.AddObject(_facebook_player_table_name, fb_player); service_context.SaveChanges(); return true; } catch { throw; } }