Deleting questions and adding tests for deleteing
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
var ErrQuestionAlreadyExists = errors.New("Question already exists")
|
||||
var ErrQuestionDoesntExist = errors.New("Question does not exist yet")
|
||||
var ErrVoterAlreadyVoted = errors.New("Voter already voted")
|
||||
var ErrPollNotFound = errors.New("Poll not found")
|
||||
|
||||
func CreatePoll(poll *models.Poll) (*models.Poll, error) {
|
||||
new_poll := models.Poll{}
|
||||
@@ -100,7 +101,7 @@ func GetPollByQuestion(question string) (*models.Poll, error) {
|
||||
)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, ErrQuestionDoesntExist
|
||||
return nil, ErrPollNotFound
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -132,7 +133,7 @@ func GetPollByQuestion(question string) (*models.Poll, error) {
|
||||
func GetAndCreatePollByQuestion(question string) (*models.Poll, error) {
|
||||
new_poll, err := GetPollByQuestion(question)
|
||||
|
||||
if err == ErrQuestionDoesntExist {
|
||||
if err == ErrPollNotFound {
|
||||
create_poll := &models.Poll{
|
||||
Question: question,
|
||||
ExpiresAt: time.Now().Add(time.Hour * 10).Format("2006-01-02 15:04:05"),
|
||||
@@ -235,3 +236,52 @@ func SetVote(vote *models.Vote) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete a poll by name
|
||||
func DeletePollByQuestion(question string) error {
|
||||
db_conn, err := db.Connect()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
delete_votes_stmt, err := db_conn.Prepare(`
|
||||
DELETE FROM voters
|
||||
WHERE poll_id IN (
|
||||
SELECT id
|
||||
FROM polls
|
||||
WHERE question == $1
|
||||
)
|
||||
`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer delete_votes_stmt.Close()
|
||||
|
||||
_, err = delete_votes_stmt.Exec(question)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
delete_poll_stmt, err := db_conn.Prepare(`
|
||||
DELETE FROM polls
|
||||
WHERE question == $1
|
||||
`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer delete_poll_stmt.Close()
|
||||
|
||||
res, err := delete_poll_stmt.Exec(question)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if num, err := res.RowsAffected(); num != 1 {
|
||||
return errors.New("Failed to delete poll")
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -459,3 +459,39 @@ func TestVoterAlreadyVoted(t *testing.T) {
|
||||
t.Fatalf("Expected ErrVoterAlreadyVoted, but got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeletePollByQuestion(t *testing.T) {
|
||||
// Preload the database with members, polls, and voters
|
||||
tmp_db, err := os.CreateTemp("", "vote_test.*.db")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temporary database: %v", err)
|
||||
}
|
||||
defer os.Remove(tmp_db.Name())
|
||||
|
||||
init_conf := &config.Config{
|
||||
DBPath: string(tmp_db.Name()),
|
||||
}
|
||||
config.SetConfig(init_conf)
|
||||
|
||||
err = PreLoadDB()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to preload database: %v", err)
|
||||
}
|
||||
|
||||
// Get a question from the new_polls array
|
||||
testQuestion := new_polls[0].question
|
||||
|
||||
// Delete the poll by question
|
||||
err = DeletePollByQuestion(testQuestion)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to delete poll by question: %v", err)
|
||||
}
|
||||
|
||||
// Verify that the poll was deleted
|
||||
_, err = GetPollByQuestion(testQuestion)
|
||||
if err == nil {
|
||||
t.Fatalf("Expected error when getting deleted poll, but got none")
|
||||
} else if err != ErrPollNotFound {
|
||||
t.Fatalf("Expected ErrPollNotFound, but got %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user