Skip to content

Commit

Permalink
vingo: add card id and name
Browse files Browse the repository at this point in the history
  • Loading branch information
hannes-dev committed Jun 25, 2024
1 parent 9bed987 commit 52df507
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 9 additions & 2 deletions vingo/database/cards.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package database
import "time"

type Card struct {
Id int `json:"id"`
Serial string `json:"serial"`
CreatedAt time.Time `json:"createdAt"`
Name string `json:"name"`
}

func CreateCard(serial string, user_id int) error {
Expand All @@ -13,7 +15,7 @@ func CreateCard(serial string, user_id int) error {
}

func GetCardsForUser(user_id int) ([]Card, error) {
rows, err := db.Query("SELECT serial, created_at FROM cards WHERE user_id = $1;", user_id)
rows, err := db.Query("SELECT id, serial, created_at, name FROM cards WHERE user_id = $1;", user_id)
if err != nil {
return nil, err
}
Expand All @@ -22,7 +24,7 @@ func GetCardsForUser(user_id int) ([]Card, error) {
cards := make([]Card, 0)
for rows.Next() {
var card Card
err := rows.Scan(&card.Serial, &card.CreatedAt)
err := rows.Scan(&card.Id, &card.Serial, &card.CreatedAt, &card.Name)
if err != nil {
return nil, err
}
Expand All @@ -31,3 +33,8 @@ func GetCardsForUser(user_id int) ([]Card, error) {

return cards, nil
}

func SetCardName(id int, name string, user_id int) error {
_, err := db.Exec("UPDATE cards SET name = $1 WHERE user_id = $2 and id = $3;", name, user_id, id)
return err
}
2 changes: 2 additions & 0 deletions vingo/database/migrations/2_cards_extra_column.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE cards ADD COLUMN name TEXT DEFAULT '' NOT NULL;
ALTER TABLE cards ADD COLUMN id SERIAL NOT NULL;

0 comments on commit 52df507

Please sign in to comment.