backend/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java

50 lines
2.0 KiB
Java
Executable File

package com.plannaplan.controllers;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import com.plannaplan.App;
import com.plannaplan.models.ConfigData;
import com.plannaplan.services.ConfiguratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
@RestController
@CrossOrigin
@RequestMapping("/api/" + App.API_VERSION + "/configurator")
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Api(tags = { "ConfigController" }, value = "ConfigController", description = "All endpoints to configure an app")
public class ConfigController {
@Autowired
private ConfiguratorService contrl;
@PostMapping("/config")
@PreAuthorize("hasRole('ROLE_ADMIN')")
@ApiOperation("Imports data to system. To call you need to provide ADMIN token")
public ResponseEntity<String> configApp(
@RequestParam("file") @ApiParam(value = "file .xlsx that contains courses and groups with apoinnted rules") MultipartFile file) {
try {
final ConfigData data = new ConfigData(null, null, file.getInputStream());
this.contrl.config(data);
return new ResponseEntity<>("Sucess", HttpStatus.OK);
} catch (IOException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}