90 lines
3.7 KiB
Java
90 lines
3.7 KiB
Java
package com.eactive.testmaster.server.controller;
|
|
|
|
import com.eactive.testmaster.server.dto.ServerDTO;
|
|
import com.eactive.testmaster.server.dto.ServerSearch;
|
|
import com.eactive.testmaster.server.entity.Server;
|
|
import com.eactive.testmaster.server.mapper.ServerMapper;
|
|
import com.eactive.testmaster.server.service.ServerMgmtService;
|
|
import java.util.List;
|
|
import javax.validation.Valid;
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.PageRequest;
|
|
import org.springframework.data.domain.Pageable;
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.security.access.annotation.Secured;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
@RestController
|
|
@RequestMapping("/servers")
|
|
public class ServerRestController {
|
|
|
|
private final ServerMgmtService serverMgmtService;
|
|
|
|
private final ServerMapper serverMapper;
|
|
|
|
public ServerRestController(ServerMgmtService serverMgmtService, ServerMapper serverMapper) {
|
|
this.serverMgmtService = serverMgmtService;
|
|
this.serverMapper = serverMapper;
|
|
}
|
|
|
|
@GetMapping("/list.do")
|
|
@Secured("ROLE_API_TESTER")
|
|
public ResponseEntity<List<ServerDTO>> listView(@ModelAttribute("serverSearch") ServerSearch serverSearch) {
|
|
Pageable maxPageable = PageRequest.of(0, Integer.MAX_VALUE);
|
|
|
|
Page<Server> page = serverMgmtService.findAll(serverSearch.buildSpecification(), maxPageable);
|
|
HttpHeaders headers = new HttpHeaders();
|
|
headers.add("X-Total-Count", String.valueOf(page.getTotalElements()));
|
|
headers.add("X-Total-Pages", String.valueOf(page.getTotalPages()));
|
|
headers.add("X-Current-Page", String.valueOf(page.getNumber()));
|
|
headers.add("X-Page-Size", String.valueOf(page.getSize()));
|
|
return new ResponseEntity<>(serverMapper.map(page.getContent()), headers, HttpStatus.OK);
|
|
}
|
|
|
|
@GetMapping
|
|
@Secured({"ROLE_MANAGE_SERVER", "ROLE_API_TESTER"})
|
|
public ResponseEntity<Page<ServerDTO>> listServers(
|
|
@ModelAttribute ServerSearch serverSearch,
|
|
@RequestParam(defaultValue = "0") int page,
|
|
@RequestParam(defaultValue = "10") int size
|
|
) {
|
|
Pageable pageable = PageRequest.of(page, size);
|
|
Page<Server> servers = serverMgmtService.findAll(serverSearch.buildSpecification(), pageable);
|
|
|
|
return ResponseEntity.ok(servers.map(serverMapper::map));
|
|
}
|
|
|
|
|
|
@PostMapping
|
|
@Secured("ROLE_MANAGE_SERVER")
|
|
public ResponseEntity<ServerDTO> createServer(@RequestBody @Valid ServerDTO server) {
|
|
Server created = serverMgmtService.create(serverMapper.map(server));
|
|
return ResponseEntity.ok(serverMapper.map(created));
|
|
}
|
|
|
|
@PostMapping("/update/{id}")
|
|
@Secured("ROLE_MANAGE_SERVER")
|
|
public ResponseEntity<ServerDTO> updateServer(@PathVariable Long id, @RequestBody @Valid ServerDTO server) {
|
|
Server updated = serverMgmtService.update(id, serverMapper.map(server));
|
|
return ResponseEntity.ok(serverMapper.map(updated));
|
|
}
|
|
|
|
@PostMapping("/delete")
|
|
@Secured("ROLE_MANAGE_SERVER")
|
|
public ResponseEntity<Void> deleteServers(@RequestBody List<Long> ids) {
|
|
for (Long id : ids) {
|
|
serverMgmtService.delete(id);
|
|
}
|
|
return ResponseEntity.ok().build();
|
|
}
|
|
}
|