Added getting newest config
This commit is contained in:
@ -3,10 +3,6 @@ package com.plannaplan.repositories;
|
||||
import com.plannaplan.entities.AppConfig;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
public interface AppConfigRepository extends JpaRepository<AppConfig, Long> {
|
||||
|
||||
@Query("FROM AppConfig ORDER BY configDate DESC")
|
||||
AppConfig getCurrentConfig();
|
||||
}
|
||||
|
@ -1,27 +0,0 @@
|
||||
package com.plannaplan.services;
|
||||
|
||||
import com.plannaplan.entities.AppConfig;
|
||||
import com.plannaplan.repositories.AppConfigRepository;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class AppConfigService {
|
||||
@Autowired
|
||||
private AppConfigRepository repo;
|
||||
|
||||
public AppConfigService() {
|
||||
}
|
||||
|
||||
/**
|
||||
* saves config instance to dadabase
|
||||
*
|
||||
* @param appConfig AppConfig instance with correct dates
|
||||
* @return AppConfig with id from databse after save
|
||||
*/
|
||||
public AppConfig save(AppConfig appConfig) {
|
||||
return this.repo.save(appConfig);
|
||||
}
|
||||
|
||||
}
|
@ -2,10 +2,18 @@ package com.plannaplan.services;
|
||||
|
||||
import com.plannaplan.models.ConfigData;
|
||||
import com.plannaplan.models.FileData;
|
||||
import com.plannaplan.repositories.AppConfigRepository;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.security.DrbgParameters.Reseed;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collector;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.plannaplan.configutils.*;
|
||||
import com.plannaplan.entities.AppConfig;
|
||||
|
||||
@ -18,7 +26,7 @@ public class ConfiguratorService {
|
||||
@Autowired
|
||||
private FileToDatabaseMigrator migrator;
|
||||
@Autowired
|
||||
private AppConfigService configService;
|
||||
private AppConfigRepository configRepo;
|
||||
|
||||
public ConfiguratorService() {
|
||||
}
|
||||
@ -31,8 +39,30 @@ public class ConfiguratorService {
|
||||
public void config(ConfigData data) {
|
||||
FileReader reader = new FileReader(data.getFilestream());
|
||||
FileData coursesData = reader.read();
|
||||
this.configService.save(new AppConfig(data.getFirstTour(), data.getSecondTour()));
|
||||
this.configRepo.save(new AppConfig(data.getFirstTour(), data.getSecondTour()));
|
||||
migrator.migrate(coursesData);
|
||||
}
|
||||
|
||||
/**
|
||||
* current config getter
|
||||
*
|
||||
* @return AppConfig with newest config_date
|
||||
*/
|
||||
public AppConfig getCurrentConfig() {
|
||||
final List<AppConfig> repsonse = this.configRepo.findAll().stream().sorted(new Comparator<AppConfig>() {
|
||||
@Override
|
||||
public int compare(AppConfig i1, AppConfig i2) {
|
||||
if (i1.getConfigDate().after(i2.getConfigDate())) {
|
||||
return -1;
|
||||
}
|
||||
if (i1.getConfigDate().before(i2.getConfigDate())) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
|
||||
}
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
return repsonse.get(0);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user