/// <summary> /// Checks for the Game in the database and updates it if found (excluding Id, MapId and name). If not found in DB it will be created. /// </summary> /// <param name="game"></param> /// <param name="context"></param> /// <returns></returns> public static DB_Game FromGame(Game game, FOEDatabaseDataContext context) { DB_Game da_game = null; if (game.Id.HasValue) { da_game = (from g in context.DB_Games where g.Id == game.Id.Value select g).FirstOrDefault(); } //game didnt exist, create it. if (da_game == null) { da_game = new DB_Game(); da_game.Id = Guid.NewGuid(); if (!game.MapId.HasValue) { throw new FOEServiceException(FOEStatusCodes.InvalidParameter, "Parameter MapId can not be null"); } da_game.MapId = game.MapId.Value; if (!string.IsNullOrEmpty(game.Name)) { da_game.Name = game.Name; } context.DB_Games.InsertOnSubmit(da_game); } //Update the values of the game (excluding Id, MapId and name since we dont ever weant those to change in an update.) if (game.GermanPlayer.HasValue) { da_game.GermanPlayer = game.GermanPlayer.Value; } if (game.JapanesePlayer.HasValue) { da_game.JapanesePlayer = game.JapanesePlayer.Value; } if (game.AmericanPlayer.HasValue) { da_game.AmericanPlayer = game.AmericanPlayer.Value; } if (game.GermanPlayer.HasValue) { da_game.EnglishPlayer = game.GermanPlayer.Value; } if (game.GermanPlayer.HasValue) { da_game.RussianPlayer = game.GermanPlayer.Value; } da_game.Phase = game.Phase; da_game.Turn = game.Turn; da_game.IsActive = game.IsActive; context.SubmitChanges(); return(da_game); }
/// <summary> /// /// </summary> /// <param name="target"></param> /// <returns></returns> protected static FOEDatabaseDataContext GetDatabaseConnection(string target) { Configuration webConfig = WebConfigurationManager.OpenWebConfiguration(string.Format("/{0}", target)); if (!webConfig.HasFile) { return(null); } FOEDatabaseDataContext context = new FOEDatabaseDataContext(webConfig.ConnectionStrings.ConnectionStrings["FOESQLServer"].ConnectionString); return(context); }
/// <summary> /// Base constructor. Establishes database connection. /// </summary> public RequestHandler() { if (_context == null) { ConnectionStringSettings connectionStringSettings = ConfigurationManager.ConnectionStrings["FOESQLServer"]; if (connectionStringSettings == null) { _context = new FOEDatabaseDataContext(); } else { _context = new FOEDatabaseDataContext(connectionStringSettings.ConnectionString); } } }
protected void Page_Load(object sender, EventArgs e) { String ConnectionString = "?"; try { FOEDatabaseDataContext context = null; try { try { context = new FOEDatabaseDataContext(ConfigurationManager.ConnectionStrings["FOESQLServer"].ConnectionString); } catch (Exception) { context = new FOEDatabaseDataContext(); } ConnectionString = context.Connection.ConnectionString; //commented out since the connecion string will contain userName and password for the remote DB server from now on. Not that good if thats made available publically. //lblConnectionString.Text = String.Format("*** {0} ***", ConnectionString); } finally { if (context != null) { context.Dispose(); } } ServiceRequestHandler request = new ServiceRequestHandler(Guid.NewGuid()); } catch (FOEServiceException ex) { // We know it is an invalid session if (ex.Reason != FOEStatusCodes.InvalidSession) { throw new Exception(ConnectionString, ex); // If it is another error, throw the exception again } } catch (Exception ex) { throw new Exception(ConnectionString, ex); } }
/// <summary> /// Looks for the inviteId in teh database, if not found it creates a new DB_Invite and returns that. /// </summary> /// <param name="invite"></param> /// <param name="context"></param> /// <returns></returns> public static DB_Invite FromInvite(Invite invite, FOEDatabaseDataContext context) { DB_Invite da_invite = null; //if user exists, get it. if (invite.Id.HasValue) { da_invite = (from i in context.DB_Invites where i.Id == invite.Id.Value select i).FirstOrDefault(); if (da_invite == null) { throw new FOEServiceException(FOEStatusCodes.InvalidInvite, "Id reference to invite unknown."); } } else { //if no Id was passed in, create a user. da_invite = new DB_Invite(); da_invite.InvitedUser = invite.InvitedUser; da_invite.InvitingUser = invite.InvitingUser; da_invite.Id = invite.Id.Value; da_invite.Status = invite.Status; da_invite.GameId = invite.GameId; context.DB_Invites.InsertOnSubmit(da_invite); context.SubmitChanges(); } //Update the user if (da_invite != null) { da_invite.Status = invite.Status; } context.SubmitChanges(); return(da_invite); }
/// <summary> /// Extracts information from the provided User and adds it as an entity to the db. /// </summary> /// <param name="user"></param> /// <param name="context"></param> /// <returns></returns> public static DB_User FromUser(User user, FOEDatabaseDataContext context) { DB_User da_user = null; //Check that the user doesnt already exist in the db. if (user.Id.HasValue) { da_user = (from u in context.DB_Users where u.Id == user.Id.Value select u).FirstOrDefault(); if (da_user == null) { throw new FOEServiceException(FOEStatusCodes.UnknownUser, String.Format("Id reference to user is unknown: {0}", user.Id.Value)); } } else { if (!string.IsNullOrEmpty(user.UserName)) { da_user = (from u in context.DB_Users where u.UserName == user.UserName select u).FirstOrDefault(); } } //User didnt exist, try to create it. if (da_user == null) { //TODO: Add canInsert check here? if (string.IsNullOrEmpty(user.UserName)) { throw new FOEServiceException(FOEStatusCodes.InvalidParameter, "Paremeter UserName cannot be null"); } if (string.IsNullOrEmpty(user.Password)) { throw new FOEServiceException(FOEStatusCodes.InvalidParameter, "Paremeter Password cannot be null"); } da_user = new DB_User() { Id = Guid.NewGuid(), UserName = user.UserName, Password = user.Password, Email = user.Email }; context.DB_Users.InsertOnSubmit(da_user); context.SubmitChanges(); } else { //TODO: Add canUpdate check here? if (!string.IsNullOrEmpty(user.Password)) { da_user.Password = user.Password; } if (!string.IsNullOrEmpty(user.UserName)) { da_user.UserName = user.UserName; } if (!string.IsNullOrEmpty(user.Email)) { da_user.Email = user.Email; } } if (da_user == null) { throw new FOEServiceException(FOEStatusCodes.InternalError, "Unable to create DB_User"); } return(da_user); }