2020-08-04 17:30:03 +02:00
package com.plannaplan.controllers ;
import org.springframework.web.bind.annotation.CrossOrigin ;
import org.springframework.web.bind.annotation.RestController ;
import java.io.IOException ;
2020-12-09 15:02:06 +01:00
import java.util.Date ;
2020-08-04 17:30:03 +02:00
2020-09-15 11:31:30 +02:00
import com.plannaplan.App ;
2020-08-04 17:30:03 +02:00
import com.plannaplan.models.ConfigData ;
2020-12-09 15:02:06 +01:00
import com.plannaplan.models.TourData ;
2020-09-23 19:12:25 +02:00
import com.plannaplan.services.ConfiguratorService ;
2020-08-04 17:30:03 +02:00
import org.springframework.beans.factory.annotation.Autowired ;
2020-12-09 15:02:06 +01:00
import org.springframework.format.annotation.DateTimeFormat ;
2020-08-04 17:30:03 +02:00
import org.springframework.http.HttpStatus ;
import org.springframework.http.ResponseEntity ;
2020-10-16 16:31:10 +02:00
import org.springframework.security.access.prepost.PreAuthorize ;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity ;
2020-08-04 17:30:03 +02:00
import org.springframework.web.bind.annotation.PostMapping ;
2020-09-15 11:31:30 +02:00
import org.springframework.web.bind.annotation.RequestMapping ;
2020-08-04 17:30:03 +02:00
import org.springframework.web.bind.annotation.RequestParam ;
import org.springframework.web.multipart.MultipartFile ;
2020-11-04 16:40:02 +01:00
import io.swagger.annotations.Api ;
2020-11-04 16:58:26 +01:00
import io.swagger.annotations.ApiOperation ;
2020-11-04 17:23:29 +01:00
import io.swagger.annotations.ApiParam ;
2020-11-04 16:40:02 +01:00
2020-08-04 17:30:03 +02:00
@RestController
@CrossOrigin
2020-09-15 11:31:30 +02:00
@RequestMapping ( " /api/ " + App . API_VERSION + " /configurator " )
2020-10-16 16:31:10 +02:00
@EnableGlobalMethodSecurity ( prePostEnabled = true )
2020-11-04 16:40:02 +01:00
@Api ( tags = { " ConfigController " } , value = " ConfigController " , description = " All endpoints to configure an app " )
2020-08-04 17:30:03 +02:00
public class ConfigController {
@Autowired
2020-09-23 19:12:25 +02:00
private ConfiguratorService contrl ;
2020-08-04 17:30:03 +02:00
2020-12-09 15:02:06 +01:00
@PostMapping ( path = " /config " , consumes = { " multipart/form-data " } )
2020-10-16 16:31:10 +02:00
@PreAuthorize ( " hasRole('ROLE_ADMIN') " )
2020-11-04 16:58:26 +01:00
@ApiOperation ( " Imports data to system. To call you need to provide ADMIN token " )
2020-11-04 17:23:29 +01:00
public ResponseEntity < String > configApp (
2020-12-09 15:02:06 +01:00
@RequestParam ( " file " ) @ApiParam ( value = " file .xlsx that contains courses and groups with apoinnted rules " ) MultipartFile file ,
2020-12-09 15:10:19 +01:00
@RequestParam ( " firstTourBegin " ) @DateTimeFormat ( pattern = " dd.MM.yyyy " ) @ApiParam ( value = " Date when first tour begin in format dd.MM.yyyy " ) Date firstTourBegin ,
@RequestParam ( " firstTourEnd " ) @DateTimeFormat ( pattern = " dd.MM.yyyy " ) @ApiParam ( value = " Date when first tour ends in format dd.MM.yyyy " ) Date firstTourEnd ,
@RequestParam ( " secondTourBegin " ) @DateTimeFormat ( pattern = " dd.MM.yyyy " ) @ApiParam ( value = " Date when second tour begin in format dd.MM.yyyy " ) Date secondTourBegin ,
@RequestParam ( " secondTourEnd " ) @DateTimeFormat ( pattern = " dd.MM.yyyy " ) @ApiParam ( value = " Date when second tour ends in format dd.MM.yyyy " ) Date secondTourEnd ) {
2020-08-04 17:30:03 +02:00
try {
2020-12-09 15:02:06 +01:00
if ( ! ( firstTourBegin . before ( firstTourEnd )
& & ( firstTourEnd . before ( secondTourBegin ) | | firstTourEnd . equals ( secondTourBegin ) )
& & secondTourBegin . before ( secondTourEnd ) ) ) {
return new ResponseEntity < > ( " Bad dates " , HttpStatus . BAD_REQUEST ) ;
}
final TourData firstTour = new TourData ( firstTourBegin , firstTourEnd ) ;
final TourData secondTour = new TourData ( secondTourBegin , secondTourEnd ) ;
final ConfigData data = new ConfigData ( firstTour , secondTour , file . getInputStream ( ) ) ;
2020-08-04 17:30:03 +02:00
this . contrl . config ( data ) ;
return new ResponseEntity < > ( " Sucess " , HttpStatus . OK ) ;
} catch ( IOException e ) {
return new ResponseEntity < > ( e . getMessage ( ) , HttpStatus . INTERNAL_SERVER_ERROR ) ;
}
}
2020-12-23 13:48:26 +01:00
public ResponseEntity < String > initAdmin ( @RequestParam ( " ticket " ) String ticket ) {
return null ;
}
2020-08-04 17:30:03 +02:00
}