How to find a word in this query params?

Asked

Viewed 88 times

-1

I’m doing a query that performs where based on the string filter received in query params. My filter query params has this format:

filter: (status~contains~false~and~username~contains~'admin')

I need to make a search() in that request.input('filter') and search for the word contained after the ~contains~'palavra' to do my query. I’m having a hard time doing this regex. (I’m still not sure if regex would be the way to solve the problem, but I believe it is the way.)

I would have something like:

 if(request.input('filter').search('regex??'))
            queryUsers.where('username', 'like', '%'+request.input('username')+'%')

@Edit:

what I’m trying to do is filter conditional, in a user search I can search for username and status, the frontend returns this query string and I need to check if there is username in the filter, if there is, I need these values to put in Where, currently my function is that way:

async index({request}) {
    const { page, pageSize } = request.get();
    const queryUsers = User
        .query()
        .with('user')

    let filter = request.input('filter')
    if(filter.search("username")){
        let userName = filter.replace(/(?<=')(.*?)(?=')/g, '')
        console.log(userName)
        queryUsers.where('username', 'like', '%'+userName+'%')
    }

    const users = await queryUsers.paginate(page, pageSize)

    return users
}
  • It was not very clear your question, you need which part of the string?

  • @Leandrade edited the question, see if it got better to understand

  • But by the example he gave status~contains~false~and~username~contains~'admin', you would need to take the word 'admin'?

  • that, correct. :)

  • But your regex is already doing it.

  • in fact I need the inverse of what I did, because in this way I’m replacing the value that is between ' ' by empty, so it’s everything but what I need. And when it’s not a string and a boolean, which is the status case, this logic won’t work

  • But status tbm will be able to have quotes?

  • no, so this logic doesn’t work for me, and also if there was more than one string I think it would ruin :/

Show 3 more comments

1 answer

0


For those who need it, I managed to solve it this way:

async index({request}) {
    const { page, pageSize } = request.get();
    const queryUsers = User
        .query()
        .with('user')

    let filter = request.input('filter')
    if(filter.search("username")){
        let userName = filter.match(/(?<=username~contains~').*?(?=')/)
        queryUsers.where('username', 'like', '%'+userName[0]+'%')
    }
    if(filter.search("status")){
        let userStatus = filter.match(/(?<=status~contains~)[^~)]+(?=[~)])/g)
        queryUsers.where('status', '=', userStatus[0])
    }

    const users = await queryUsers.paginate(page, pageSize)

    return users
}

This way I get the value of a string and boolean contained through my param query.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.