canape/backend/models/imdb_ratings.go

33 lines
802 B
Go

package models
import (
"time"
"github.com/jmoiron/sqlx"
)
const (
getRatingQueryByImdbID = ` SELECT * FROM imdb_ratings WHERE imdb_id=$1;`
upsertRatingQuery = `INSERT INTO imdb_ratings (imdb_id, rating, votes) VALUES (:imdb_id, :rating, :votes)
ON CONFLICT (imdb_id)
DO UPDATE
SET imdb_id=:imdb_id, rating=:rating, votes=:votes
RETURNING imdb_id;`
)
// ImdbRating represents the ImdbRating in the DB
type ImdbRating struct {
ImdbID string `db:"imdb_id"`
Rating float32 `db:"rating"`
Votes int `db:"votes"`
Created time.Time `db:"created_at"`
Updated time.Time `db:"updated_at"`
}
// TxUpsertImdbRating upsert a ImdbRating in a transaction
func TxUpsertImdbRating(t *sqlx.Tx, rating *ImdbRating) error {
_, err := t.NamedExec(upsertRatingQuery, rating)
return err
}