Created
September 23, 2022 20:18
-
-
Save isaacbatst/59157d10cc3da1d712131d29eea14599 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const connection = require('./connection'); | |
const insert = async (travel) => { | |
const { passengerId, startingAddress, endingAddress } = travel; | |
const [{ insertId }] = await connection.execute( | |
'INSERT INTO travels' | |
+ '(passenger_id, starting_address, ending_address)' | |
+ 'VALUE (?, ?, ?)', | |
[passengerId, startingAddress, endingAddress], | |
); | |
return insertId; | |
}; | |
const findById = async (travelId) => { | |
const [[travel]] = await connection.execute( | |
'SELECT * FROM travels WHERE id = ?', | |
[travelId], | |
); | |
if (!travel) { | |
return null; | |
} | |
return { | |
passengerId: travel.passenger_id, | |
startingAddress: travel.starting_address, | |
endingAddress: travel.ending_address, | |
}; | |
}; | |
const findByTravelStatusId = async (travelStatusId) => { | |
const [travels] = await connection.execute( | |
'SELECT * FROM travels WHERE travel_status_id = ?', | |
[travelStatusId], | |
); | |
return travels.map((travel) => ({ | |
passengerId: travel.passenger_id, | |
startingAddress: travel.starting_address, | |
endingAddress: travel.ending_address, | |
})); | |
}; | |
module.exports = { | |
insert, | |
findById, | |
findByTravelStatusId, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment