In controller/ also known as the Logic of the endpoint:
This creates a new entry in our ListeningSessions table that ties:
- the user
- the song
- the location
- the time
The app triggers this request after the user logs in, and you’ve collected:
- their geolocation
- their currently playing or recently played track
Remember we have 2 separate tables:
Songs — a master list of unique songs (one row per track)
ListeningSessions — every time a user listens to a track
This is why we always check if the song exist in our Song Table .. we want to avoid having duplicates.
Tip:
- Get the user’s ID
2.Check if the song exists
- If not, create the song
- Create the listening session
- send payload back to the front end :
{
"message": "Listening session saved",
"song": {
"title": "City of Gods",
"artist": "Fivio Foreign",
"preview_url": "...",
"image_url": "..."
},
"session": {
"lat": 40.7527,
"lng": -73.9772,
"timestamp": "2025-07-31T00:00:00.000Z"
}
}
{
"spotifyTrackId": "123abc",
"title": "Song Name",
"artist": "Artist Name",
"preview_url": "...",
"image_url": "...",
"lat": 40.7128,
"lng": -74.0060,
"timestamp": "optional" // if not sent, use current date
}
In controller/ also known as the Logic of the endpoint:
- If not, create it
This creates a new entry in our ListeningSessions table that ties:
The app triggers this request after the user logs in, and you’ve collected:
Remember we have 2 separate tables:
Songs — a master list of unique songs (one row per track)
ListeningSessions — every time a user listens to a track
This is why we always check if the song exist in our Song Table .. we want to avoid having duplicates.
Tip:
2.Check if the song exists
{
"message": "Listening session saved",
"song": {
"title": "City of Gods",
"artist": "Fivio Foreign",
"preview_url": "...",
"image_url": "..."
},
"session": {
"lat": 40.7527,
"lng": -73.9772,
"timestamp": "2025-07-31T00:00:00.000Z"
}
}