Adding initial code for adding a new poll on the backend

This commit is contained in:
2025-11-04 16:39:10 -05:00
parent 3e2efae4ce
commit 5e8b4e2b61
8 changed files with 147 additions and 51 deletions

41
server/services/poll.go Normal file
View File

@@ -0,0 +1,41 @@
package services
import (
"go-sjles-pta-vote/server/db"
"go-sjles-pta-vote/server/models"
)
func CreatePoll(poll *models.Poll) (*models.Poll, error) {
new_poll := models.Poll{}
db_conn, err := db.Connect()
if err != nil {
return nil, err
}
defer db.Close()
stmt, err := db_conn.Prepare(`
INSERT INTO polls (
question,
expires_at
) VALUES (
$1,
$2
) RETURNING ID;
`)
if err != nil {
return nil, err
}
defer stmt.Close()
res, err := stmt.Exec(poll.Question, poll.ExpiresAt)
if err != nil {
return nil, err
}
new_poll.ID, err = res.LastInsertId()
return &new_poll, err
}