package Ġde . et ern al w ings . v ima . controller Ċ Ċ import Ġde . et ern al w ings . v ima . domain . Playlist Ċ import Ġde . et ern al w ings . v ima . process . Coll age Creator Ċ import Ġde . et ern al w ings . v ima . repository . Playlist Repository Ċ import Ġde . et ern al w ings . v ima . repository . Video Repository Ċ import Ġorg . springframework . http . MediaType Ċ import Ġorg . springframework . http . Response Entity Ċ import Ġorg . springframework . transaction . annotation . Transactional Ċ import Ġorg . springframework . web . bind . annotation . Delete Mapping Ċ import Ġorg . springframework . web . bind . annotation . GetMapping Ċ import Ġorg . springframework . web . bind . annotation . PathVariable Ċ import Ġorg . springframework . web . bind . annotation . PostMapping Ċ import Ġorg . springframework . web . bind . annotation . Put Mapping Ċ import Ġorg . springframework . web . bind . annotation . RequestBody Ċ import Ġorg . springframework . web . bind . annotation . RequestMapping Ċ import Ġorg . springframework . web . bind . annotation . RequestParam Ċ import Ġorg . springframework . web . bind . annotation . RestController Ċ import Ġjavax . persistence . Entity NotFoundException Ċ Ċ data Ġclass ĠPlaylist Create Information ( val Ġname : ĠString , Ġval Ġvideo Ids : ĠList < Int >) Ċ Ċ data Ġclass ĠPlaylist Information ( val Ġid : ĠInt , Ġval Ġname : ĠString , Ġval Ġvideo Ids : ĠList < Int >) Ġ{ ĊĠĠĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠĠĠĠĠ Ġfun Ġfrom ( playlist : ĠPlaylist ): ĠPlaylist Information Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġvideo Ids Ġ= ĠArrayList ( playlist . videos ). sorted By Ġ{ Ġit . position Ġ}. map NotNull Ġ{ Ġit . video ?. id Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn ĠPlaylist Information ( playlist . id !! , Ġplaylist . name !! , Ġvideo Ids ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ } Ċ Ċ @ RestController Ċ @ RequestMapping ("/ api / play lists ") Ċ class ĠPlaylist Controller ( ĊĠĠĠ Ġprivate Ġval Ġplaylist Repository : ĠPlaylist Repository , ĊĠĠĠ Ġprivate Ġval Ġvideo Repository : ĠVideo Repository , ĊĠĠĠ Ġprivate Ġval Ġcoll age Creator : ĠColl age Creator Ċ ) Ġ{ ĊĊĠĠĠ Ġ@ Transactional ( readOnly Ġ= Ġtrue ) ĊĠĠĠ Ġ@ GetMapping ĊĠĠĠ Ġfun ĠgetAll (): ĠList < Playlist Information > Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġplaylist Repository . findAll (). map Ġ{ ĠPlaylist Information . from ( it ) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Transactional ĊĠĠĠ Ġ@ PostMapping ĊĠĠĠ Ġfun Ġcreate Playlist (@ RequestBody Ġplaylist : ĠPlaylist Create Information ): ĠPlaylist Information Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġnew Playlist Ġ= ĠPlaylist ( playlist . name ) ĊĠĠĠĠĠĠĠ Ġnew Playlist . add Videos ( video Repository . findAll ById ( playlist . video Ids )) ĊĠĠĠĠĠĠĠ Ġreturn ĠPlaylist Information . from ( playlist Repository . save ( new Playlist )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Transactional ĊĠĠĠ Ġ@ Put Mapping ("/ { id }") ĊĠĠĠ Ġfun Ġupdate Playlist Name (@ PathVariable (" id ") Ġplaylist Id : ĠInt , Ġ@ RequestBody ĠnewName : ĠString ): ĠPlaylist Information Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġplaylist Ġ= Ġplaylist Repository . getById ( playlist Id ) ĊĠĠĠĠĠĠĠ Ġplaylist . name Ġ= ĠnewName ĊĠĠĠĠĠĠĠ Ġval Ġsaved Ġ= Ġplaylist Repository . save ( playlist ) ĊĠĠĠĠĠĠĠ Ġreturn ĠPlaylist Information . from ( saved ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Transactional ĊĠĠĠ Ġ@ Put Mapping ("/ { id }/ add ") ĊĠĠĠ Ġfun Ġadd Video To Playlist (@ PathVariable (" id ") Ġplaylist Id : ĠInt , Ġ@ RequestBody Ġvideos : ĠList < Int >): ĠPlaylist Information Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġplaylist Ġ= Ġplaylist Repository . findById ( playlist Id ). orElse Throw Ġ{ ĠEntity NotFoundException () Ġ} ĊĠĠĠĠĠĠĠ Ġval Ġvideos ToAdd Ġ= Ġvideos . filter Ġ{ Ġplaylist . videos . none Ġ{ Ġvideo Ġ-> Ġvideo . video ?. id Ġ== Ġit Ġ} Ġ} ĊĠĠĠĠĠĠĠ Ġval Ġlowest Position Before Ġ= Ġplaylist Repository . get Low est Position Index For ( playlist . id !! ) Ġ?: Ġ 0 ĊĠĠĠĠĠĠĠ Ġplaylist . add Videos ( video Repository . findAll ById ( videos ToAdd ), Ġlowest Position Before Ġ+ Ġ 1 ) ĊĠĠĠĠĠĠĠ Ġreturn ĠPlaylist Information . from ( playlist Repository . save ( playlist )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Transactional ĊĠĠĠ Ġ@ Put Mapping ("/ { id }/ remove ") ĊĠĠĠ Ġfun Ġremove Video From Playlist (@ PathVariable (" id ") Ġplaylist Id : ĠInt , Ġ@ RequestBody Ġvideos : ĠList < Int >): ĠPlaylist Information Ġ{ ĊĠĠĠĠĠĠĠ Ġplaylist Repository . delete From Playlist ( playlist Id , Ġvideos ) ĊĠĠĠĠĠĠĠ Ġreturn ĠPlaylist Information . from ( playlist Repository . getById ( playlist Id )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Delete Mapping ("/ { id }") ĊĠĠĠ Ġfun Ġdelete (@ PathVariable (" id ") Ġplaylist Id : ĠInt ) Ġ{ ĊĠĠĠĠĠĠĠ Ġplaylist Repository . delete ById ( playlist Id ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Transactional ĊĠĠĠ Ġ@ Put Mapping ("/ { id }/ order ") ĊĠĠĠ Ġfun Ġupdate Playlist Order (@ PathVariable (" id ") Ġplaylist Id : ĠInt , Ġ@ RequestBody Ġvideos : ĠList < Int >): ĠPlaylist Information Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġplaylist Ġ= Ġplaylist Repository . findById ( playlist Id ). orElse Throw Ġ{ ĠEntity NotFoundException () Ġ} ĊĠĠĠĠĠĠĠ Ġplaylist . videos . forEach Ġ{ Ġplaylist Position Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠ Ġplaylist Position . position Ġ= Ġvideos . indexOf ( playlist Position . video ?. id ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġreturn ĠPlaylist Information . from ( playlist Repository . save ( playlist )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Transactional ( readOnly Ġ= Ġtrue ) ĊĠĠĠ Ġ@ GetMapping ("/ { id }/ poster ", Ġproduces Ġ= Ġ[ MediaType . IMAGE _ PNG _ VALUE ]) ĊĠĠĠ Ġfun Ġcreate Post er ( ĊĠĠĠĠĠĠĠ Ġ@ PathVariable (" id ") Ġplaylist Id : ĠInt , ĊĠĠĠĠĠĠĠ Ġ@ RequestParam (" width ", ĠdefaultValue Ġ= Ġ" 3 2 0 ") Ġwidth : ĠInt , ĊĠĠĠĠĠĠĠ Ġ@ RequestParam (" height ", ĠdefaultValue Ġ= Ġ" 1 8 0 ") Ġheight : ĠInt ĊĠĠĠ Ġ): ĠResponseEntity < ByteArray > Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġplaylist Ġ= Ġplaylist Repository . getById ( playlist Id ) ĊĠĠĠĠĠĠĠ Ġval Ġvideos Thumb nails Ġ= Ġplaylist . videos . map NotNull Ġ{ Ġv ip Ġ-> Ġv ip . video ?. thumbnail Ġ} ĊĠĠĠĠĠĠĠ Ġif Ġ( videos Thumb nails . isEmpty ()) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn ĠResponseEntity . not Found (). build () ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġval Ġthumb nails ToUse Ġ= Ġwhen Ġ( videos Thumb nails . size ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠ 3 Ġ-> Ġ( videos Thumb nails Ġ+ Ġvideos Thumb nails . random ()). sh uffled () ĊĠĠĠĠĠĠĠĠĠĠĠĠ 2 Ġ-> Ġ( videos Thumb nails Ġ+ Ġvideos Thumb nails ). sh uffled () ĊĠĠĠĠĠĠĠĠĠĠĠĠ 1 Ġ-> Ġ( listOf ( videos Thumb nails [ 0 ], Ġvideos Thumb nails [ 0 ], Ġvideos Thumb nails [ 0 ], Ġvideos Thumb nails [ 0 ])) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġelse Ġ-> Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġrandom Indexes Ġ= Ġvideos Thumb nails . indices . sh uffled (). take ( 4 ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġrandom Indexes . map Ġ{ Ġindex Ġ-> Ġvideos Thumb nails [ index ] Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġval Ġresulting Image Ġ= Ġcoll age Creator . create Coll age Using ( thumb nails ToUse , Ġwidth , Ġheight ) ĊĠĠĠĠĠĠĠ Ġreturn ĠResponseEntity . ok ( result ing Image ) ĊĠĠĠ Ġ} Ċ } Ċ