Skip to content

[PS-182] Backend should store play history - #91

Open
NatachaKey wants to merge 1 commit into
mainfrom
backendStoresPlayHistory
Open

[PS-182] Backend should store play history#91
NatachaKey wants to merge 1 commit into
mainfrom
backendStoresPlayHistory

Conversation

@NatachaKey

Copy link
Copy Markdown
Collaborator

No description provided.

@akosasante akosasante left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good. One thing to note though, looks like there's a bit of overlap with @DYA13's work here: https://github.com/Code-the-Dream-School/dd-prac-team4-back/pull/90/files

Since this schema has some more fields I will recommend that we use this one going forward.

artistName: {
type: String,
required: true,
//AKOS: should i delete this field because we have no artist model and we can't reference it here ?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could leave it like this, and that means when creating a RecentlyListened object we'd need to do something like: await RecentlyListened.create({user, album, artistName: album.artistName}).

But then that leaves the possibility for bugs where someone accidentally passes in a different artist that doesn't match the album name.

I think a better way would be to have artistName be a virtual field that just pulls the value from the album:

recentlyListenedSchema.virtual('artistName', { ref: 'Album', localField: 'album', foreignField: 'artistName', justOne: true });

// below is in node console:

> let x = await RecentlyListened.findOne({})
undefined
> x.artistName
'Luke Bryan'
> x
{
  _id: new ObjectId("665f69c1150dc898bdd44267"),
  user: new ObjectId("64d6a966a15d2e18ab96a263"),
  album: new ObjectId("64d2ae2308a725b72bd5c0db"),
  timeListened: 2024-06-04T19:23:45.664Z,
  __v: 0
}

Or we could leave out the artistName altogether, and just make sure that we are always populating it from the album when fetching like this:

// populate the full album
await RecentlyListened.findOne({}).populate('album')
{
  _id: new ObjectId("665f69c1150dc898bdd44267"),
  user: new ObjectId("64d6a966a15d2e18ab96a263"),
  artistName: 'Luke Bryan',
  album: {
    _id: new ObjectId("64d2ae2308a725b72bd5c0db"),
    artistName: 'Luke Bryan',
    albumName: 'Crash My Party...Again',
    price: 80,
    image: 'https://i.scdn.co/image/ab67616d0000b27322df448bba2c2b671d8502c8',
    releaseDate: 2023-08-04T00:00:00.000Z,
    spotifyUrl: 'https://api.spotify.com/v1/albums/3f4MmjZHwYHNHdnodbOHRe',
    averageRating: 3,
    numOfReviews: 4,
    __v: 0,
    createdAt: 2023-08-08T21:05:39.694Z,
    updatedAt: 2023-12-07T13:22:15.191Z
  },
  timeListened: 2024-06-04T19:23:45.664Z,
  __v: 0
}



// or populate just the artist name
await RecentlyListened.findOne({}).populate('album', 'artistName')
{
  _id: new ObjectId("665f69c1150dc898bdd44267"),
  user: new ObjectId("64d6a966a15d2e18ab96a263"),
  artistName: 'Luke Bryan',
  album: {
    _id: new ObjectId("64d2ae2308a725b72bd5c0db"),
    artistName: 'Luke Bryan'
  },
  timeListened: 2024-06-04T19:23:45.664Z,
  __v: 0
}

Comment on lines +25 to +29
// to add an index on user and timeListened for better query performance
// This is specifying the fields on which the index is created. In this case, it's a compound index on the user field in ascending order (1), and the timeListened field in descending order (-1).
// Indexing on user is useful for quickly retrieving records for a specific user.
// Indexing on timeListened in descending order is often used when you want to retrieve records in a time-based order, such as fetching the most recently listened albums.
//Indexes help MongoDB efficiently retrieve and filter data. In this case, the compound index can speed up queries that involve filtering by both user and sorting by timeListened, which is common when retrieving recently listened items for a particular user.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤩

Comment on lines +7 to +10
// Fetch recently listened albums for the user, sorted by time in descending order
const recentlyListened = await RecentlyListened.find({ user: userId })
.sort({ timeListened: -1 })
.limit(10); // set the limit to 10 albums

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good; but I do wonder if the frontend/caller will want more than just the user id + album id, maybe would be good to populate some additional fields (ie: user name, album name, album artist) as part of the response.

Currently response looks like:

[
  {
    "_id": "665f6d32eabaab4cebdaff45",
    "user": "64d6a966a15d2e18ab96a263",
    "album": "64d2ae2308a725b72bd5c0cf",
    "timeListened": "2024-06-04T19:38:26.917Z",
    "__v": 0
  },
  {
    "_id": "665f6c95980c13e83a4b2a7a",
    "user": "64d6a966a15d2e18ab96a263",
    "album": "64d2ae2308a725b72bd5c0cf",
    "timeListened": "2024-06-04T19:35:49.005Z",
    "__v": 0
  },
  {
    "_id": "665f6c55e919e2b68791e6a0",
    "user": "64d6a966a15d2e18ab96a263",
    "album": "64d2ae2308a725b72bd5c0cf",
    "timeListened": "2024-06-04T19:34:45.344Z",
    "__v": 0
  },
  {
    "_id": "665f6c4b1d5446711e80f8ff",
    "user": "64d6a966a15d2e18ab96a263",
    "album": "64d2ae2308a725b72bd5c0cf",
    "timeListened": "2024-06-04T19:34:35.985Z",
    "__v": 0
  },
  {
    "_id": "665f6bbf80e3baf26b58c508",
    "user": "64d6a966a15d2e18ab96a263",
    "album": "64d2ae2308a725b72bd5c0cf",
    "timeListened": "2024-06-04T19:32:15.571Z",
    "__v": 0
  },
  {
    "_id": "665f69c1150dc898bdd44267",
    "user": "64d6a966a15d2e18ab96a263",
    "album": "64d2ae2308a725b72bd5c0db",
    "timeListened": "2024-06-04T19:23:45.664Z",
    "__v": 0
  }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants