package Ġcom . int workers . application . controller . school system Ċ Ċ import Ġcom . int workers . application . exception . Resource NotFoundException Ċ import Ġcom . int workers . application . model . school system . Visit Ċ import Ġcom . int workers . application . service . school system . School Admin Service Ċ import Ġcom . int workers . application . service . school system . Social Worker Service Ċ import Ġcom . int workers . application . service . school system . Visit Service Ċ import Ġcom . int workers . application . service . user . UserService Ċ import Ġio . swagger . annotations . Api Implicit Param Ċ import Ġio . swagger . annotations . Api Implicit Params Ċ import Ġio . swagger . annotations . ApiOperation Ċ import Ġio . swagger . annotations . Api Param Ċ import Ġorg . springframework . beans . factory . annotation . Autowired Ċ import Ġorg . springframework . http . HttpStatus Ċ import Ġorg . springframework . http . Response Entity Ċ import Ġorg . springframework . security . access . pre post . Pre Authorize Ċ import Ġorg . springframework . security . core . Authentication Ċ import Ġorg . springframework . security . core . user details . UserDetails Ċ import Ġorg . springframework . web . bind . annotation .* Ċ import Ġjavax . validation . Valid Ċ Ċ @ RestController Ċ @ RequestMapping ("/ vis its ") Ċ class ĠVisit Controller Ġ{ ĊĊĠĠĠ Ġ@ Autowired ĊĠĠĠ Ġprivate Ġlateinit Ġvar ĠuserService : ĠUserService ĊĊĠĠĠ Ġ@ Autowired ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġschool Admin Service : ĠSchool Admin Service ĊĊĠĠĠ Ġ@ Autowired ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġvisit Service : ĠVisit Service ĊĊĠĠĠ Ġ@ Autowired ĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġsocial Worker Service : ĠSocial Worker Service ĊĊĠĠĠ Ġ@ ApiOperation ( value Ġ= Ġ" Find Ġa ĠVisit Ġby ĠId ", Ġresponse Ġ= ĠVisit :: class ) ĊĠĠĠ Ġ@ GetMapping ( value Ġ= Ġ["/ visit /{ id } "], Ġproduces Ġ= Ġ[" application / json "]) ĊĠĠĠ Ġfun Ġfind ById (@ Api Param ( value Ġ= Ġ" Visit ĠId ", Ġrequired Ġ= Ġtrue , Ġexample Ġ= Ġ" 1 ") ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ@ PathVariable Ġid : ĠLong ): ĠResponseEntity < *> Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn ĠResponseEntity ( visit Service . findById ( id ), ĠHttpStatus . OK ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ ApiOperation ( value Ġ= Ġ" Find Ġall ĠVis its ", Ġresponse Ġ= ĠVisit :: class , Ġresponse Container Ġ= Ġ" List ") ĊĠĠĠ Ġ@ Api Implicit Params ( ĊĠĠĠĠĠĠĠĠĠĠĠ ĠApi Implicit Param ( name Ġ= Ġ" page ", ĠdataType Ġ= Ġ" integer ", Ġparam Type Ġ= Ġ" query ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġvalue Ġ= Ġ" Results Ġpage Ġyou Ġwant Ġto Ġretrieve Ġ( 0 .. N )"), ĊĠĠĠĠĠĠĠĠĠĠĠ ĠApi Implicit Param ( name Ġ= Ġ" size ", ĠdataType Ġ= Ġ" integer ", Ġparam Type Ġ= Ġ" query ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġvalue Ġ= Ġ" Number Ġof Ġrecords Ġper Ġpage ."), ĊĠĠĠĠĠĠĠĠĠĠĠ ĠApi Implicit Param ( name Ġ= Ġ" sort ", Ġallow Multiple Ġ= Ġtrue , ĠdataType Ġ= Ġ" string ", Ġparam Type Ġ= Ġ" query ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġvalue Ġ= Ġ" Sorting Ġcriteria Ġin Ġthe Ġformat : Ġproperty ( , asc | desc ). Ġ" Ġ+ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" Default Ġsort Ġorder Ġis Ġascending . Ġ" Ġ+ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" Multiple Ġsort Ġcriteria Ġare Ġsupported .")) ĊĠĠĠ Ġ@ Pre Authorize (" has Authority (' ROLE _ ADMIN ') ") ĊĠĠĠ Ġ@ GetMapping ( value Ġ= Ġ["/ all "], Ġproduces Ġ= Ġ[" application / json "]) ĊĠĠĠ Ġfun ĠfindAll (): ĠResponseEntity < *> Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn ĠResponseEntity ( visit Service . findAll (), ĠHttpStatus . OK ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ ApiOperation ( value Ġ= Ġ" Find Ġall ĠVis its Ġthat Ġbelong Ġto Ġcurrent ĠSocial ĠWorker ", ĊĠĠĠĠĠĠĠĠĠĠĠ Ġresponse Ġ= ĠVisit :: class , Ġresponse Container Ġ= Ġ" List ") ĊĠĠĠ Ġ@ Api Implicit Params ( ĊĠĠĠĠĠĠĠĠĠĠĠ ĠApi Implicit Param ( name Ġ= Ġ" page ", ĠdataType Ġ= Ġ" integer ", Ġparam Type Ġ= Ġ" query ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġvalue Ġ= Ġ" Results Ġpage Ġyou Ġwant Ġto Ġretrieve Ġ( 0 .. N )"), ĊĠĠĠĠĠĠĠĠĠĠĠ ĠApi Implicit Param ( name Ġ= Ġ" size ", ĠdataType Ġ= Ġ" integer ", Ġparam Type Ġ= Ġ" query ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġvalue Ġ= Ġ" Number Ġof Ġrecords Ġper Ġpage ."), ĊĠĠĠĠĠĠĠĠĠĠĠ ĠApi Implicit Param ( name Ġ= Ġ" sort ", Ġallow Multiple Ġ= Ġtrue , ĠdataType Ġ= Ġ" string ", Ġparam Type Ġ= Ġ" query ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġvalue Ġ= Ġ" Sorting Ġcriteria Ġin Ġthe Ġformat : Ġproperty ( , asc | desc ). Ġ" Ġ+ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" Default Ġsort Ġorder Ġis Ġascending . Ġ" Ġ+ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" Multiple Ġsort Ġcriteria Ġare Ġsupported .")) ĊĠĠĠ Ġ@ Pre Authorize (" has Authority (' ROLE _ SO CIAL WORK ER ') ") ĊĠĠĠ Ġ@ GetMapping ( value Ġ= Ġ[" social worker / all "], Ġproduces Ġ= Ġ[" application / json "]) ĊĠĠĠ Ġfun ĠfindAll Worker Vis its ( authentication : ĠAuthentication ): ĠResponseEntity < Any > Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġusername Ġ= Ġ( authentication . principal Ġas ĠUser Details ). username ĊĠĠĠĠĠĠĠ Ġval Ġuser Ġ= ĠuserService . findBy Username ( username ) ĊĠĠĠĠĠĠĠ Ġval Ġsocial Worker Ġ= Ġsocial Worker Service . findById ( user . userId ) ĊĠĠĠĠĠĠĠ Ġval Ġall Vis its Ġ= Ġvisit Service . findAll () ĊĠĠĠĠĠĠĠ Ġval Ġmy Vis its Ġ= Ġmutable ListOf < Visit >() ĊĠĠĠĠĠĠĠ Ġfor Ġ( visit Ġin Ġall Vis its ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif Ġ( visit . worker Id Ġ== Ġsocial Worker . worker id ) Ġmy Vis its . add ( visit ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġreturn ĠResponseEntity ( my Vis its , ĠHttpStatus . OK ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ ApiOperation ( value Ġ= Ġ" Find Ġall ĠVis its Ġthat Ġbelong Ġto Ġcurrent ĠSchool ĠAdmin 's Ġschool ", ĊĠĠĠĠĠĠĠĠĠĠĠ Ġresponse Ġ= ĠVisit :: class , Ġresponse Container Ġ= Ġ" List ") ĊĠĠĠ Ġ@ Api Implicit Params ( ĊĠĠĠĠĠĠĠĠĠĠĠ ĠApi Implicit Param ( name Ġ= Ġ" page ", ĠdataType Ġ= Ġ" integer ", Ġparam Type Ġ= Ġ" query ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġvalue Ġ= Ġ" Results Ġpage Ġyou Ġwant Ġto Ġretrieve Ġ( 0 .. N )"), ĊĠĠĠĠĠĠĠĠĠĠĠ ĠApi Implicit Param ( name Ġ= Ġ" size ", ĠdataType Ġ= Ġ" integer ", Ġparam Type Ġ= Ġ" query ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġvalue Ġ= Ġ" Number Ġof Ġrecords Ġper Ġpage ."), ĊĠĠĠĠĠĠĠĠĠĠĠ ĠApi Implicit Param ( name Ġ= Ġ" sort ", Ġallow Multiple Ġ= Ġtrue , ĠdataType Ġ= Ġ" string ", Ġparam Type Ġ= Ġ" query ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġvalue Ġ= Ġ" Sorting Ġcriteria Ġin Ġthe Ġformat : Ġproperty ( , asc | desc ). Ġ" Ġ+ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" Default Ġsort Ġorder Ġis Ġascending . Ġ" Ġ+ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" Multiple Ġsort Ġcriteria Ġare Ġsupported .")) ĊĠĠĠ Ġ@ Pre Authorize (" has Authority (' ROLE _ S CHO OL ADMIN ') ") ĊĠĠĠ Ġ@ GetMapping ( value Ġ= Ġ[" school Admin / all "], Ġproduces Ġ= Ġ[" application / json "]) ĊĠĠĠ Ġfun ĠfindAll Admin Vis its ( authentication : ĠAuthentication ): ĠResponseEntity < Any > Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġusername Ġ= Ġ( authentication . principal Ġas ĠUser Details ). username ĊĠĠĠĠĠĠĠ Ġval Ġuser Ġ= ĠuserService . findBy Username ( username ) ĊĠĠĠĠĠĠĠ Ġval Ġschool Admin Ġ= Ġschool Admin Service . findById ( user . userId ) ĊĠĠĠĠĠĠĠ Ġval Ġall Vis its Ġ= Ġvisit Service . findAll () ĊĠĠĠĠĠĠĠ Ġval Ġmy Vis its Ġ= Ġmutable ListOf < Visit >() ĊĠĠĠĠĠĠĠ Ġfor Ġ( visit Ġin Ġall Vis its ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġif Ġ( visit . school Id Ġ== Ġschool Admin . school ?. school Id ) Ġmy Vis its . add ( visit ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġreturn ĠResponseEntity ( my Vis its , ĠHttpStatus . OK ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ ApiOperation ( value Ġ= Ġ" Create Ġa ĠVisit Ġat Ġcurrent ĠSchool ĠAdmin 's Ġschool ", ĊĠĠĠĠĠĠĠĠĠĠĠ Ġresponse Ġ= ĠVisit :: class ) ĊĠĠĠ Ġ@ Pre Authorize (" has Authority (' ROLE _ S CHO OL ADMIN ') ") ĊĠĠĠ Ġ@ PostMapping ( value Ġ= Ġ["/ new /{ worker Id } "], ĊĠĠĠĠĠĠĠĠĠĠĠ Ġconsume s Ġ= Ġ[" application / json "], Ġproduces Ġ= Ġ[" application / json "]) ĊĠĠĠ Ġfun Ġvisit School (@ Api Param ( value Ġ= Ġ" Visit Ġto Ġbe Ġcreated ", Ġrequired Ġ= Ġtrue ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ@ Valid Ġ@ RequestBody Ġvisit : ĠVisit , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ@ Api Param ( value Ġ= Ġ" Work ed ĠId ", Ġrequired Ġ= Ġtrue , Ġexample Ġ= Ġ" 1 ") ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ@ PathVariable Ġworker Id : ĠLong , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġauthentication : ĠAuthentication ): ĠResponseEntity < *> Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġusername Ġ= Ġ( authentication . principal Ġas ĠUser Details ). username ĊĠĠĠĠĠĠĠ Ġval Ġuser Ġ= ĠuserService . findBy Username ( username ) ĊĠĠĠĠĠĠĠ Ġval Ġschool Admin Ġ= Ġschool Admin Service . findById ( user . userId ) ĊĠĠĠĠĠĠĠ Ġvisit . worker Ġ= Ġsocial Worker Service . findById ( worker Id ) ĊĠĠĠĠĠĠĠ Ġvisit . school Ġ= Ġschool Admin . school ĊĠĠĠĠĠĠĠ Ġval Ġsaved Visit Ġ= Ġvisit Service . save ( visit ) ĊĠĠĠĠĠĠĠ Ġreturn ĠResponseEntity ( visit Service . findById ( saved Visit . visit Id ), ĠHttpStatus . CREATED ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ ApiOperation ( value Ġ= Ġ" Update Ġa ĠVisit Ġat Ġcurrent ĠSchool ĠAdmin 's Ġschool ", ĊĠĠĠĠĠĠĠĠĠĠĠ Ġresponse Ġ= ĠVisit :: class ) ĊĠĠĠ Ġ@ Pre Authorize (" has Authority (' ROLE _ S CHO OL ADMIN ') ") ĊĠĠĠ Ġ@ Put Mapping ( value Ġ= Ġ["/ update /{ visit Id } "], Ġconsume s Ġ= Ġ[" application / json "], ĊĠĠĠĠĠĠĠĠĠĠĠ Ġproduces Ġ= Ġ[" application / json "]) ĊĠĠĠ Ġfun Ġupdate Visit (@ Api Param ( value Ġ= Ġ" Visit Ġinfo Ġto Ġbe Ġupdated ", Ġrequired Ġ= Ġtrue ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ@ Valid Ġ@ RequestBody Ġvisit : ĠVisit , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ@ Api Param ( value Ġ= Ġ" Visit ĠId ", Ġrequired Ġ= Ġtrue , Ġexample Ġ= Ġ" 1 ") ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ@ PathVariable Ġvisit Id : ĠLong , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġauthentication : ĠAuthentication ): ĠResponseEntity < *> Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġusername Ġ= Ġ( authentication . principal Ġas ĠUser Details ). username ĊĠĠĠĠĠĠĠ Ġval Ġuser Ġ= ĠuserService . findBy Username ( username ) ĊĠĠĠĠĠĠĠ Ġval Ġschool Admin Ġ= Ġschool Admin Service . findById ( user . userId ) ĊĠĠĠĠĠĠĠ Ġvar Ġupdated Visit Ġ= Ġvisit Service . findById ( visit Id ) ĊĠĠĠĠĠĠĠ Ġif Ġ( updated Visit . school Id Ġ!= Ġschool Admin . school ?. school Id ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthrow ĠResource NotFoundException (" Visit Ġdoes Ġnot Ġbelong Ġto ĠSchool ĠAdmin 's Ġschool ") ĊĠĠĠĠĠĠĠ Ġreturn ĠResponseEntity ( visit Service . update ( visit , Ġvisit Id ), ĠHttpStatus . OK ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ ApiOperation ( value Ġ= Ġ" Delete Ġa ĠVisit Ġat Ġcurrent ĠSchool ĠAdmin 's ĠSchool ", Ġresponse Ġ= ĠVoid :: class ) ĊĠĠĠ Ġ@ Pre Authorize (" has Authority (' ROLE _ S CHO OL ADMIN ') ") ĊĠĠĠ Ġ@ Delete Mapping ( value Ġ= Ġ["/ delete /{ visit Id }" ]) ĊĠĠĠ Ġfun Ġdelete Visit (@ Api Param ( value Ġ= Ġ" Visit ĠId ", Ġrequired Ġ= Ġtrue , Ġexample Ġ= Ġ" 1 ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ@ PathVariable Ġvisit Id : ĠLong , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġauthentication : ĠAuthentication ): ĠResponseEntity < Any > Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġusername Ġ= Ġ( authentication . principal Ġas ĠUser Details ). username ĊĠĠĠĠĠĠĠ Ġval Ġuser Ġ= ĠuserService . findBy Username ( username ) ĊĠĠĠĠĠĠĠ Ġval Ġschool Admin Ġ= Ġschool Admin Service . findById ( user . userId ) ĊĠĠĠĠĠĠĠ Ġvar Ġvisit Ġ= Ġvisit Service . findById ( visit Id ) ĊĠĠĠĠĠĠĠ Ġif Ġ( visit . school Id Ġ!= Ġschool Admin . school ?. school Id ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthrow ĠResource NotFoundException (" Visit Ġdoes Ġnot Ġbelong Ġto ĠSchool ĠAdmin 's Ġschool ") ĊĠĠĠĠĠĠĠ Ġvisit Service . delete ( visit Id ) ĊĠĠĠĠĠĠĠ Ġreturn ĠResponseEntity ( HttpStatus . OK ) ĊĠĠĠ Ġ} Ċ Ċ }