package eu.dnetlib.dnetrolemanagement.controllers; import eu.dnetlib.dnetrolemanagement.entities.Response; import eu.dnetlib.dnetrolemanagement.exception.ConflictException; import eu.dnetlib.dnetrolemanagement.services.RegistryService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.HttpClientErrorException; @RestController @RequestMapping("/super") public class SuperAdminController { private final RegistryService registryService; @Autowired public SuperAdminController(RegistryService registryService) { this.registryService = registryService; } /** * Create a new role (only for admins) */ @RequestMapping(value = "/create", method = RequestMethod.POST) public ResponseEntity createRole(@RequestParam("name") String name, @RequestParam(value = "description", required = false) String description) { try { if (registryService.getCouId(name) == null) { registryService.createRole(name, description != null ? description : ""); return ResponseEntity.ok(new Response("Role has been created successfully")); } else { throw new ConflictException("This role already exists"); } } catch (HttpClientErrorException e) { throw new ConflictException("This role already exists"); } } }