Refactor
This commit is contained in:
+15
-13
@@ -2,7 +2,7 @@ package lib
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Story struct {
|
||||
@@ -22,14 +22,21 @@ type Vote struct {
|
||||
|
||||
// create story
|
||||
func CreateStory(roomID int, title string) (*Story, error) {
|
||||
_, err := DB.Exec("INSERT INTO stories (room_id, title, voted) VALUES (?, ?, 0)", roomID, title)
|
||||
res, err := DB.Exec("INSERT INTO stories (room_id, title, voted) VALUES (?, ?, 0)", roomID, title)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s := &Story{RoomID: roomID, Title: title}
|
||||
row := DB.QueryRow("SELECT id, room_id, title, points, voted FROM stories WHERE room_id = ? ORDER BY id DESC LIMIT 1", roomID)
|
||||
err = row.Scan(&s.ID, &s.RoomID, &s.Title, &s.Points, &s.Voted)
|
||||
return s, err
|
||||
id64, err := res.LastInsertId()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Story{
|
||||
ID: int(id64),
|
||||
RoomID: roomID,
|
||||
Title: title,
|
||||
Points: nil,
|
||||
Voted: false,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// get stories for room
|
||||
@@ -138,11 +145,7 @@ func RenameStory(storyID int, title string) error {
|
||||
|
||||
// delete story and its votes
|
||||
func DeleteStory(storyID int) error {
|
||||
_, err := DB.Exec("DELETE FROM votes WHERE story_id = ?", storyID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = DB.Exec("DELETE FROM stories WHERE id = ?", storyID)
|
||||
_, err := DB.Exec("DELETE FROM stories WHERE id = ?", storyID)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -163,8 +166,7 @@ func tshirtToPoints(s string) float64 {
|
||||
case "?":
|
||||
return 0
|
||||
default:
|
||||
var n int
|
||||
fmt.Sscanf(s, "%d", &n)
|
||||
n, _ := strconv.Atoi(s)
|
||||
return float64(n)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user