feat: enhance user confirmation and data submission; add newEntry function and update MainForm to handle user selection
This commit is contained in:
@@ -68,7 +68,7 @@ export const confirmUser = async (username) => {
|
|||||||
nextID = rows.length > 0 ? rows[0].id + 1 : 1;
|
nextID = rows.length > 0 ? rows[0].id + 1 : 1;
|
||||||
};
|
};
|
||||||
await getNextID();
|
await getNextID();
|
||||||
return { success: true, nextID };
|
return { success: true, nextID, tableName };
|
||||||
} else {
|
} else {
|
||||||
return { success: false, message: "Table creation failed" };
|
return { success: false, message: "Table creation failed" };
|
||||||
}
|
}
|
||||||
@@ -77,4 +77,33 @@ export const confirmUser = async (username) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const confirmData = async (data) => {};
|
export const newEntry = async (formData, username) => {
|
||||||
|
const confirmation = await confirmUser(username);
|
||||||
|
|
||||||
|
if (!confirmation || !confirmation.success) {
|
||||||
|
return { success: false, message: "User confirmation failed" };
|
||||||
|
}
|
||||||
|
|
||||||
|
const tableName = confirmation.tableName;
|
||||||
|
|
||||||
|
const [result] = await pool.query(
|
||||||
|
`INSERT INTO ${tableName} (Vorname, Nachname, EMail, Telefonnummer, Lose, Firmenname, Vorname_Geschaeftlich, Nachname_Geschaeftlich, EMail_Geschaeftlich, Telefonnummer_Geschaeftlich, Strasse_Hausnr, Plz_Ort, Zahlungsmethode) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||||
|
[
|
||||||
|
formData.firstName,
|
||||||
|
formData.lastName,
|
||||||
|
formData.email,
|
||||||
|
formData.phoneNumber,
|
||||||
|
formData.tickets,
|
||||||
|
formData.companyName,
|
||||||
|
formData.cmpFirstName,
|
||||||
|
formData.cpmLastName,
|
||||||
|
formData.cpmEmail,
|
||||||
|
formData.cpmPhoneNumber,
|
||||||
|
formData.street,
|
||||||
|
formData.postalCode,
|
||||||
|
formData.paymentMethod,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
return { success: true, insertId: result.insertId };
|
||||||
|
};
|
||||||
|
|||||||
@@ -3,15 +3,16 @@ import dotenv from "dotenv";
|
|||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
import { getUser, confirmData, confirmUser } from "./frontend.data.js";
|
import { getUser, newEntry, confirmUser } from "./frontend.data.js";
|
||||||
|
|
||||||
router.post("/frontend", async (req, res) => {
|
router.post("/new-entry", async (req, res) => {
|
||||||
const result = await confirmData(req.body);
|
const username = req.query.username;
|
||||||
|
const result = await newEntry(req.body, username);
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
return res.status(500).json({ message: "Form Data Invalid" });
|
return res.status(500).json({ message: "Form Data Invalid" });
|
||||||
}
|
}
|
||||||
console.log(req.body);
|
console.log(req.body);
|
||||||
console.log(user);
|
console.log(username);
|
||||||
res.sendStatus(204);
|
res.sendStatus(204);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,4 @@
|
|||||||
CREATE TABLE users (
|
CREATE TABLE users (
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
||||||
username VARCHAR(100) NOT NULL,
|
|
||||||
first_name VARCHAR(100) NOT NULL,
|
|
||||||
last_name VARCHAR(100) NOT NULL,
|
|
||||||
email VARCHAR(255) NOT NULL UNIQUE,
|
|
||||||
unique_key CHAR(25) NOT NULL UNIQUE,
|
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE usersNEW (
|
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
username VARCHAR(100) NOT NULL UNIQUE,
|
username VARCHAR(100) NOT NULL UNIQUE,
|
||||||
first_name VARCHAR(100) NOT NULL,
|
first_name VARCHAR(100) NOT NULL,
|
||||||
@@ -16,17 +6,20 @@ CREATE TABLE usersNEW (
|
|||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Trigger zur automatischen Generierung eines 25‑stelligen Keys
|
CREATE TABLE xx_DD_MM_YYYY (
|
||||||
DELIMITER $
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
Vorname VARCHAR(100) NOT NULL,
|
||||||
CREATE TRIGGER before_user_insert
|
Nachname Varchar(100) NOT NULL,
|
||||||
BEFORE INSERT ON users
|
EMail Varchar(100) NOT NULL,
|
||||||
FOR EACH ROW
|
Telefonnummer Varchar(100) NOT NULL,
|
||||||
BEGIN
|
Lose INT NOT NULL,
|
||||||
SET NEW.unique_key = SUBSTRING(
|
Firmenname Varchar(100),
|
||||||
REPLACE(UUID(), '-', '')
|
Vorname_Geschaeftlich Varchar(100),
|
||||||
-- UUID = 32 Zeichen, wir nehmen 25 davon
|
Nachname_Geschaeftlich Varchar(100),
|
||||||
, 1, 25);
|
EMail_Geschaeftlich Varchar(100),
|
||||||
END$
|
Telefonnummer_Geschaeftlich Varchar(100),
|
||||||
|
Strasse_Hausnr Varchar(100),
|
||||||
DELIMITER ;
|
Plz_Ort Varchar(100),
|
||||||
|
Zahlungsmethode Varchar(100),
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
);
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ export const MainForm = () => {
|
|||||||
const handleSubmit = async () => {
|
const handleSubmit = async () => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
try {
|
try {
|
||||||
const result = await submitFormData(formData);
|
const result = await submitFormData(formData, selectedUser || "");
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
setMsg({
|
setMsg({
|
||||||
type: "success",
|
type: "success",
|
||||||
|
|||||||
@@ -14,9 +14,9 @@ interface FormData {
|
|||||||
paymentMethod: string;
|
paymentMethod: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const submitFormData = async (data: FormData) => {
|
export const submitFormData = async (data: FormData, username: string) => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch("/backend/default/frontend", {
|
const response = await fetch(`http://localhost:8004/default/new-entry?username=${username}`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@@ -27,7 +27,7 @@ export const submitFormData = async (data: FormData) => {
|
|||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const errorText = await response.text();
|
const errorText = await response.text();
|
||||||
return { success: false, error: `Server error: ${errorText}` };
|
return { success: false, error: `Server error: ${errorText}` };
|
||||||
}
|
}
|
||||||
|
|
||||||
return { success: true };
|
return { success: true };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user