/// <summary> /// Sign a client up for the service. Will fail if username is already in user, or email is not formatted validly. /// </summary> /// <param name="username">Client username.</param> /// <param name="password">Client password.</param> /// <param name="email">Client email.</param> /// <returns>The outcome of the operation.</returns> public Response MobileSignUp(string username, string password, string email) { using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return (Response)Common.LogError(r.message, Environment.StackTrace, r, 0); // Escape to allow the MobileTestClient to list all Mobile information // WILL BE REMOVED FOR RELEASE! if (username.Equals("list", StringComparison.OrdinalIgnoreCase)) { Response listResponse = db.MobileListMembers(); if (listResponse.error) return (Response) Common.LogError(listResponse.message, Environment.StackTrace, listResponse, 0); return listResponse; } // Validate that username and password are not blank. if (username.Length == 0 || password.Length == 0) { r.error = true; r.message = "Username or password is blank."; return r; } // Validate that username and password are not too long. if (username.Length > 20 || password.Length > 20) { r.error = true; r.message = "Username or password is longer than 20 characters."; return r; } // Validate the email address. try { var address = new System.Net.Mail.MailAddress(email); } catch { r.error = true; r.message = "Email address is not valid"; return r; } // Try to see if the username already exists. If it does, inform the client. r = db.MobileValidateUsername(username); if (r.error) return (Response)Common.LogError(r.message, Environment.StackTrace, r, 0); if (r.message.Trim() != string.Empty) { r.error = true; r.message = "That username already exists."; return r; } // Create salt and hashed/salted password; string salt = Common.CreateSalt(16); string hashSaltPassword = Common.CreatePasswordHash(password, salt); // Information seems valid, sign up client and return successfulness. r = db.MobileSignUp(username, hashSaltPassword, email, salt); if(r.error) return (Response)Common.LogError(r.message, Environment.StackTrace, r, 0); return r; } }
/// <summary> /// Add a song request to the queue. Automatically figures out of the user is already in the queue or not. /// If the song request userID is > 0, matches based on registered user id. /// If the song request userID is 0, matches based in registered user name. /// If the song request uesrID is less than 0, matches based on temporary user name. /// Automaticlaly creates the temporary user if needed. /// </summary> /// <param name="sr">The song request to add.</param> /// <param name="queueIndex">The position to add the user in, if they don't already have song requests in the queue.</param> /// <param name="DJKey">The DJ's assigned key.</param> /// <returns>The outcome of the operation. If the operation is sucessful, the client ID number is returned in result and message.</returns> public Response DJAddQueue(SongRequest sr, int queueIndex, long DJKey) { int DJID = -1; int songID = -1; int clientID = -1; using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection Response r = db.OpenConnection(); if (r.error) return r; // Convert the DJKey to a DJID r = DJKeyToID(DJKey, out DJID, db); if (r.error) return r; // Make sure the DJ isn't logged out. r = DJValidateStatus(DJID, "2", db); if (r.error) return r; // Check to see if song exists. r = db.SongExists(DJID, sr.songID); if (r.error) return r; // Make sure the songExists method returned a song. if (!int.TryParse(r.message.Trim(), out songID)) { r.error = true; r.message = "Could not find song"; return r; } if (sr.user.userID < -1) sr.user.userID = -1; // when userID == -1, we are dealing with creating an anonmymous user. if (sr.user.userID == -1) { // See if this username exists. r = db.DJValidateTempUserName(sr.user.userName, DJID); if (r.error) return r; // In this case, the username does not exist. if (r.message.Trim().Length == 0) { // Add the tempUser. r = db.DJAddTempUser(sr.user.userName, DJID); if (r.error) return r; // Get the tempUser's ID from the DB. r = db.DJValidateTempUserName(sr.user.userName, DJID); if (r.error) return r; // Parse the ID. if (!int.TryParse(r.message.Trim(), out clientID)) { r.error = true; r.message = "Unable to get the clientID of the new user."; return r; } } // In this case, the username already exists. else { // Get the tempUser's ID from the DB. r = db.DJValidateTempUserName(sr.user.userName, DJID); if (r.error) return r; // Parse the ID. if (!int.TryParse(r.message.Trim(), out clientID)) { r.error = true; r.message = "Unable to get the clientID of the temp user."; return r; } } } // When userID == 0, we look the user up by username instead of userID. else if (sr.user.userID == 0) { r = db.MobileValidateUsername(sr.user.userName); if (r.error) return r; if (!int.TryParse(r.message.Trim(), out clientID)) { r.error = true; r.message = "Client name could not be validated."; return r; } } // If a userID is passed in. else { r = db.MobileValidateID(sr.user.userID); if (r.error) return r; // See if an ID was returned. if (r.message.Trim() == String.Empty) { string s = r.message.Trim(); r.error = true; r.message = "Client ID could not be validated."; return r; } clientID = sr.user.userID; } // Get the current song Requests r = db.GetSongRequests(DJID); if (r.error) return r; string requests = r.message; string newRequests = string.Empty; // If there were no requests, simply add the single request. if (requests.Trim().Length == 0) { newRequests = clientID.ToString() + "~" + sr.songID.ToString(); //r = Common.PushMessageToMobile(sr.user.userID, "queue", db); Common.PushMessageToUsersOfDJ(DJID, "queue", db); r = db.SetSongRequests(DJID, newRequests); return r; } // Since there is a list of requests, call to parse the raw string data into an list of queuesingers. List<queueSinger> queue; r = Common.DBToMinimalList(requests, out queue); if (r.error) return r; // Search to see if the user is already in this list of singers. for (int i = 0; i < queue.Count; i++) { // We found the userID already in here. if (queue[i].user.userID == clientID) { // Loop through the songs to see if the user is already singing this song. for (int j = 0; j < queue[i].songs.Count; j++) { if (queue[i].songs[j].ID == sr.songID) { r.error = true; r.message = "User is already singing that song"; return r; } } // They dont' already have the song in the list, add them to the list Song s = new Song(); s.ID = sr.songID; queue[i].songs.Add(s); Common.MinimalListToDB(queue, out newRequests); r = db.SetSongRequests(DJID, newRequests); if (r.error) return r; Common.PushMessageToMobile(clientID, "queue", db); //Common.PushMessageToUsersOfDJ(DJID, "queue", db); r.message = clientID.ToString(); r.result = clientID; return r; } } // Now they are not in the queue, add them at queueIndex. queueSinger qs = new queueSinger(); qs.songs = new List<Song>(); qs.user = sr.user; qs.user.userID = clientID; Song song = new Song(); song.ID = sr.songID; qs.songs.Add(song); if (queueIndex < 0) queueIndex = 0; if (queueIndex > queue.Count) queueIndex = queue.Count; queue.Insert(queueIndex, qs); Common.MinimalListToDB(queue, out newRequests); r = db.SetSongRequests(DJID, newRequests); if (r.error) return r; Common.PushMessageToUsersOfDJ(DJID, "queue", db); r.message = clientID.ToString(); r.result = clientID; return r; } }
/// <summary> /// Sign a client up for the service. Will fail if username is already in user, or email is not formatted validly. /// </summary> /// <param name="username">Client username.</param> /// <param name="password">Client password.</param> /// <param name="email">Client email.</param> /// <returns>The outcome of the operation.</returns> public Response MobileSignUp(string username, string password, string email) { using (DatabaseConnectivity db = new DatabaseConnectivity()) { // Try to establish a database connection ExpResponse r = db.OpenConnection(); if (r.error) return Common.LogErrorRetNewMsg(r, Messages.ERR_SERVER, Common.LogFile.Web); // Validate that username and password are not blank. if (username.Length == 0 || password.Length == 0) { r.setErMsg(true, Messages.ERR_CRED_BLANK); return r; } // Validate that username and password are not too long. if (username.Length > 20 || password.Length > 20) { r.setErMsg(true, Messages.ERR_CRED_LONG); return r; } // Validate the email address. try { var address = new System.Net.Mail.MailAddress(email); } catch { r.setErMsg(true, Messages.ERR_BAD_EMAIL); return r; } // Try to see if the username already exists. If it does, inform the client. r = db.MobileValidateUsername(username); if (r.error) return Common.LogErrorRetNewMsg(r, Messages.ERR_SERVER, Common.LogFile.Web); if (r.message.Trim() != string.Empty) { r.setErMsg(true, Messages.ERR_CRED_TAKEN); return r; } // Create salt and hashed/salted password; string salt = Common.CreateSalt(16); string hashSaltPassword = Common.CreatePasswordHash(password, salt); // Information seems valid, sign up client and return successfulness. r = db.MobileSignUp(username, hashSaltPassword, email, salt); if (r.error) return Common.LogErrorRetNewMsg(r, Messages.ERR_SERVER, Common.LogFile.Web); return r; } }