emdb/storage/movie.go

112 lines
3.1 KiB
Go
Raw Permalink Normal View History

package storage
import (
"fmt"
"strings"
"github.com/google/uuid"
)
2024-03-09 13:18:51 +01:00
type Movie struct {
ID string `json:"id"`
TMDBID int64 `json:"tmdbID"`
IMDBID string `json:"imdbID"`
Title string `json:"title"`
EnglishTitle string `json:"englishTitle"`
Year int `json:"year"`
Directors []string `json:"directors"`
WatchedOn string `json:"watchedOn"`
Rating int `json:"rating"`
Summary string `json:"summary"`
Comment string `json:"comment"`
}
type MovieRepository struct {
db *Postgres
}
2024-03-09 13:18:51 +01:00
func NewMovieRepository(db *Postgres) *MovieRepository {
return &MovieRepository{
db: db,
}
}
2024-03-09 13:18:51 +01:00
func (mr *MovieRepository) Store(m Movie) error {
if m.ID == "" {
m.ID = uuid.New().String()
}
directors := strings.Join(m.Directors, ",")
if _, err := mr.db.Exec(`INSERT INTO movie (id, tmdb_id, imdb_id, title, english_title, year, directors, summary, watched_on, rating, comment)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
ON CONFLICT (id) DO UPDATE
SET
tmdb_id = EXCLUDED.tmdb_id,
imdb_id = EXCLUDED.imdb_id,
title = EXCLUDED.title,
english_title = EXCLUDED.english_title,
year = EXCLUDED.year,
directors = EXCLUDED.directors,
summary = EXCLUDED.summary,
watched_on = EXCLUDED.watched_on,
rating = EXCLUDED.rating,
comment = EXCLUDED.comment;`,
m.ID, m.TMDBID, m.IMDBID, m.Title, m.EnglishTitle, m.Year, directors, m.Summary, m.WatchedOn, m.Rating, m.Comment); err != nil {
2024-03-09 13:23:15 +01:00
return fmt.Errorf("%w: %v", ErrPostgresqlFailure, err)
}
return nil
}
2024-03-09 13:18:51 +01:00
func (mr *MovieRepository) Delete(id string) error {
if _, err := mr.db.Exec(`DELETE FROM movie WHERE id=$1`, id); err != nil {
2024-03-09 13:23:15 +01:00
return fmt.Errorf("%w: %v", ErrPostgresqlFailure, err)
}
return nil
}
2024-03-09 13:18:51 +01:00
func (mr *MovieRepository) FindOne(id string) (Movie, error) {
row := mr.db.QueryRow(`
SELECT id, tmdb_id, imdb_id, title, english_title, year, directors, summary, watched_on, rating, comment
FROM movie
WHERE id=$1`, id)
if row.Err() != nil {
2024-03-09 13:18:51 +01:00
return Movie{}, row.Err()
}
2024-03-09 13:18:51 +01:00
m := Movie{
ID: id,
}
var directors string
if err := row.Scan(&m.ID, &m.TMDBID, &m.IMDBID, &m.Title, &m.EnglishTitle, &m.Year, &directors, &m.Summary, &m.WatchedOn, &m.Rating, &m.Comment); err != nil {
2024-03-09 13:23:15 +01:00
return Movie{}, fmt.Errorf("%w: %w", ErrPostgresqlFailure, err)
}
m.Directors = strings.Split(directors, ",")
return m, nil
}
2024-03-09 13:18:51 +01:00
func (mr *MovieRepository) FindAll() ([]Movie, error) {
rows, err := mr.db.Query(`
SELECT id, tmdb_id, imdb_id, title, english_title, year, directors, summary, watched_on, rating, comment
FROM movie`)
if err != nil {
2024-03-09 13:23:15 +01:00
return nil, fmt.Errorf("%w: %v", ErrPostgresqlFailure, err)
}
2024-03-09 13:18:51 +01:00
movies := make([]Movie, 0)
defer rows.Close()
for rows.Next() {
2024-03-09 13:18:51 +01:00
m := Movie{}
var directors string
if err := rows.Scan(&m.ID, &m.TMDBID, &m.IMDBID, &m.Title, &m.EnglishTitle, &m.Year, &directors, &m.Summary, &m.WatchedOn, &m.Rating, &m.Comment); err != nil {
2024-03-09 13:23:15 +01:00
return nil, fmt.Errorf("%w: %v", ErrPostgresqlFailure, err)
}
m.Directors = strings.Split(directors, ",")
movies = append(movies, m)
}
return movies, nil
}