Home > OS >  If/then logic or use switch statement?
If/then logic or use switch statement?

Time:10-22

I'm trying to build an address verification system that does a SQL query against a predefined database. The query needs to do 3 things: 1- Look for an exact address match, and return a success JSON with the address. 2- If there is no exact match, look for a close match. If there are one or more close matches, it is to return a success JSON array with the addresses. 3 - If there are no close matches, return a failure JSON.

I'm struggling with the middle logic - what I currently has works if there is no exact match, but I do not currently know how to add an "else if" statement to look for a portion of a string.

Current

app.get("/addresses/api/find/", async (req, res) => {
    try {
        const address1 = req.query.Address1;
        const address2 = req.query.Address2;
        const city = req.query.City;
        const state = req.query.State;
        const zip = req.query.ZipCode;
        
        console.log(req.body, "Get request reached.");
        const [address] = await pool.query ("SELECT * FROM addresses WHERE (Address1, City, 
        State, ZipCode) = (?, ?, ?, ?)",
        [
            address1,
            city,
            state,
            zip
        ],
        );
            
            if(address!=''){
            res.json({
                status: "Success: 200",
                message: "There was a match to your address.",
                address
                });
            }

       //NEED TO ADD ELSE IF HERE FOR PARTIAL MATCH, PROBABLY JUST BASED ON STREET 
       //NAME IN "Address1"

            else {
            res.json({
                status: "Failure: 400",
                message: "No match found. Would you like to add your address to the 
                database?",
                address1,
                address2,
                city,
                state,
                zip
                });
            }

      } catch (err) {
        console.error(err.message)
               
            }    
        })

Here is the postman response for an exact match:

{
"status": "Success: 200",
"message": "There was a match to your address.",
"address": [
    {
        "id": 112,
        "Address1": "16 Blue Sage Circle",
        "Address2": "",
        "City": "Atlanta",
        "State": "GA",
        "ZipCode": "30318-1030"
    }
]
}

And here is the Postman response for no match:

{
"status": "Failure: 400",
"message": "No match found. Would you like to add your address to the database?",
"address1": "16 Blue Sage",
"address2": "",
"city": "Atlanta",
"state": "GA",
"zip": "30318-1030"
}

Any thoughts on how to add/trigger the else if statement, as well as best way to query SQL?

CodePudding user response:

Try to query for City, State and ZipCode and handle the match logic in code:

app.get('/addresses/api/find/', async (req, res) => {
  try {
    const address1 = req.query.Address1;
    const address2 = req.query.Address2;
    const city = req.query.City;
    const state = req.query.State;
    const zip = req.query.ZipCode;

    console.log(req.body, 'Get request reached.');

    const [rows, fields] = await pool.query(
      'SELECT * FROM addresses WHERE City = ?, State = ?, ZipCode = ?',
      [city, state, zip]
    );

    const hasExactMatch = rows.some((a) => a.Address1 === address1);
    if (hasExactMatch) {
      return res.json({
        status: 'Success: 200',
        message: 'There was a match to your address.',
        address: address1,
      });
    }

    // Here change the filter logic to suit your needs
    const hasPartialMatch = rows.some((a) =>
      a.Address1.toLowerCase().includes(address1.toLowerCase())
    );
    if (hasPartialMatch) {
      return res.json({
        status: 'Success: 200',
        message: 'There was a partial match to your address.',
        address: address1,
      });
    }

    // No match
    return res.json({
      status: 'Failure: 400',
      message:
        'No match found. Would you like to add your address to the database?',
      address1,
      address2,
      city,
      state,
      zip,
    });
  } catch (err) {
    console.error(err.message);
  }
});
  • Related