updated service
This commit is contained in:
@@ -4,13 +4,17 @@ import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.scene.input.MouseEvent;
|
||||
import javafx.scene.control.cell.PropertyValueFactory;
|
||||
import javafx.stage.Stage;
|
||||
import org.example.petshopdesktop.database.ServiceDB;
|
||||
import org.example.petshopdesktop.models.Service;
|
||||
import org.example.petshopdesktop.controllers.dialogcontrollers.ServiceDialogController;
|
||||
import javafx.stage.Modality;
|
||||
|
||||
|
||||
public class ServiceController {
|
||||
|
||||
@@ -52,7 +56,7 @@ public class ServiceController {
|
||||
|
||||
@FXML
|
||||
void btnAddClicked(ActionEvent event) {
|
||||
openDialog(null);
|
||||
openDialog(null, "Add");
|
||||
loadServices();
|
||||
}
|
||||
|
||||
@@ -66,50 +70,46 @@ public class ServiceController {
|
||||
return;
|
||||
}
|
||||
|
||||
openDialog(selected);
|
||||
openDialog(selected, "Edit");
|
||||
loadServices();
|
||||
}
|
||||
|
||||
@FXML
|
||||
void btnDeleteClicked(ActionEvent event) {
|
||||
void btnDeleteClicked(ActionEvent e) {
|
||||
|
||||
Service selected = tvServices.getSelectionModel().getSelectedItem();
|
||||
|
||||
if (selected == null) {
|
||||
showAlert("Select Service", "Please select a service to delete.");
|
||||
return;
|
||||
}
|
||||
Service service = tvServices.getSelectionModel().getSelectedItem();
|
||||
if (service == null) return;
|
||||
|
||||
try {
|
||||
ServiceDB.deleteService(selected.getServiceId());
|
||||
ServiceDB.deleteService(service.getServiceId());
|
||||
loadServices();
|
||||
} catch (Exception e) {
|
||||
showAlert("Database Error", "Unable to delete service.");
|
||||
e.printStackTrace();
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void openDialog(Service service) {
|
||||
private void openDialog(Service service, String mode) {
|
||||
|
||||
try {
|
||||
FXMLLoader loader = new FXMLLoader(
|
||||
getClass().getResource(
|
||||
"/org.example.petshopdesktop/dialogviews/ServiceDialogView.fxml"
|
||||
)
|
||||
getClass().getResource("/org/example/petshopdesktop/dialogviews/service-dialog-view.fxml")
|
||||
);
|
||||
|
||||
Parent root = loader.load();
|
||||
|
||||
// IMPORTANT: specify controller type
|
||||
ServiceDialogController controller =
|
||||
loader.getController();
|
||||
|
||||
controller.setService(service);
|
||||
|
||||
Stage stage = new Stage();
|
||||
stage.setScene(new Scene(root));
|
||||
stage.setScene(new Scene(loader.load()));
|
||||
|
||||
ServiceDialogController controller = loader.getController();
|
||||
controller.setMode(mode);
|
||||
|
||||
if (mode.equals("Edit")) {
|
||||
controller.setService(service);
|
||||
}
|
||||
|
||||
stage.initModality(Modality.APPLICATION_MODAL);
|
||||
stage.showAndWait();
|
||||
|
||||
loadServices();
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package org.example.petshopdesktop.controllers.dialogcontrollers;
|
||||
|
||||
import javafx.event.EventHandler;
|
||||
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.control.Button;
|
||||
@@ -10,6 +10,7 @@ import javafx.scene.input.MouseEvent;
|
||||
import javafx.stage.Stage;
|
||||
import org.example.petshopdesktop.database.ServiceDB;
|
||||
import org.example.petshopdesktop.models.Service;
|
||||
import javafx.scene.control.Alert;
|
||||
|
||||
public class ServiceDialogController {
|
||||
|
||||
@@ -37,62 +38,68 @@ public class ServiceDialogController {
|
||||
@FXML
|
||||
private TextField txtServicePrice;
|
||||
|
||||
private Service service; // holds selected service
|
||||
private String mode;
|
||||
private Service selectedService;
|
||||
|
||||
// This method is called from ServiceController
|
||||
public void setService(Service service) {
|
||||
@FXML
|
||||
public void initialize() {
|
||||
btnSave.setOnAction(e -> saveService());
|
||||
btnCancel.setOnAction(e -> close());
|
||||
}
|
||||
|
||||
this.service = service;
|
||||
public void setMode(String mode) {
|
||||
this.mode = mode;
|
||||
lblMode.setText(mode + " Service");
|
||||
|
||||
if (service == null) {
|
||||
lblMode.setText("Add Service");
|
||||
lblServiceId.setText("New");
|
||||
if (mode.equals("Add")) {
|
||||
lblServiceId.setVisible(false);
|
||||
} else {
|
||||
lblMode.setText("Edit Service");
|
||||
lblServiceId.setText("ID: " + service.getServiceId());
|
||||
|
||||
txtServiceName.setText(service.getServiceName());
|
||||
txtServiceDesc.setText(service.getServiceDesc());
|
||||
txtServiceDuration.setText(
|
||||
String.valueOf(service.getServiceDuration()));
|
||||
txtServicePrice.setText(
|
||||
String.valueOf(service.getServicePrice()));
|
||||
lblServiceId.setVisible(true);
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void buttonSaveClicked(MouseEvent mouseEvent) {
|
||||
public void setService(Service service) {
|
||||
this.selectedService = service;
|
||||
|
||||
lblServiceId.setText("ID: " + service.getServiceId());
|
||||
txtServiceName.setText(service.getServiceName());
|
||||
txtServiceDesc.setText(service.getServiceDesc());
|
||||
txtServiceDuration.setText(String.valueOf(service.getServiceDuration()));
|
||||
txtServicePrice.setText(String.valueOf(service.getServicePrice()));
|
||||
}
|
||||
|
||||
private void saveService() {
|
||||
|
||||
try {
|
||||
String name = txtServiceName.getText();
|
||||
String desc = txtServiceDesc.getText();
|
||||
int duration = Integer.parseInt(txtServiceDuration.getText());
|
||||
double price = Double.parseDouble(txtServicePrice.getText());
|
||||
|
||||
if (service == null) {
|
||||
service = new Service(0, "", "", 0, 0);
|
||||
}
|
||||
Service service = new Service(
|
||||
selectedService == null ? 0 : selectedService.getServiceId(),
|
||||
name,
|
||||
desc,
|
||||
duration,
|
||||
price
|
||||
);
|
||||
|
||||
service.setServiceName(txtServiceName.getText());
|
||||
service.setServiceDesc(txtServiceDesc.getText());
|
||||
service.setServiceDuration(
|
||||
Integer.parseInt(txtServiceDuration.getText()));
|
||||
service.setServicePrice(
|
||||
Double.parseDouble(txtServicePrice.getText()));
|
||||
|
||||
if (service.getServiceId() == 0) {
|
||||
if (mode.equals("Add")) {
|
||||
ServiceDB.insertService(service);
|
||||
} else {
|
||||
ServiceDB.updateService(service.getServiceId(), service);
|
||||
ServiceDB.updateService(selectedService.getServiceId(), service);
|
||||
}
|
||||
|
||||
closeStage(mouseEvent);
|
||||
close();
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
new Alert(Alert.AlertType.ERROR, "Invalid Input").showAndWait();
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void closeStage(MouseEvent mouseEvent) {
|
||||
Node node = (Node) mouseEvent.getSource();
|
||||
Stage stage = (Stage) node.getScene().getWindow();
|
||||
private void close() {
|
||||
Stage stage = (Stage) btnSave.getScene().getWindow();
|
||||
stage.close();
|
||||
}
|
||||
}
|
||||
@@ -6,53 +6,46 @@ import org.example.petshopdesktop.models.Service;
|
||||
|
||||
import java.sql.*;
|
||||
|
||||
/**
|
||||
* A class containing all the methods relating to CRUD on Services table
|
||||
*/
|
||||
public class ServiceDB {
|
||||
|
||||
/**
|
||||
* gets all the services into an observable list
|
||||
* @return a list of all the services
|
||||
* @throws SQLException if failed to find services in the database
|
||||
*/
|
||||
// ============================
|
||||
// GET ALL SERVICES
|
||||
// ============================
|
||||
public static ObservableList<Service> getServices() throws SQLException {
|
||||
|
||||
ObservableList<Service> services = FXCollections.observableArrayList();
|
||||
ObservableList<Service> list = FXCollections.observableArrayList();
|
||||
Connection conn = ConnectionDB.getConnection();
|
||||
|
||||
String sql = "SELECT * FROM service";
|
||||
Statement stmt = conn.createStatement();
|
||||
ResultSet rs = stmt.executeQuery("SELECT * FROM service");
|
||||
Service service;
|
||||
ResultSet rs = stmt.executeQuery(sql);
|
||||
|
||||
while (rs.next()) {
|
||||
|
||||
service = new Service(
|
||||
rs.getInt(1), // service_id
|
||||
rs.getString(2),// service_name
|
||||
rs.getString(3),// description
|
||||
rs.getInt(4), // duration
|
||||
rs.getDouble(5)// price
|
||||
Service service = new Service(
|
||||
rs.getInt("serviceId"),
|
||||
rs.getString("serviceName"),
|
||||
rs.getString("serviceDesc"),
|
||||
rs.getInt("serviceDuration"),
|
||||
rs.getDouble("servicePrice")
|
||||
);
|
||||
|
||||
services.add(service);
|
||||
list.add(service);
|
||||
}
|
||||
|
||||
conn.close();
|
||||
return services;
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a new service to the database
|
||||
*/
|
||||
// ============================
|
||||
// INSERT SERVICE
|
||||
// ============================
|
||||
public static int insertService(Service service) throws SQLException {
|
||||
|
||||
int numRows;
|
||||
|
||||
Connection conn = ConnectionDB.getConnection();
|
||||
|
||||
String sql =
|
||||
"INSERT INTO services (service_name, description, duration, price) " +
|
||||
"INSERT INTO service (serviceName, serviceDesc, serviceDuration, servicePrice) " +
|
||||
"VALUES (?, ?, ?, ?)";
|
||||
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
@@ -62,28 +55,23 @@ public class ServiceDB {
|
||||
stmt.setInt(3, service.getServiceDuration());
|
||||
stmt.setDouble(4, service.getServicePrice());
|
||||
|
||||
numRows = stmt.executeUpdate();
|
||||
int rows = stmt.executeUpdate();
|
||||
conn.close();
|
||||
|
||||
return numRows;
|
||||
return rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update existing service
|
||||
*/
|
||||
public static int updateService(int serviceId, Service service) throws SQLException {
|
||||
|
||||
int numRows;
|
||||
// ============================
|
||||
// UPDATE SERVICE
|
||||
// ============================
|
||||
public static int updateService(int id, Service service) throws SQLException {
|
||||
|
||||
Connection conn = ConnectionDB.getConnection();
|
||||
|
||||
String sql =
|
||||
"UPDATE services SET " +
|
||||
" service_name = ?, " +
|
||||
" description = ?, " +
|
||||
" duration = ?, " +
|
||||
" price = ? " +
|
||||
" WHERE service_id = ?";
|
||||
"UPDATE service SET " +
|
||||
"serviceName=?, serviceDesc=?, serviceDuration=?, servicePrice=? " +
|
||||
"WHERE serviceId=?";
|
||||
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
|
||||
@@ -91,31 +79,29 @@ public class ServiceDB {
|
||||
stmt.setString(2, service.getServiceDesc());
|
||||
stmt.setInt(3, service.getServiceDuration());
|
||||
stmt.setDouble(4, service.getServicePrice());
|
||||
stmt.setInt(5, serviceId);
|
||||
stmt.setInt(5, id);
|
||||
|
||||
numRows = stmt.executeUpdate();
|
||||
int rows = stmt.executeUpdate();
|
||||
conn.close();
|
||||
|
||||
return numRows;
|
||||
return rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a service
|
||||
*/
|
||||
public static int deleteService(int serviceId) throws SQLException {
|
||||
|
||||
int numRows;
|
||||
// ============================
|
||||
// DELETE SERVICE
|
||||
// ============================
|
||||
public static int deleteService(int id) throws SQLException {
|
||||
|
||||
Connection conn = ConnectionDB.getConnection();
|
||||
|
||||
String sql = "DELETE FROM services WHERE service_id = ?";
|
||||
String sql = "DELETE FROM service WHERE serviceId=?";
|
||||
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, serviceId);
|
||||
stmt.setInt(1, id);
|
||||
|
||||
numRows = stmt.executeUpdate();
|
||||
int rows = stmt.executeUpdate();
|
||||
conn.close();
|
||||
|
||||
return numRows;
|
||||
return rows;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user