66import static edu .tamu .framework .enums .BusinessValidationType .EXISTS ;
77import static edu .tamu .framework .enums .BusinessValidationType .NONEXISTS ;
88import static edu .tamu .framework .enums .BusinessValidationType .UPDATE ;
9+ import static edu .tamu .app .enums .AppRole .ROLE_ANONYMOUS ;
10+ import static edu .tamu .app .enums .AppRole .ROLE_USER ;
911
1012import org .springframework .beans .factory .annotation .Autowired ;
1113import org .springframework .messaging .simp .SimpMessagingTemplate ;
1214import org .springframework .web .bind .annotation .RestController ;
1315
16+ import edu .tamu .app .model .AppUser ;
1417import edu .tamu .app .model .Service ;
18+ import edu .tamu .app .model .repo .AppUserRepo ;
1519import edu .tamu .app .model .repo .ServiceRepo ;
1620import edu .tamu .app .service .SystemMonitorService ;
21+ import edu .tamu .framework .aspect .annotation .ApiCredentials ;
1722import edu .tamu .framework .aspect .annotation .ApiMapping ;
1823import edu .tamu .framework .aspect .annotation .ApiValidatedModel ;
1924import edu .tamu .framework .aspect .annotation .ApiValidation ;
2025import edu .tamu .framework .aspect .annotation .ApiVariable ;
2126import edu .tamu .framework .aspect .annotation .Auth ;
2227import edu .tamu .framework .model .ApiResponse ;
28+ import edu .tamu .framework .model .Credentials ;
2329
2430@ RestController
2531@ ApiMapping ("/service" )
@@ -34,6 +40,9 @@ public class ServiceController {
3440 @ Autowired
3541 private SystemMonitorService systemMonitorService ;
3642
43+ @ Autowired
44+ private AppUserRepo userRepo ;
45+
3746 @ ApiMapping ("/all" )
3847 @ Auth (role ="ROLE_ANONYMOUS" )
3948 public ApiResponse getAllServices () {
@@ -56,23 +65,32 @@ public ApiResponse getService(@ApiVariable Long id) {
5665 @ ApiMapping ("/create" )
5766 @ Auth (role = "ROLE_SERVICE_MANAGER" )
5867 @ ApiValidation (business = { @ ApiValidation .Business (value = CREATE ), @ ApiValidation .Business (value = EXISTS ) })
59- public ApiResponse createService (@ ApiValidatedModel Service service ) {
68+ public ApiResponse createService (@ ApiValidatedModel Service service , @ ApiCredentials Credentials credentials ) {
6069 service = serviceRepo .create (service .getName (), service .getStatus (), service .getIsAuto (), service .getIsPublic (), service .getOnShortList (), service .getServiceUrl (), service .getDescription ());
6170 simpMessagingTemplate .convertAndSend ("/channel/service" , new ApiResponse (SUCCESS , serviceRepo .findAll ()));
62- systemMonitorService . updateAll ( );
71+ sendStatusUpdate ( service , credentials );
6372 return new ApiResponse (SUCCESS , service );
6473 }
6574
6675 @ ApiMapping ("/update" )
6776 @ Auth (role = "ROLE_SERVICE_MANAGER" )
6877 @ ApiValidation (business = { @ ApiValidation .Business (value = UPDATE ), @ ApiValidation .Business (value = NONEXISTS ) })
69- public ApiResponse updateService (@ ApiValidatedModel Service service ) {
78+ public ApiResponse updateService (@ ApiValidatedModel Service service , @ ApiCredentials Credentials credentials ) {
7079 service = serviceRepo .save (service );
7180 simpMessagingTemplate .convertAndSend ("/channel/service/" + service .getId (), new ApiResponse (SUCCESS , service ));
72- systemMonitorService . updateAll ( );
81+ sendStatusUpdate ( service , credentials );
7382 return new ApiResponse (SUCCESS , service );
7483 }
7584
85+ private void sendStatusUpdate (Service service , Credentials credentials ) {
86+ AppUser user = userRepo .findByUin (credentials .getUin ());
87+ if (user .getRole () == ROLE_ANONYMOUS || user .getRole () == ROLE_USER ) {
88+ simpMessagingTemplate .convertAndSend ("/channel/status/overall-public" , new ApiResponse (SUCCESS , systemMonitorService .getOverallStatusPublic ()));
89+ } else {
90+ simpMessagingTemplate .convertAndSend ("/channel/status/overall-full" , new ApiResponse (SUCCESS , systemMonitorService .getOverallStatus ()));
91+ }
92+ }
93+
7694 @ ApiMapping ("/remove" )
7795 @ Auth (role = "ROLE_SERVICE_MANAGER" )
7896 @ ApiValidation (business = { @ ApiValidation .Business (value = DELETE ), @ ApiValidation .Business (value = NONEXISTS ) })
0 commit comments