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

90 lines
3.9 KiB
Java

package eu.dnetlib.dnetrolemanagement.controllers;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import eu.dnetlib.dnetrolemanagement.dto.User;
import eu.dnetlib.dnetrolemanagement.exception.ResourceNotFoundException;
import eu.dnetlib.dnetrolemanagement.services.RegistryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/admin")
public class AdminController {
private final RegistryService registryService;
private final Gson gson;
@Autowired
public AdminController(RegistryService registryService) {
this.gson = new Gson();
this.registryService = registryService;
}
/**
* Get the user info of the managers of a type(Community, etc.) with id(ee, egi, etc.)
*/
@RequestMapping(value = "/{type}/{id}", method = RequestMethod.GET)
public ResponseEntity<User[]> getInfos(@PathVariable("type") String type, @PathVariable("id") String id) {
Integer couId = registryService.getCouId(type, id);
if(couId != null) {
JsonArray users = registryService.getUserIdByCouId(couId, true);
JsonArray emails = registryService.getUserEmailByCouId(couId, true);
JsonArray names = registryService.getUserNamesByCouId(couId, true);
for (int i = 0; i < users.size(); i++) {
users.get(i).getAsJsonObject().addProperty("email", emails.get(i).getAsJsonObject().get("email").getAsString());
users.get(i).getAsJsonObject().addProperty("name", names.get(i).getAsJsonObject().get("name").getAsString());
}
return ResponseEntity.ok(gson.fromJson(users, User[].class));
}
throw new ResourceNotFoundException("Role has not been found");
}
/**
* Get the names of the managers of a type(Community, etc.) with id(ee, egi, etc.)
*/
@RequestMapping(value = "/{type}/{id}/name", method = RequestMethod.GET)
public ResponseEntity<User[]> getNames(@PathVariable("type") String type, @PathVariable("id") String id) {
Integer couId = registryService.getCouId(type, id);
if(couId != null) {
JsonArray users = registryService.getUserNamesByCouId(couId, true);
return ResponseEntity.ok(gson.fromJson(users, User[].class));
}
throw new ResourceNotFoundException("Role has not been found");
}
/**
* Get the Identifiers of the managers of a type(Community, etc.) with id(ee, egi, etc.)
*/
@RequestMapping(value = "/{type}/{id}/id", method = RequestMethod.GET)
public ResponseEntity<User[]> getIds(@PathVariable("type") String type, @PathVariable("id") String id) {
Integer couId = registryService.getCouId(type, id);
if(couId != null) {
JsonArray users = registryService.getUserIdByCouId(couId, true);
return ResponseEntity.ok(gson.fromJson(users, User[].class));
}
throw new ResourceNotFoundException("Role has not been found");
}
/**
* Get the emails of the managers of a type(Community, etc.) with id(ee, egi, etc.)
*/
@RequestMapping(value = "/{type}/{id}/email", method = RequestMethod.GET)
public ResponseEntity<User[]> getEmails(@PathVariable("type") String type, @PathVariable("id") String id) {
Integer couId = registryService.getCouId(type, id);
if(couId != null) {
JsonArray users = registryService.getUserEmailByCouId(couId, true);
return ResponseEntity.ok(gson.fromJson(users, User[].class));
}
throw new ResourceNotFoundException("Role has not been found");
}
}