Home > Back-end >  Not in condition using gorm in golang - dynamic not in condition for select statement
Not in condition using gorm in golang - dynamic not in condition for select statement

Time:03-26

I have two databases Booking and Room. Booking has roomid as one of its field. I wrote a select statement which saves the rows retrieved in result variable as stated below.

var result models.Booking
rows, err := utils.DB.Model(&currRequest).Where("check_in BETWEEN ? AND ? AND check_out BETWEEN ? AND ?", currRequest.CheckIn, currRequest.CheckOut, currRequest.CheckIn, currRequest.CheckOut).Select("room_id").Rows()
for rows.Next() {
            utils.DB.ScanRows(rows, &result)
            fmt.Println(result.RoomID)
        }

Now my result.roomid has values of roomids that satisfy the select statement from the bookings table

My result variable may have multiple room id values. I am able to retrieve the roomid values by looping through the result variable. Now I have to check in my main room database called Room and get those room ids that are not in the result struct. By using the below statement, I am only able to access the first value in result.roomid so the not in condition only considers the first values in result.roomid. How do I do the not in condition for all the values in result.roomid?

rows, err := utils.DB.Model(&models.Room{}).Not(result.RoomID).Select("room_id").Rows()

Full code:

package handlers

import (
    "encoding/json"
    "fmt"
    "net/http"
    "server/models"
    "server/utils"
    "strings"
)

func AvailableRoomsHandler(w http.ResponseWriter, r *http.Request) {

    currRequest := &models.Booking{}
    err := json.NewDecoder(r.Body).Decode(currRequest)
    //check if a valid request has been sent from front end
    if err != nil {
        //fmt.Println(err)
        var resp = map[string]interface{}{"status": false, "message": "Invalid json request"}
        json.NewEncoder(w).Encode(resp)
        return
    }

    noOfRoomsOccupied := 0
    var notinrooms string

    // Use GORM API build SQL
    //check if any rooms are available which havent been booked yet in the requested check-in and check-out dates
    var result models.Booking
    rows, err := utils.DB.Model(&currRequest).Where("check_in BETWEEN ? AND ? AND check_out BETWEEN ? AND ?", currRequest.CheckIn, currRequest.CheckOut, currRequest.CheckIn, currRequest.CheckOut).Select("room_id").Rows()
    if err != nil {
        json.NewEncoder(w).Encode(err)
        fmt.Print("error occured in select statement")
        return
    } else {
        defer rows.Close()
        for rows.Next() {
            noOfRoomsOccupied = noOfRoomsOccupied   1
            utils.DB.ScanRows(rows, &result)
            fmt.Println(result.RoomID)
            notinrooms = notinrooms   result.RoomID   ","
        }
        notinrooms = strings.TrimRight(notinrooms, ",")
        fmt.Println(notinrooms)
        //calculate the number of rooms in the database
        //rows, err := utils.DB.Model(&models.Room{}).Select("room_id").Rows()
        res := utils.DB.Find(&models.Room{})
        rowcount := res.RowsAffected
        fmt.Println(rowcount)
        if noOfRoomsOccupied == int(rowcount) {
            var resp = map[string]interface{}{"status": false, "message": "no rooms available in the specified time period"}
            json.NewEncoder(w).Encode(resp)
            return
        } else {
            noOfRooms := (currRequest.NoOfGuests   currRequest.NoOfChildren) / 2
            if (currRequest.NoOfGuests currRequest.NoOfChildren)%2 == 1 {
                noOfRooms = noOfRooms   1
            }
            if int(noOfRooms) < int(rowcount)-noOfRoomsOccupied {
                fmt.Println("number of rooms to book : ", noOfRooms)
                //assign rooms if available
                var roomids models.Room
                //rows, err := utils.DB.Model(&models.Room{}).Not(result.RoomID).Select("room_id").Rows()
                fmt.Println("rooms that can be booked")
                rows, err := utils.DB.Model(&models.Room{}).Not(result.RoomID).Select("room_id").Rows()
                //rows, err := utils.DB.Model(&models.Room{}).Not([]string{notinrooms}).Select("room_id").Rows()
                //map[string]interface{}{"name": []string{"jinzhu", "jinzhu 2"}}
                if err != nil {
                    json.NewEncoder(w).Encode(err)
                    fmt.Print("error occured in select statement to get room ids to assign")
                    return
                } else {
                    defer rows.Close()
                    for rows.Next() {
                        noOfRoomsOccupied = noOfRoomsOccupied   1
                        utils.DB.ScanRows(rows, &roomids)
                        fmt.Println(roomids.RoomID)
                    }
                }
                var success = map[string]interface{}{"message": "Select statement worked well"}
                json.NewEncoder(w).Encode(success)
                return
            }
        }
    }

}

When I do result.roomid, it only gives the first room id and eliminates only that room id in the above select statement. How do I eliminate all the room ids I found in the booking table in the rooms table data?

I tried splitting the result.roomid values and tried to form a string and gave it in the select statement but that didn't work. I tried looping through every result.roomid and ran the not in a statement but that will not make any sense.

CodePudding user response:

With this code:

var result models.Booking
rows, err := utils.DB.Model(&currRequest).Where("check_in BETWEEN ? AND ? AND check_out BETWEEN ? AND ?", currRequest.CheckIn, currRequest.CheckOut, currRequest.CheckIn, currRequest.CheckOut).Select("room_id").Rows()
if err != nil {
        json.NewEncoder(w).Encode(err)
        fmt.Print("error occured in select statement")
        return
} else {
        defer rows.Close()
        for rows.Next() {
            noOfRoomsOccupied = noOfRoomsOccupied   1
            utils.DB.ScanRows(rows, &result)
            //rest of the code
        }
}

you only get one row of potentially many rows from the result set. To get all the rows and extract their values, you should use []models.Booking.

result := []models.Booking{}
rows, err := utils.DB.Model(&currRequest).Where("check_in BETWEEN ? AND ? AND check_out BETWEEN ? AND ?", currRequest.CheckIn, currRequest.CheckOut, currRequest.CheckIn, currRequest.CheckOut).Select("room_id").Rows()
if err != nil {
        json.NewEncoder(w).Encode(err)
        fmt.Print("error occured in select statement")
        return
} else {
        defer rows.Close()
        for rows.Next() {
            var b models.Booking
            noOfRoomsOccupied = noOfRoomsOccupied   1
            utils.DB.ScanRows(rows, &b)
            result = append(result, b)
            //rest of the code
        }
}

However, since you only need roomid anyway, you could make it easier by using []uint (assuming roomid is of type uint).

result := []uint{}
rows, err := utils.DB.Model(&currRequest).Where("check_in BETWEEN ? AND ? AND check_out BETWEEN ? AND ?", currRequest.CheckIn, currRequest.CheckOut, currRequest.CheckIn, currRequest.CheckOut).Select("room_id").Rows()
if err != nil {
        json.NewEncoder(w).Encode(err)
        fmt.Print("error occured in select statement")
        return
} else {
        defer rows.Close()
        for rows.Next() {
            var rid uint
            noOfRoomsOccupied = noOfRoomsOccupied   1
            utils.DB.ScanRows(rows, &rid)
            result = append(result, rid)
            //rest of the code
        }
}

With the result being of type []uint, it would be easier to use it with the Not function (per documentation):

rows, err := utils.DB.Model(&models.Room{}).Not(result).Select("room_id").Rows()
  • Related