2024-03-09 12:19:55 +01:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-03-09 13:18:51 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ReviewSourceIMDB = "imdb"
|
2024-03-09 12:19:55 +01:00
|
|
|
|
2024-03-09 13:18:51 +01:00
|
|
|
MentionsSeparator = "|"
|
2024-03-09 12:19:55 +01:00
|
|
|
)
|
|
|
|
|
2024-03-09 13:18:51 +01:00
|
|
|
type ReviewSource string
|
|
|
|
|
|
|
|
type Titles struct {
|
|
|
|
Movies []string `json:"movies"`
|
|
|
|
TVShows []string `json:"tvShows"`
|
|
|
|
Games []string `json:"games"`
|
|
|
|
Books []string `json:"books"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Review struct {
|
|
|
|
ID string
|
|
|
|
MovieID string
|
|
|
|
Source ReviewSource
|
|
|
|
URL string
|
|
|
|
Review string
|
|
|
|
MovieRating int
|
|
|
|
Quality int
|
|
|
|
Titles Titles
|
|
|
|
}
|
|
|
|
|
|
|
|
type ReviewRepository struct {
|
2024-03-09 12:19:55 +01:00
|
|
|
db *Postgres
|
|
|
|
}
|
|
|
|
|
2024-03-09 13:18:51 +01:00
|
|
|
func NewReviewRepository(db *Postgres) *ReviewRepository {
|
|
|
|
return &ReviewRepository{
|
2024-03-09 12:19:55 +01:00
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-09 13:18:51 +01:00
|
|
|
func (rr *ReviewRepository) Store(r Review) error {
|
2024-03-09 12:19:55 +01:00
|
|
|
titles, err := json.Marshal(r.Titles)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := rr.db.Exec(`INSERT INTO review (id, movie_id, source, url, review, movie_rating, quality, mentioned_titles)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
|
|
|
ON CONFLICT (id) DO UPDATE SET movie_id = EXCLUDED.movie_id, source = EXCLUDED.source, url = EXCLUDED.url,
|
|
|
|
review = EXCLUDED.review, movie_rating = EXCLUDED.movie_rating, quality = EXCLUDED.quality,
|
|
|
|
mentioned_titles = EXCLUDED.mentioned_titles;`,
|
|
|
|
r.ID, r.MovieID, r.Source, r.URL, r.Review, r.MovieRating, r.Quality, titles); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-09 13:18:51 +01:00
|
|
|
func (rr *ReviewRepository) FindOne(id string) (Review, error) {
|
2024-03-09 12:19:55 +01:00
|
|
|
row := rr.db.QueryRow(`
|
|
|
|
SELECT id, movie_id, source, url, review, movie_rating, quality, mentioned_titles
|
|
|
|
FROM review
|
|
|
|
WHERE id=$1`, id)
|
|
|
|
if row.Err() != nil {
|
2024-03-09 13:18:51 +01:00
|
|
|
return Review{}, row.Err()
|
2024-03-09 12:19:55 +01:00
|
|
|
}
|
|
|
|
|
2024-03-09 13:18:51 +01:00
|
|
|
r := Review{}
|
2024-03-09 12:19:55 +01:00
|
|
|
var titles string
|
|
|
|
if err := row.Scan(&r.ID, &r.MovieID, &r.Source, &r.URL, &r.Review, &r.MovieRating, &r.Quality, &titles); err != nil {
|
2024-03-09 13:18:51 +01:00
|
|
|
return Review{}, err
|
2024-03-09 12:19:55 +01:00
|
|
|
}
|
|
|
|
if err := json.Unmarshal([]byte(titles), &r.Titles); err != nil {
|
2024-03-09 13:18:51 +01:00
|
|
|
return Review{}, err
|
2024-03-09 12:19:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2024-03-09 13:18:51 +01:00
|
|
|
func (rr *ReviewRepository) FindByMovieID(movieID string) ([]Review, error) {
|
2024-03-09 12:19:55 +01:00
|
|
|
rows, err := rr.db.Query(`
|
|
|
|
SELECT id, movie_id, source, url, review, movie_rating, quality, mentioned_titles
|
|
|
|
FROM review
|
|
|
|
WHERE movie_id=$1`, movieID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-09 13:18:51 +01:00
|
|
|
reviews := make([]Review, 0)
|
2024-03-09 12:19:55 +01:00
|
|
|
var titles string
|
|
|
|
for rows.Next() {
|
2024-03-09 13:18:51 +01:00
|
|
|
r := Review{}
|
2024-03-09 12:19:55 +01:00
|
|
|
if err := rows.Scan(&r.ID, &r.MovieID, &r.Source, &r.URL, &r.Review, &r.MovieRating, &r.Quality, &titles); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal([]byte(titles), &r.Titles); err != nil {
|
2024-03-09 13:18:51 +01:00
|
|
|
return []Review{}, err
|
2024-03-09 12:19:55 +01:00
|
|
|
}
|
|
|
|
reviews = append(reviews, r)
|
|
|
|
}
|
|
|
|
rows.Close()
|
|
|
|
|
|
|
|
return reviews, nil
|
|
|
|
}
|
|
|
|
|
2024-03-09 13:18:51 +01:00
|
|
|
func (rr *ReviewRepository) FindNextUnrated() (Review, error) {
|
2024-03-09 12:19:55 +01:00
|
|
|
row := rr.db.QueryRow(`
|
|
|
|
SELECT id, movie_id, source, url, review, movie_rating, quality, mentioned_titles
|
|
|
|
FROM review
|
|
|
|
WHERE quality=0
|
|
|
|
LIMIT 1`)
|
|
|
|
if row.Err() != nil {
|
2024-03-09 13:18:51 +01:00
|
|
|
return Review{}, row.Err()
|
2024-03-09 12:19:55 +01:00
|
|
|
}
|
|
|
|
|
2024-03-09 13:18:51 +01:00
|
|
|
r := Review{}
|
2024-03-09 12:19:55 +01:00
|
|
|
var titles string
|
|
|
|
if err := row.Scan(&r.ID, &r.MovieID, &r.Source, &r.URL, &r.Review, &r.MovieRating, &r.Quality, &titles); err != nil {
|
2024-03-09 13:18:51 +01:00
|
|
|
return Review{}, err
|
2024-03-09 12:19:55 +01:00
|
|
|
}
|
|
|
|
if err := json.Unmarshal([]byte(titles), &r.Titles); err != nil {
|
2024-03-09 13:18:51 +01:00
|
|
|
return Review{}, err
|
2024-03-09 12:19:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2024-03-09 13:18:51 +01:00
|
|
|
func (rr *ReviewRepository) FindUnrated() ([]Review, error) {
|
2024-03-09 12:19:55 +01:00
|
|
|
rows, err := rr.db.Query(`
|
|
|
|
SELECT id, movie_id, source, url, review, movie_rating, quality, mentioned_titles
|
|
|
|
FROM review
|
|
|
|
WHERE quality=0`)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-09 13:18:51 +01:00
|
|
|
reviews := make([]Review, 0)
|
2024-03-09 12:19:55 +01:00
|
|
|
var titles string
|
|
|
|
for rows.Next() {
|
2024-03-09 13:18:51 +01:00
|
|
|
r := Review{}
|
2024-03-09 12:19:55 +01:00
|
|
|
if err := rows.Scan(&r.ID, &r.MovieID, &r.Source, &r.URL, &r.Review, &r.MovieRating, &r.Quality, &titles); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal([]byte(titles), &r.Titles); err != nil {
|
2024-03-09 13:18:51 +01:00
|
|
|
return []Review{}, err
|
2024-03-09 12:19:55 +01:00
|
|
|
}
|
|
|
|
reviews = append(reviews, r)
|
|
|
|
}
|
|
|
|
rows.Close()
|
|
|
|
|
|
|
|
return reviews, nil
|
|
|
|
}
|
|
|
|
|
2024-03-09 13:18:51 +01:00
|
|
|
func (rr *ReviewRepository) FindNextNoTitles() (Review, error) {
|
2024-03-09 12:19:55 +01:00
|
|
|
row := rr.db.QueryRow(`
|
|
|
|
SELECT id, movie_id, source, url, review, movie_rating, quality, mentioned_titles
|
|
|
|
FROM review
|
|
|
|
WHERE mentioned_titles='{}'
|
|
|
|
LIMIT 1`)
|
|
|
|
if row.Err() != nil {
|
2024-03-09 13:18:51 +01:00
|
|
|
return Review{}, row.Err()
|
2024-03-09 12:19:55 +01:00
|
|
|
}
|
|
|
|
|
2024-03-09 13:18:51 +01:00
|
|
|
r := Review{}
|
2024-03-09 12:19:55 +01:00
|
|
|
var titles string
|
|
|
|
if err := row.Scan(&r.ID, &r.MovieID, &r.Source, &r.URL, &r.Review, &r.MovieRating, &r.Quality, &titles); err != nil {
|
2024-03-09 13:18:51 +01:00
|
|
|
return Review{}, err
|
2024-03-09 12:19:55 +01:00
|
|
|
}
|
|
|
|
if err := json.Unmarshal([]byte(titles), &r.Titles); err != nil {
|
2024-03-09 13:18:51 +01:00
|
|
|
return Review{}, err
|
2024-03-09 12:19:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2024-03-09 13:18:51 +01:00
|
|
|
func (rr *ReviewRepository) FindNoTitles() ([]Review, error) {
|
2024-03-09 12:19:55 +01:00
|
|
|
rows, err := rr.db.Query(`
|
|
|
|
SELECT id, movie_id, source, url, review, movie_rating, quality, mentioned_titles
|
|
|
|
FROM review
|
|
|
|
WHERE mentioned_titles='{}'`)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-09 13:18:51 +01:00
|
|
|
reviews := make([]Review, 0)
|
2024-03-09 12:19:55 +01:00
|
|
|
var titles string
|
|
|
|
for rows.Next() {
|
2024-03-09 13:18:51 +01:00
|
|
|
r := Review{}
|
2024-03-09 12:19:55 +01:00
|
|
|
if err := rows.Scan(&r.ID, &r.MovieID, &r.Source, &r.URL, &r.Review, &r.MovieRating, &r.Quality, &titles); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal([]byte(titles), &r.Titles); err != nil {
|
2024-03-09 13:18:51 +01:00
|
|
|
return []Review{}, err
|
2024-03-09 12:19:55 +01:00
|
|
|
}
|
|
|
|
reviews = append(reviews, r)
|
|
|
|
}
|
|
|
|
rows.Close()
|
|
|
|
|
|
|
|
return reviews, nil
|
|
|
|
}
|
|
|
|
|
2024-03-09 13:18:51 +01:00
|
|
|
func (rr *ReviewRepository) FindAll() ([]Review, error) {
|
2024-03-09 12:19:55 +01:00
|
|
|
rows, err := rr.db.Query(`
|
|
|
|
SELECT id, movie_id, source, url, review, movie_rating, quality, mentioned_titles
|
|
|
|
FROM review`)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-09 13:18:51 +01:00
|
|
|
reviews := make([]Review, 0)
|
2024-03-09 12:19:55 +01:00
|
|
|
var titles string
|
|
|
|
for rows.Next() {
|
2024-03-09 13:18:51 +01:00
|
|
|
r := Review{}
|
2024-03-09 12:19:55 +01:00
|
|
|
if err := rows.Scan(&r.ID, &r.MovieID, &r.Source, &r.URL, &r.Review, &r.MovieRating, &r.Quality, &titles); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal([]byte(titles), &r.Titles); err != nil {
|
2024-03-09 13:18:51 +01:00
|
|
|
return []Review{}, err
|
2024-03-09 12:19:55 +01:00
|
|
|
}
|
|
|
|
reviews = append(reviews, r)
|
|
|
|
}
|
|
|
|
rows.Close()
|
|
|
|
|
|
|
|
return reviews, nil
|
|
|
|
}
|
|
|
|
|
2024-03-09 13:18:51 +01:00
|
|
|
func (rr *ReviewRepository) DeleteByMovieID(id string) error {
|
2024-03-09 12:19:55 +01:00
|
|
|
if _, err := rr.db.Exec(`DELETE FROM review WHERE movie_id=$1`, id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|