dnet-role-management/src/main/java/eu/dnetlib/dnetrolemanagement/controllers/SuperAdminController.java

42 lines
1.7 KiB
Java
Raw Normal View History

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<Response> 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");
}
}
}