Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Implement Work.findByIswc
Browse files Browse the repository at this point in the history
  • Loading branch information
ocharles committed Mar 4, 2013
1 parent b5e1f06 commit c5a866c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/MusicBrainz/Data/Work.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ should see the documentation on the 'Work' type and notice all the type class
instances. -}
module MusicBrainz.Data.Work
( findByArtist
, findByIswc

-- * ISWCs
, findIswcs
Expand Down Expand Up @@ -234,3 +235,26 @@ findByArtist artistId = query q (artistId, artistId)
ORDER BY
musicbrainz_collate(name.name)
|]


--------------------------------------------------------------------------------
findByIswc :: (Functor m, MonadIO m) => ISWC -> MusicBrainzT m [CoreEntity Work]
findByIswc iswc = query q (Only iswc)
where
q = [sql|
SELECT work_id, revision_id,
name.name, comment, work_type_id, language_id
FROM (
SELECT DISTINCT work_id, work_revision.revision_id, work_tree_id
FROM work
JOIN work_revision USING (work_id)
JOIN iswc USING (work_tree_id)
WHERE iswc.iswc = ?
AND work_revision.revision_id = work.master_revision_id
) q
JOIN work_tree USING (work_tree_id)
JOIN work_data USING (work_data_id)
JOIN work_name name ON (work_data.name = name.id)
ORDER BY
musicbrainz_collate(name.name)
|]
14 changes: 14 additions & 0 deletions test/suite/MusicBrainz/Data/Work/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tests = [ testCreateFindLatest
, testResolveRevisionReference
, testFindIswcs
, testFindByArtist
, testFindByIswc
]


Expand Down Expand Up @@ -114,6 +115,19 @@ testIswc = testCase "Can add and remove ISWCs" $
withIswc x = x { workIswcs = Set.singleton expected }


--------------------------------------------------------------------------------
testFindByIswc :: Test
testFindByIswc = testCase "Can find works by ISWCs" $ do
editor <- entityRef <$> register acid2
createEdit $ do
work <- viewRevision =<< create editor (withIswc honeysuckleRose)
actual <- findByIswc expected
actual @?= [ work ]
where
expected = "T-070.116.442-2" ^?! iswc
withIswc x = x { workIswcs = Set.singleton expected }


--------------------------------------------------------------------------------
testResolveRevisionReference :: Test
testResolveRevisionReference = testCase "Resolve revision reference" $ do
Expand Down

0 comments on commit c5a866c

Please sign in to comment.