package Ġcom . github . ca ay 2 0 0 0 . tt k . context . world . stop . application . create Ċ Ċ import Ġarrow . core . Either Ċ import Ġarrow . core . flatMap Ċ import Ġarrow . core . left Ċ import Ġarrow . core . right Ċ import Ġcom . github . ca ay 2 0 0 0 . tt k . context . shared . domain . Stop Id Ċ import Ġcom . github . ca ay 2 0 0 0 . tt k . context . shared . domain . World Id Ċ import Ġcom . github . ca ay 2 0 0 0 . tt k . context . world . stop . domain . Stop Ċ import Ġcom . github . ca ay 2 0 0 0 . tt k . context . world . stop . domain . Stop Repository Ċ import Ġcom . github . ca ay 2 0 0 0 . tt k . context . world . world . domain . World Repository Ċ import Ġcom . github . ca ay 2 0 0 0 . tt k . lib . event bus . event . Event Ċ import Ġcom . github . ca ay 2 0 0 0 . tt k . lib . event bus . event . Event Publisher Ċ Ċ class ĠStop Creator Service ( ĊĠĠĠ Ġprivate Ġval Ġevent Publisher : ĠEvent Publisher < Event >, ĊĠĠĠ Ġprivate Ġval Ġworld Repository : ĠWorld Repository , ĊĠĠĠ Ġprivate Ġval Ġstop Repository : ĠStop Repository Ċ ) Ġ{ ĊĊĠĠĠ Ġfun Ġinvoke ( world Id : ĠWorld Id , Ġstop Id : ĠStop Id , Ġname : ĠString ) Ġ= ĊĠĠĠĠĠĠĠ Ġguard World Exists ( world Id ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. flatMap Ġ{ Ġguard Stop DoesNot Exists ( stop Id ) Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. flatMap Ġ{ ĠStop . create ( world Id , Ġstop Id , Ġname ). right () Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. flatMap Ġ{ Ġstop Ġ-> Ġstop . save () Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. flatMap Ġ{ Ġstop Ġ-> Ġstop . publish Events () Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. map Left Ġ{ Ġerror Ġ-> Ġerror . map Error () Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġguard World Exists ( world Id : ĠWorld Id ): ĠEither < Stop Creator ServiceException , ĠUnit > Ġ= ĊĠĠĠĠĠĠĠ Ġif Ġ( world Repository . exists ( world Id )) ĠUnit . right () ĊĠĠĠĠĠĠĠ Ġelse ĠStop Creator ServiceException . World NotFound ( world Id ). left () ĊĊĠĠĠ Ġprivate Ġfun Ġguard Stop DoesNot Exists ( stop Id : ĠStop Id ): ĠEither < Stop Creator ServiceException , ĠUnit > Ġ= ĊĠĠĠĠĠĠĠ Ġif Ġ( stop Repository . exists ( stop Id )) ĠStop Creator ServiceException . Stop AlreadyExists ( stop Id ). left () ĊĠĠĠĠĠĠĠ Ġelse ĠUnit . right () ĊĊĠĠĠ Ġprivate Ġfun ĠStop . save () Ġ= Ġstop Repository . save ( this ) ĊĊĠĠĠ Ġprivate Ġfun ĠStop . publish Events () Ġ= Ġevent Publisher . publish ( this . pull Events ()). right () Ċ } Ċ