// SaveCard saves a card to the database and sets its id func SaveCard(card Card, creator int) Card { sql := "insert into %s (cardbody, cardtype, cardblanks, createdby) VALUES ($1, $2, $3, $4) returning id, createdon" var id int64 err := database.QueryRow(sql, card.CardBody, card.CardType, card.CardBlanks, creator).Scan(&id, &card.CreatedOn) if err != nil { log.Fatal(err) return card } card.ID = id card.CreatedBy = int64(creator) return card }
// RateCard changes the card's rating and returns the new rating. // The modifiedon timestamo and the modifiedby field will be updated appropriately. // TODO: once a user system is in place, add this rating to the user's list of rated cards. func RateCard(newRating, ID, raterID int) float32 { sql := `update %s set rating = ((rating * raters + $1) / (raters + 1)), raters = raters + 1, modifiedon = current_timestamp, modifiedby = $2 where id = $3 returning rating` var cardRating float32 if err := database.QueryRow(sql, newRating, raterID, ID).Scan(&cardRating); err != nil { log.Fatal(err) return -1 } return cardRating }