Microsoft Word Document5
LearningOutcome
Bytheendofthisexercise,youwillfurtherdevelopyourskills
inbuildingAPIs,andbuildingonyourunderstandingofREST.
Prerequisites
YouwillbebuildingonwhatyouhadfromExercise5,
continuingtouseSequelizetoprogrammaticallyinteractwith
yourdatabase.
IfyouhaventdoneExercise5,pleaseseetheprerequisites
fromthatexercise.
GettingStarted
YouwillbebuildingontopofwhatyouhadfromExercise5.
JustlikeinExercise5,youllneedtoinstalltheSequelize
package.YoucanfindthatinformationontheGettingStarted
sectionofExercise5.
Task
Inyourpreviousassignment,youupdatedyourGETrequests
tointeractwithyourdatabaseviaSequelize,butyoudidnt
updatethePOSTs.WellexpandonthistogetyourMusicApp
tobefullyoperationalagain,exceptwellbechangingtheAPIs
upandaddingafewnewones.
InExercise3whenyoubuiltyourPOST/api/playlistsAPI,
ittookcareofanyUpdateinyourapplication(addinganew
playlist,oraddingsongstoaplaylist).Thatwasnotaverygood
wayofbuildingAPIs(remember,Imteachingyouthebad
waysofdoingthingsandsoyouunderstandwhythegood
waysaregood!).Itwasntgoodbecause:
Theysendfartoomuchdataforeverychange(whysend
alltheplaylistdatawhenyoucouldjustsendwhichsong
youreaddingtowhichplaylist?).
TheyarenotRESTful.
Withthefollowingtasks,wellmakeourAPIrequestsmore
efficient,moreRESTful,andfunctionalusingtheORM.
AddingSongstoPlaylists
Inexercise3,youpersistedplaylistdatatodiskbysendingall
ofthePlaylistdataviaaPOSTto/api/playlists.Insteadwe
willcreateaAPIsspecificallyforaddingsongstoplaylists.
ThefollowingtwoAPIrequestsdescribedbothsolvethisina
betterway.Youcanimplement*eitherone*(bothdescribed
justtohelpwithyourunderstandingofPUTvsPOST).
PUT/API/PLAYLISTS/:ID
PUTisusedwhenyouwanttoupdateanexistingresource.But
whenusingaPUT,youshouldalsobeprovidingallthe
informationnecessary,becauseitwillreplacetheentire
resource.RememberPUTsshouldbeidempotent,meaningif
youkeepissuingaPUTrequestrepeatedly,onlythefirst
requestshouldactuallychangeanystateinyourdatabase.
ThebodyofthePUTrequestshouldbethe_entire_listofsongs
(notjusttheonebeingadded).Forexample,ifyouwereadding
song64toaplaylistwithsongs8,16,and32,yourrequest
wouldlooklikethis:
PUT/api/playlists/4
{
name:SomeAwesomePlaylist,
songs:[8,16,32,64]
}
TheresultisthatyourPlaylist#4shouldcontainsongs8,16,
32and64.DontworryaboutdealingwithinvalidPOSTbody
dataforthisexercise.Theresponseshouldbea200.
Ontheserveryoudeitherdeleteallofthesongsintheplaylist
andthenaddthenewones,oryouddoadifftoseewhat
changedandperformthenecessaryoperationstoadd/remove
basedonthatdiff.Howyouimplementthisisuptoyou.
Butthisisalsonotveryefficientinourcase,becausewerenot
doinganybulkoperations(itcouldbeusefulifyourUIdidnt
persistsongsuntilyoupressSavethough!).
SohereisanotherAPIthatfitsourusecaseabitbetter:
POST/API/PLAYLISTS/:ID
ButyoumightsayWait,IthoughtweshouldonlyusePOSTfor
create?.WhileitstruethatPOSTistheonlyHTTPmethodyou
shouldusewhencreatingaresource,itdoesntmeanyoucant
useitforupdates.Itsalsousedwhenyouwanttomakean
updatethatisnon-idempotent,whichwearedoinginthiscase
(asdoingthefollowingPOSTwouldupdatethestateofthe
serverbycontinuouslyaddingthissongoverandover).
Anexamplerequesttoaddsong64toaplaylistwith8,16,and
32wouldlooklike:
POST/api/playlists/4
{
song:64
}
Thisshouldaddsong64toplaylist4.Dontworryabout
dealingwithinvalidPOSTbodydataforthisexercise.The
responseshouldbea200.
UPDATINGAPPLOGIC
YoushouldalsoupdateyourMusicAppJSlogictocalloneof
thesetwomethods(insteadofusingPOST/api/playlists
whichwasthecasefromExercise3).Whichoneyouchooseis
uptoyou,andnoitdoesntmatterforyourmarks.
CreatingnewPlaylists
Inexercise3,youpersistednewPlayliststhroughthesameAPI
asaddingsongs(POST/api/playlists).Wellkeep
/api/playlists,butthistimeitllstrictlybeusedtocreate
newplaylists.
POST/API/PLAYLISTS
Whencreatinganewplaylist,itshouldacceptaname
parameterinthebodylikeso:
POST/api/playlists
{
name:Secondgreatestplaylist
}
DontworryabouthandlinginvalidPOSTbodydatainthe
request.Theresponseshouldcontaina200,andtheidand
nameofthenewplaylist:
{
id:101
name:Sendgreatestplaylist
}
UPDATE.ItsalsofineiftheresponsecontainsaJSONresponse
containinganemptyarrayofsongs:
{
id:101
name:Sendgreatestplaylist,
songs:[]
}
UPDATINGAPPLOGIC
Whenyougetaresponsebackfromtheserver,addthenew
playlisttoyourMUSIC_DATAin-memorystateobject,and
updateyourUIwiththenewplaylist.
(interestingtidbit:youcouldaddittotheUIrightaway,doing
sowouldmeanbuildingaOptimisticUI]),butyoualsohaveto
dealwithwhathappensiftheserveractuallyendsuprejecting
yourrequest).
Removingasongfromaplaylist
DELETE/API/PLAYLISTS/:ID/
ThebodyoftheDELETErequestshouldcontainthesongid
thatyouwanttoremovefromaparticularplaylist.For
example:
DELETE/api/playlists/:id
{
song:64
}
GenerallyDELETErequestsshouldbeidempotent,butsince
ourapplicationstatedoesntknowabouttheidsofthe
relationships(andIdontthinkitshould),wellmakean
exceptionhere(RESTisnotaperfectscience????).
UPDATINGAPPLOGIC
ThisisnewfunctionalityyoulladdtoyourUIyoullbeadding
aRemovebuttononthesongslistedwhenlookingatthe
songsintheplaylist.InyourUIforthatpage,addanxbutton
totherightofthe+button(usethesamexyouusedinthe
modaldismissal).
Whenclickingthex,thesongshouldbea)removedfrom
memory,b)removedintheUI,andc)removefromtheDBviaa
calltoyourDELETEAPI.
Requirements
*One*ofPUT/api/playlists/:idorPOST
/api/playlists/:id,whichupdatesthePlaylistatid
withthecontentsoftherequestbody.(2mark)
AddsongstoplaylistinyourUIusesoneoftheabove
APIcalls.(1mark)
POST/api/playlistscreatesplaylistsandreturnsthe
playlistid.(2marks)
CreatePlaylistinyourUIusesyourupdatedPOST
/api/playlists.(1mark)
DELETE/api/playlists/:iddeletesthesongfrom
playlistidspecifiedinthebody.(2marks)
AddDeletesongfromplaylistfunctionalityinUI.(1
mark)
DeletesongfromplaylistinyourUIusesyourDELETE
APItopersistchanges.(1mark)
Reviews
There are no reviews yet.