added prize draw name to tables
This commit is contained in:
@@ -11,14 +11,16 @@ const pool = mysql
|
||||
})
|
||||
.promise();
|
||||
|
||||
export const getUser = async () => {
|
||||
const [rows] = await pool.query("SELECT username FROM users");
|
||||
export const getInfo = async () => {
|
||||
const [rows] = await pool.query("SELECT username FROM users;");
|
||||
const [rows2] = await pool.query("SELECT name FROM prize_draws;");
|
||||
|
||||
if (rows.length > 0) {
|
||||
if (rows.length > 0 && rows2.length > 0) {
|
||||
const users = rows.map((r) => r.username);
|
||||
return { users };
|
||||
const prize_draws = rows2.map((n) => n.name);
|
||||
return { users, prize_draws };
|
||||
} else {
|
||||
return { users: [], message: "No data found" };
|
||||
return { message: "No data found" };
|
||||
}
|
||||
};
|
||||
|
||||
@@ -43,6 +45,7 @@ export const confirmUser = async (username) => {
|
||||
const [createTable] = await pool.query(
|
||||
`CREATE TABLE IF NOT EXISTS ?? (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
Verlosung VARCHAR(100) NOT NULL,
|
||||
Vorname VARCHAR(100) NOT NULL,
|
||||
Nachname Varchar(100) NOT NULL,
|
||||
EMail Varchar(100) NOT NULL,
|
||||
@@ -80,7 +83,7 @@ export const confirmUser = async (username) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const newEntry = async (formData, username) => {
|
||||
export const newEntry = async (formData, username, prizeDraw) => {
|
||||
const confirmation = await confirmUser(username);
|
||||
|
||||
if (!confirmation || !confirmation.success) {
|
||||
@@ -90,9 +93,10 @@ export const newEntry = async (formData, username) => {
|
||||
const tableName = confirmation.tableName;
|
||||
|
||||
const [result] = await pool.query(
|
||||
`INSERT INTO ?? (Vorname, Nachname, EMail, Telefonnummer, Lose, Firmenname, Vorname_Geschaeftlich, Nachname_Geschaeftlich, EMail_Geschaeftlich, Telefonnummer_Geschaeftlich, Strasse_Hausnr, Plz_Ort, Zahlungsmethode) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
`INSERT INTO ?? (Verlosung, Vorname, Nachname, EMail, Telefonnummer, Lose, Firmenname, Vorname_Geschaeftlich, Nachname_Geschaeftlich, EMail_Geschaeftlich, Telefonnummer_Geschaeftlich, Strasse_Hausnr, Plz_Ort, Zahlungsmethode) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
[
|
||||
tableName,
|
||||
prizeDraw,
|
||||
formData.firstName,
|
||||
formData.lastName,
|
||||
formData.email,
|
||||
|
||||
@@ -3,24 +3,33 @@ import dotenv from "dotenv";
|
||||
const router = express.Router();
|
||||
dotenv.config();
|
||||
|
||||
import { getUser, newEntry, confirmUser } from "./frontend.data.js";
|
||||
import { getInfo, newEntry, confirmUser } from "./frontend.data.js";
|
||||
|
||||
router.post("/new-entry", async (req, res) => {
|
||||
const username = req.query.username;
|
||||
const result = await newEntry(req.body, username);
|
||||
const draw = req.query.draw;
|
||||
|
||||
const result = await newEntry(req.body, username, draw);
|
||||
|
||||
if (!result.success) {
|
||||
return res.status(500).json({ message: "Form Data Invalid" });
|
||||
}
|
||||
res.sendStatus(204);
|
||||
});
|
||||
|
||||
router.get("/users", async (req, res) => {
|
||||
const users = await getUser();
|
||||
res.json(users);
|
||||
router.get("/info", async (req, res) => {
|
||||
const info = await getInfo();
|
||||
|
||||
if (!info) {
|
||||
return res.status(500).json({ message: "Server error" });
|
||||
}
|
||||
|
||||
res.json(info);
|
||||
});
|
||||
|
||||
router.get("/confirm-user", async (req, res) => {
|
||||
const username = req.query.username;
|
||||
|
||||
if (!username) {
|
||||
return res.status(400).json({ message: "Username is required" });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user