Update db schema to persist polls and choices
This commit is contained in:
+1
-1
@@ -57,7 +57,7 @@ submitBtn.onclick = function() {
|
|||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json", // Inform the server about the data format
|
"Content-Type": "application/json", // Inform the server about the data format
|
||||||
},
|
},
|
||||||
body: JSON.stringify(choices), // Convert the data to JSON string
|
body: JSON.stringify({ pollId: 1, username: submitName.value, choices: choices }), // Convert the data to JSON string
|
||||||
})
|
})
|
||||||
/*
|
/*
|
||||||
.then(response => {
|
.then(response => {
|
||||||
|
|||||||
+32
-12
@@ -19,6 +19,12 @@ var (
|
|||||||
db *sql.DB
|
db *sql.DB
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ChoicesPost struct {
|
||||||
|
PollId int `json:"pollId"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
Choices [][]bool `json:"choices"`
|
||||||
|
}
|
||||||
|
|
||||||
func initDatabase() {
|
func initDatabase() {
|
||||||
var err error
|
var err error
|
||||||
db, err = sql.Open("sqlite3", DB_FILE)
|
db, err = sql.Open("sqlite3", DB_FILE)
|
||||||
@@ -26,17 +32,31 @@ func initDatabase() {
|
|||||||
log.Fatalf("[!] Could not open database file %s, error: %s\n", DB_FILE, err)
|
log.Fatalf("[!] Could not open database file %s, error: %s\n", DB_FILE, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
const query = `
|
const createPollsQuery = `
|
||||||
CREATE TABLE IF NOT EXISTS choices (
|
CREATE TABLE IF NOT EXISTS polls (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
username TEXT NOT NULL,
|
name TEXT NOT NULL
|
||||||
choices TEXT NOT NULL
|
|
||||||
);
|
);
|
||||||
`
|
`
|
||||||
|
|
||||||
_, err = db.Exec(query)
|
_, err = db.Exec(createPollsQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("[!] Could not create database table, error: %s\n", err)
|
log.Fatalf("[!] Could not create database table polls, error: %s\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
const createChoicesQuery = `
|
||||||
|
CREATE TABLE IF NOT EXISTS choices (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
pollid INTEGER NOT NULL,
|
||||||
|
username TEXT NOT NULL,
|
||||||
|
choices TEXT NOT NULL,
|
||||||
|
FOREIGN KEY(pollid) REFERENCES polls(id)
|
||||||
|
);
|
||||||
|
`
|
||||||
|
|
||||||
|
_, err = db.Exec(createChoicesQuery)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("[!] Could not create database table choices, error: %s\n", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,25 +73,25 @@ func handlePostSubmit(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, POST")
|
w.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, POST")
|
||||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Origin")
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Origin")
|
||||||
|
|
||||||
var choices [][]bool
|
var choicesPost ChoicesPost
|
||||||
|
|
||||||
err := json.NewDecoder(r.Body).Decode(&choices)
|
err := json.NewDecoder(r.Body).Decode(&choicesPost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[!] Error decoding request Body %v, error: %s\n", r.Body, err)
|
log.Printf("[!] Error decoding request Body %v, error: %s\n", r.Body, err)
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
choicesJson, err := json.Marshal(choices)
|
choicesJson, err := json.Marshal(choicesPost.Choices)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[!] Could not marshal choices, error: %s\n", err)
|
log.Printf("[!] Could not marshal choices, error: %s\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
const query = `
|
const query = `
|
||||||
INSERT INTO choices (username, choices)
|
INSERT INTO choices (pollid, username, choices)
|
||||||
VALUES (?, ?);
|
VALUES (?, ?, ?);
|
||||||
`
|
`
|
||||||
|
|
||||||
_, err = db.Exec(query, "DUMMY USERNAME", string(choicesJson))
|
_, err = db.Exec(query, choicesPost.PollId, choicesPost.Username, string(choicesJson))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[!] Could not insert choices into database, error: %s\n", err)
|
log.Printf("[!] Could not insert choices into database, error: %s\n", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user