IanTDuncan / MealTime

Project for CSC 480
0 stars 0 forks source link

Database Update on Registration - Backend Development #131

Closed Cade5480 closed 6 months ago

Cade5480 commented 7 months ago

Database Updating On Registration

Description:

I will be adding code for the account registration to insert a new userID and password into the database table

Steps to Reproduce:

N/A

Expected vs. Actual Behavior:

Expected Behavior: That on registration, userID and password entered will store into the database table Actual Behavior: At the moment when I try and insert the entered username and password values, I keep getting a email restraint saying it cant be null, which is weird because I am not touching the email and when I make the email in the database not null it does not take the restraint away.

Code snippets:

` buttonRegister.setOnClickListener { // Implement your registration logic here val newUsername = editTextNewUsername.text.toString() val newPassword = editTextNewPassword.text.toString()

        // Example: Check if new username and password are not empty
        if (newUsername.isNotEmpty() && newPassword.isNotEmpty()) {
            val dbHelper = DatabaseHelper(this)
            val db = dbHelper.writableDatabase

            // Check if the username already exists in the database
            val existingUserCursor = db.query("User", null, "username = ?", arrayOf(newUsername), null, null, null)
            existingUserCursor.use {
                if (it.count > 0) {
                    // Username already exists, display a message
                    Toast.makeText(this, "Username already taken", Toast.LENGTH_SHORT).show()
                    return@setOnClickListener
                }
            }
            // Username is unique, insert the new username and password into the database
            val values = ContentValues().apply {
                put("username", newUsername)
                put("password", newPassword)
            }
            val userId = db.insert("User", null, values)

            if (userId != -1L) {
                // Registration successful
                Toast.makeText(this, "Registration successful", Toast.LENGTH_SHORT).show()
                val intent = Intent(this, MainMenuActivity::class.java)
                startActivity(intent)
            } else {
                // Registration failed
                Toast.makeText(this, "Registration failed", Toast.LENGTH_SHORT).show()
                Log.e("Registration", "Failed to insert user")
            }

            db.close()
        } else {
            Toast.makeText(this, "Please enter new username and password", Toast.LENGTH_SHORT).show()
        }
    }`

Environment Details:

IDE: Android Studio SDK: 34.1.2

Issue Resolution:

This is the current error I am getting Error inserting username=Test password=This android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: User.email (code 1299 SQLITE_CONSTRAINT_NOTNULL)

Current Status: ToDo

aaleksandraristic commented 7 months ago

@Cade5480 Seems like we don't have a due date for this issue, have you started working on it?

aaleksandraristic commented 6 months ago

Hey Cade, I just reviewed this issue, are you still working on this issue? I would like to start testing this week?

Cade5480 commented 6 months ago

Yes I am, I just saw that Brandon committed a change to the registration page so i will have to change the code i had in place, I will try to have this finished before the weekend. Setting due date to March 22, but may finish before that date.

aaleksandraristic commented 6 months ago

Sounds good! Thank you for keeping me updated!