/// <summary> /// this function will register a new user in the database and then return it /// </summary> /// <param name="registerController">the register controller for the new user</param> /// <returns>the newly added user</returns> public static User RegisterUser(RegisterController registerController) { #region LogAction //the action for the log String Action = "A fost inregistrat un nou utilizator la adresa de email: " + registerController.Email; //First we format the command to register String command = String.Format("INSERT INTO users.utilizatori(nume_utilizator,email,parola,nume,prenume) " + "VALUES({0},{1},{2},{3},{4}) RETURNING *", registerController.Username, registerController.Email, registerController.Password, registerController.Surname, registerController.Name ); //then we will create a new ipFunctions for the httpContextAccessor String IP = IPFunctions.GetWANIp(); #endregion //the insert returning command will return a single column based on the new insert String queryCommand = "INSERT INTO users.utilizatori(nume_utilizator,email,parola,nume,prenume) " + "VALUES(:p_username,:p_email,:p_password,:p_surname,:p_name) " + "RETURNING *"; //we bind the parameters to the registerController properties NpgsqlParameter[] queryParameters = { new NpgsqlParameter("p_username", registerController.Username), new NpgsqlParameter("p_email", registerController.Email), new NpgsqlParameter("p_password", registerController.Password), new NpgsqlParameter("p_surname", registerController.Surname), new NpgsqlParameter("p_name", registerController.Name) }; //if we are unable to connect to the database we return a null object //this should never happen since they will be on the same server though better safe than sorry if (!PgSqlConnection.OpenConnection()) { return(null); } //once done we run the sqlCommand and retrieve the values to a new DataTable DataTable result = PgSqlConnection.ExecuteReaderToDataTable(queryCommand, queryParameters); //once that is done we close the f*****g connection Miscellaneous.NormalConnectionClose(PgSqlConnection); //we will log the current action ActionLog.LogAction(Action, IP, command); //before initializing a new user from the dataTable if (result != null && result.Rows.Count > 0) { //then finally return the new user return new User { ID = (Int64)result.Rows[0]["ID"], Username = result.Rows[0]["NUME_UTILIZATOR"].ToString(), Email = result.Rows[0]["EMAIL"].ToString(), Name = result.Rows[0]["PRENUME"].ToString(), Surname = result.Rows[0]["NUME"].ToString() } } ; else { return(null); } }