DB -> Tour add, edit, del

This commit is contained in:
Georg Reisinger 2021-04-14 22:11:53 +02:00
parent dd1357ec1d
commit bb8a796bd4
6 changed files with 92 additions and 11 deletions

View File

@ -8,6 +8,7 @@ import javafx.scene.image.Image;
import javafx.stage.Stage; import javafx.stage.Stage;
import tourplaner.business.ConfigHelper; import tourplaner.business.ConfigHelper;
import tourplaner.business.TourPlaner;
import java.io.IOException; import java.io.IOException;
@ -23,6 +24,7 @@ public class Main extends Application {
Parent root = FXMLLoader.load(Main.class.getResource("tourplaner.fxml")); Parent root = FXMLLoader.load(Main.class.getResource("tourplaner.fxml"));
primaryStage.setTitle(ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "start", "apptitle")); primaryStage.setTitle(ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "start", "apptitle"));
primaryStage.getIcons().add(new Image("file:"+ ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "start", "logo"))); primaryStage.getIcons().add(new Image("file:"+ ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "start", "logo")));
TourPlaner.init();
primaryStage.setScene(new Scene(root, 1500, 1000)); primaryStage.setScene(new Scene(root, 1500, 1000));
primaryStage.show(); primaryStage.show();
} }

View File

@ -13,20 +13,53 @@ import java.util.ArrayList;
public class TourPlaner{ public class TourPlaner{
private Logger logger; private Logger logger;
public TourPlaner(){ /**
// LogHelper.info(ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "start", "message"), * Erstellt alle DB Tabellen falls nötig
// ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "start", "app")); */
public static void init(){
new DbConnect().init(); new DbConnect().init();
} }
public ArrayList<Tour> getAllTours(){ /**
* Holt alle touren aus der DB
* @return Alle touren als ArrayList
*/
public static ArrayList<Tour> getAllTours(){
DbConnect dbConnect = new DbConnect(); DbConnect dbConnect = new DbConnect();
ArrayList<Tour> touren = dbConnect.getAllTouren(); ArrayList<Tour> touren = dbConnect.getAllTouren();
return touren; return touren;
} }
public String getMapJson(String start, String ziel){ /**
* Bearbeitet eine Tour
* @param oldname Alter Tourname
* @param tour Neuer Tourname
* @return false bei error
*/
public static boolean editTour(String oldname, Tour tour){
DbConnect dbConnect = new DbConnect();
return dbConnect.editTour(oldname, tour);
}
public static boolean delTour(String tourname){
DbConnect dbConnect = new DbConnect();
return dbConnect.delTour(tourname);
}
/**
* Fügt eine Tour hinzu
* @param newTour Neue Tour
* @return false bei error
*/
public static boolean addTour(Tour newTour){
DbConnect dbConnect = new DbConnect();
return dbConnect.addTour(newTour);
}
public static String getMapJson(String start, String ziel){
// TODO: 14.04.2021 Map Quest
return start + " " + ziel; return start + " " + ziel;
} }
} }

View File

