Skip to content

feat(api): POST /api/listening-sessions #5

@Rend1027

Description

@Rend1027
  • Define Sequelize model/controller logic (if not already)
  • Create backend route: POST /api/listening-sessions
  • Accept payload from the front end:
    {
    "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:

  • Use userId from req.user.id (via JWT)
  • Check if the song already exists in Songs table (via spotifyTrackId)
    - If not, create it
  • Save ListeningSession with user_id, song_id, lat, lng, and timestamp

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:

  1. Get the user’s ID
    2.Check if the song exists
  2. If not, create the song
  3. Create the listening session
  4. 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"
    }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions