Skip to content

Commit

Permalink
trpc call works!
Browse files Browse the repository at this point in the history
  • Loading branch information
amendelsohn committed Jan 3, 2024
1 parent 78a2854 commit edf24bc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 37 deletions.
34 changes: 0 additions & 34 deletions packages/trpc-server/src/routers/playlist-router.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { z } from 'zod'
import { publicProcedure, router } from '../trpc'
import { TRPCError } from '@trpc/server'
import { esc } from './search-router'
import { PlaylistRow } from '../db-tables'

export type PlaylistRouteData = {
playlist_id: number
Expand All @@ -19,36 +17,4 @@ export const playlistRouter = router({
}
return row
}),

containTrackId: publicProcedure
.input(
z.object({ trackId: z.number(), collectionType: z.string().optional() })
)
.query(async ({ ctx, input }) => {
const found = await esc.search<PlaylistRow>({
index: 'playlists',
query: {
bool: {
must: [
{ term: { 'playlist_contents.track_ids.track': input.trackId } },
{ term: { is_delete: false } },
{ term: { is_private: false } },
...(input.collectionType === 'album'
? [{ term: { is_album: true } }]
: []),
...(input.collectionType === 'playlist'
? [{ term: { is_album: false } }]
: []),
],
must_not: [],
should: [],
},
},
_source: false,
})

return await ctx.loaders.playlistLoader.loadMany(
found.hits.hits.map((h) => parseInt(h._id))
)
}),
})
41 changes: 40 additions & 1 deletion packages/trpc-server/src/routers/track-router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { z } from 'zod'
import { publicProcedure, router } from '../trpc'
import { TRPCError } from '@trpc/server'
import { esc } from './search-router'

type AlbumBacklinkMetadata = {
playlist_id: number
playlist_name: string
permalink: string
playlist_owner_id: number
}

export const trackRouter = router({
get: publicProcedure.input(z.string()).query(async ({ ctx, input }) => {
Expand All @@ -9,5 +17,36 @@ export const trackRouter = router({
throw new TRPCError({ code: 'NOT_FOUND' })
}
return row
})
}),

getAlbumBacklink: publicProcedure
.input(z.object({ trackId: z.number() }))
.query(async ({ input }) => {
const found = await esc.search<AlbumBacklinkMetadata>({
index: 'playlists',
query: {
bool: {
must: [
{ term: { 'playlist_contents.track_ids.track': input.trackId } },
{ term: { is_delete: false } },
{ term: { is_private: false } },
{ term: { is_album: true } },
],
must_not: [],
should: [],
},
},
size: 1,
sort: [{ created_at: { order: 'desc' } }],
_source: [
'playlist_id',
'playlist_name',
'permalink',
'playlist_owner_id',
],
})

const hits = found.hits.hits.map((h) => h._source)
return hits.length > 0 ? hits[0] : null
}),
})
4 changes: 2 additions & 2 deletions packages/trpc-server/test/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ test('search tracks', async () => {
test('you can query for playlists that contain a track id', async () => {
const caller = await testRouter()

const playlistIds = await caller.playlists.containTrackId({ trackId: 101 })
expect(playlistIds).toEqual(['301'])
const playlistIds = await caller.tracks.getAlbumBacklink({ trackId: 101 })
expect(playlistIds?.playlist_id).toEqual(['301'])
})

0 comments on commit edf24bc

Please sign in to comment.