@ -38,6 +38,34 @@ public class DbConnect {
} }
/**
* Bearbeitet eine Tour
* @param oldname Alter Tour name
* @param tour Neues Tour Object
* @return false bei error
*/
public boolean editTour(String oldname, Tour tour) {
return PostgresHelper.executeUpdate("UPDATE public.tour SET tourname = '"+tour.getName()+"', dauer = 1, mapjson = '"+tour.getMapJson()+"', start = '"+tour.getStart()+"', ziel = '"+tour.getZiel()+"', strecke = "+tour.getStrecke()+" WHERE tourname ='" + oldname + "';");
}
/**
* Fügt eine Tour hinzu
* @param tour Neue Tour
* @return false bei error
*/
public boolean addTour(Tour tour){
return PostgresHelper.executeUpdate("INSERT INTO public.tour (tourname, dauer, mapjson, start, ziel, strecke) VALUES ('"+tour.getName()+"', "+tour.getDauer()+", '"+tour.getMapJson()+"', '"+tour.getStart()+"', '"+tour.getZiel()+"', "+tour.getStrecke()+")");
}
/**
* Löscht eine Tour anhand des Tournamens
* @param tourname Tourname der zu löschen ist
* @return false bei error
*/
public boolean delTour(String tourname){
return PostgresHelper.executeUpdate("DELETE FROM public.tour WHERE tourname = '"+tourname+"'");
}
/** /**
* Holt alle Touren aus der Datenbank * Holt alle Touren aus der Datenbank
* @return Null bei fehler, sonst eine List aus den IDs * @return Null bei fehler, sonst eine List aus den IDs
@ -50,7 +78,7 @@ public class DbConnect {
ArrayList<Tour> touren = new ArrayList<>(); ArrayList<Tour> touren = new ArrayList<>();
try { try {
stmt = this.c.createStatement(); stmt = this.c.createStatement();
ResultSet rs = stmt.executeQuery("select tourname, dauer, mapjson, start, ziel, strecke from tour;"); ResultSet rs = stmt.executeQuery("select * from tour;");
while (rs.next()) { while (rs.next()) {
tourname = rs.getString("tourname"); tourname = rs.getString("tourname");
mapjson = rs.getString("mapjson"); mapjson = rs.getString("mapjson");

View File

@ -21,6 +21,16 @@ public class Tour {
this.log = new ArrayList<>(); this.log = new ArrayList<>();
} }
public void setTour(Tour tour){
this.dauer = tour.getDauer();
this.mapJson = tour.getMapJson();
this.strecke = tour.getStrecke();
this.name = tour.getName();
this.start = tour.getStart();
this.ziel = tour.getZiel();
this.log = tour.getLogs();
}
/** /**
* Holt einen einzigen Log Eintrag anhand der Id * Holt einen einzigen Log Eintrag anhand der Id
* @param id Id des Eintrags der zu besorgen ist * @param id Id des Eintrags der zu besorgen ist

View File

@ -49,6 +49,8 @@ public class TourplanerController implements Initializable {
@FXML @FXML
private void editTourBtn(){ private void editTourBtn(){
this.viewModel.editTour(); this.viewModel.editTour();
beschreibungTableView.getItems().removeIf(s -> true); //Leert die Table View komplett
beschreibungTableView.getItems().add(this.viewModel.getTour(this.viewModel.getSelectedTour().getName()));
} }
@FXML @FXML

View File

@ -60,8 +60,11 @@ public class ViewModel {
this.tourData.removeIf(tour -> tour.getName().equals(this.selectedTour.getName())); this.tourData.removeIf(tour -> tour.getName().equals(this.selectedTour.getName()));
this.tourNamen.removeIf(tour -> tour.equals(this.selectedTour.getName())); this.tourNamen.removeIf(tour -> tour.equals(this.selectedTour.getName()));
this.tourData.add(new Tour(this.neueTourName, "TBD", "TBD", 0, this.neueTourStart, this.neueTourZiel)); Tour newTour = new Tour(this.neueTourName, "TBD", "TBD", 0, this.neueTourStart, this.neueTourZiel);
this.tourData.add(newTour);
this.tourNamen.add(this.neueTourName); this.tourNamen.add(this.neueTourName);
LogHelper.info("Edit Tour: " + this.neueTourName + " upload zu Postgres war: " + TourPlaner.editTour(this.selectedTour.getName(), newTour), "ViewModel - EditTour");
selectTour(this.neueTourName);
} }
} }
} }
@ -140,7 +143,7 @@ public class ViewModel {
return false; return false;
} }
} }
return false; return true;
} }
@ -152,8 +155,10 @@ public class ViewModel {
tourStartInput(""); tourStartInput("");
tourZielInput(""); tourZielInput("");
if (getTour(this.neueTourName) == null) { if (getTour(this.neueTourName) == null) {
tourData.add(new Tour(this.neueTourName, "TBD", "TBD", 0, this.neueTourStart, this.neueTourZiel)); Tour newTour = new Tour(this.neueTourName, "1", "1", 0, this.neueTourStart, this.neueTourZiel);
tourData.add(newTour);
tourNamen.add(this.neueTourName); tourNamen.add(this.neueTourName);
TourPlaner.addTour(newTour);
} }
this.neueTourStart = null; this.neueTourStart = null;
this.neueTourZiel = null; this.neueTourZiel = null;
@ -483,7 +488,7 @@ public class ViewModel {
public ObservableList<Tour> getTourData() { public ObservableList<Tour> getTourData() {
tourData.removeAll(); tourData.removeAll();
// ObservableList<Tour> data = ; // ObservableList<Tour> data = ;
tourData.addAll(new TourPlaner().getAllTours()); tourData.addAll(TourPlaner.getAllTours());
return tourData; return tourData;
} }
@ -497,6 +502,7 @@ public class ViewModel {
tourNamen.removeIf(s -> s.equals(tourname)); tourNamen.removeIf(s -> s.equals(tourname));
logData.removeIf(s -> true); logData.removeIf(s -> true);
setSelectedTour(null); setSelectedTour(null);
TourPlaner.delTour(tourname);
}catch (NullPointerException e){ }catch (NullPointerException e){
LogHelper.error(ConfigHelper.getLangIniString("keinetourselected"), LogHelper.error(ConfigHelper.getLangIniString("keinetourselected"),
ConfigHelper.getLangIniString("deltournoselect")); ConfigHelper.getLangIniString("deltournoselect"));