|
| 1 | +package edu.tamu.app.controller; |
| 2 | + |
| 3 | +import static edu.tamu.framework.enums.ApiResponseType.SUCCESS; |
| 4 | +import static edu.tamu.framework.enums.BusinessValidationType.CREATE; |
| 5 | +import static edu.tamu.framework.enums.BusinessValidationType.DELETE; |
| 6 | +import static edu.tamu.framework.enums.BusinessValidationType.EXISTS; |
| 7 | +import static edu.tamu.framework.enums.BusinessValidationType.NONEXISTS; |
| 8 | +import static edu.tamu.framework.enums.BusinessValidationType.UPDATE; |
| 9 | + |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.messaging.simp.SimpMessagingTemplate; |
| 12 | +import org.springframework.web.bind.annotation.RestController; |
| 13 | + |
| 14 | +import edu.tamu.app.model.Service; |
| 15 | +import edu.tamu.app.model.repo.ServiceRepo; |
| 16 | +import edu.tamu.framework.aspect.annotation.ApiMapping; |
| 17 | +import edu.tamu.framework.aspect.annotation.ApiValidatedModel; |
| 18 | +import edu.tamu.framework.aspect.annotation.ApiValidation; |
| 19 | +import edu.tamu.framework.aspect.annotation.ApiVariable; |
| 20 | +import edu.tamu.framework.aspect.annotation.Auth; |
| 21 | +import edu.tamu.framework.model.ApiResponse; |
| 22 | + |
| 23 | +@RestController |
| 24 | +@ApiMapping("/service") |
| 25 | +public class ServiceController { |
| 26 | + |
| 27 | + @Autowired |
| 28 | + private SimpMessagingTemplate simpMessagingTemplate; |
| 29 | + |
| 30 | + @Autowired |
| 31 | + private ServiceRepo serviceRepo; |
| 32 | + |
| 33 | + @ApiMapping("/all") |
| 34 | + @Auth(role="ROLE_STAFF") |
| 35 | + public ApiResponse getAllServices() { |
| 36 | + return new ApiResponse(SUCCESS, serviceRepo.findAll()); |
| 37 | + } |
| 38 | + |
| 39 | + @ApiMapping("/public") |
| 40 | + @Auth(role="ROLE_ANONYMOUS") |
| 41 | + public ApiResponse getPublicServices() { |
| 42 | + return new ApiResponse(SUCCESS, serviceRepo.findByIsPublic(true)); |
| 43 | + } |
| 44 | + |
| 45 | + @ApiMapping("/get/{id}") |
| 46 | + @Auth(role = "ROLE_ANONYMOUS") |
| 47 | + public ApiResponse getService(@ApiVariable Long id) { |
| 48 | + Service service = serviceRepo.findOne(id); |
| 49 | + return new ApiResponse(SUCCESS, service); |
| 50 | + } |
| 51 | + |
| 52 | + @ApiMapping("/create") |
| 53 | + @Auth(role = "ROLE_SERVICE_MANAGER") |
| 54 | + @ApiValidation(business = { @ApiValidation.Business(value = CREATE), @ApiValidation.Business(value = EXISTS) }) |
| 55 | + public ApiResponse createService(@ApiValidatedModel Service service) { |
| 56 | + service = serviceRepo.create(service.getName(), service.getStatus(), service.getIsAuto(), service.getIsPublic(), service.getOnShortList(), service.getServiceUrl()); |
| 57 | + simpMessagingTemplate.convertAndSend("/channel/service", new ApiResponse(SUCCESS, serviceRepo.findAll())); |
| 58 | + return new ApiResponse(SUCCESS, service); |
| 59 | + } |
| 60 | + |
| 61 | + @ApiMapping("/update") |
| 62 | + @Auth(role = "ROLE_SERVICE_MANAGER") |
| 63 | + @ApiValidation(business = { @ApiValidation.Business(value = UPDATE), @ApiValidation.Business(value = NONEXISTS) }) |
| 64 | + public ApiResponse updateService(@ApiValidatedModel Service service) { |
| 65 | + service = serviceRepo.save(service); |
| 66 | + simpMessagingTemplate.convertAndSend("/channel/service/" + service.getId(), new ApiResponse(SUCCESS, service)); |
| 67 | + return new ApiResponse(SUCCESS, service); |
| 68 | + } |
| 69 | + |
| 70 | + @ApiMapping("/remove") |
| 71 | + @Auth(role = "ROLE_SERVICE_MANAGER") |
| 72 | + @ApiValidation(business = { @ApiValidation.Business(value = DELETE), @ApiValidation.Business(value = NONEXISTS) }) |
| 73 | + public ApiResponse removeService(@ApiValidatedModel Service service) { |
| 74 | + serviceRepo.delete(service); |
| 75 | + simpMessagingTemplate.convertAndSend("/channel/service", new ApiResponse(SUCCESS, serviceRepo.findAll())); |
| 76 | + return new ApiResponse(SUCCESS); |
| 77 | + } |
| 78 | +} |
0 commit comments