[PS-182] Backend should store play history - #91
Conversation
akosasante
left a comment
There was a problem hiding this comment.
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 ? |
There was a problem hiding this comment.
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
}| // 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. |
| // 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 |
There was a problem hiding this comment.
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
}
]
No description provided.