Skip to content

Commit

Permalink
Implement playlist updates with sql
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Aug 27, 2024
1 parent e988881 commit 9d1f40f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/controllers/playlists.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ async function updatePlaylist(req) {
throw new PlaylistNotFoundError({ id });
}

await playlists.updatePlaylist(playlist, patch);
const updatedPlaylist = await playlists.updatePlaylist(playlist, patch);

return toItemResponse(
serializePlaylist(playlist),
serializePlaylist(updatedPlaylist),
{ url: req.fullUrl },
);
}
Expand Down Expand Up @@ -173,10 +173,10 @@ async function renamePlaylist(req) {
throw new PlaylistNotFoundError({ id });
}

await playlists.updatePlaylist(playlist, { name });
const updatedPlaylist = await playlists.updatePlaylist(playlist, { name });

return toItemResponse(
serializePlaylist(playlist),
serializePlaylist(updatedPlaylist),
{ url: req.fullUrl },
);
}
Expand Down
23 changes: 17 additions & 6 deletions src/plugins/playlists.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,25 @@ class PlaylistsRepository {

/**
* @param {Playlist} playlist
* @param {Partial<Playlist>} patch
* @returns {Promise<Playlist>}
* @param {Partial<Pick<Playlist, 'name'>>} patch
*/
// eslint-disable-next-line class-methods-use-this
async updatePlaylist(playlist, patch = {}) {
Object.assign(playlist, patch);
await playlist.save();
return playlist;
const { db } = this.#uw;

const updatedPlaylist = await db.updateTable('playlists')
.where('id', '=', playlist.id)
.set(patch)
.returning([
'id',
'userID',
'name',
(eb) => jsonLength(eb.ref('items')).as('size'),
'createdAt',
'updatedAt',
])
.executeTakeFirstOrThrow();

return updatedPlaylist;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions test/playlists.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,15 @@ describe('Playlists', () => {

describe('PATCH /playlists/:id', () => {
it('requires authentication', async () => {
const fakeID = '603e43b12d46ab05a8946a23';
const fakeID = 'e2c85d94-344b-4c2a-86bd-95edb939f3e6';

await supertest(uw.server)
.patch(`/api/playlists/${fakeID}`)
.expect(401);
});

it('validates input', async () => {
const fakeID = '603e43b12d46ab05a8946a23';
const fakeID = 'e2c85d94-344b-4c2a-86bd-95edb939f3e6';
const token = await uw.test.createTestSessionToken(user);

await supertest(uw.server)
Expand Down Expand Up @@ -256,15 +256,15 @@ describe('Playlists', () => {

describe('PUT /playlists/:id/rename', () => {
it('requires authentication', async () => {
const fakeID = '603e43b12d46ab05a8946a23';
const fakeID = 'e2c85d94-344b-4c2a-86bd-95edb939f3e6';

await supertest(uw.server)
.put(`/api/playlists/${fakeID}/rename`)
.expect(401);
});

it('validates input', async () => {
const fakeID = '603e43b12d46ab05a8946a23';
const fakeID = 'e2c85d94-344b-4c2a-86bd-95edb939f3e6';
const token = await uw.test.createTestSessionToken(user);

await supertest(uw.server)
Expand Down Expand Up @@ -326,7 +326,7 @@ describe('Playlists', () => {

describe('PUT /playlists/:id/activate', () => {
it('requires authentication', async () => {
const fakeID = '603e43b12d46ab05a8946a23';
const fakeID = 'e2c85d94-344b-4c2a-86bd-95edb939f3e6';

await supertest(uw.server)
.put(`/api/playlists/${fakeID}/activate`)
Expand Down

0 comments on commit 9d1f40f

Please sign in to comment.