emdb/cmd/api-service/moviestore/review.go

134 lines
3.2 KiB
Go
Raw Normal View History

2023-12-29 19:10:31 +01:00
package moviestore
2024-01-06 14:22:22 +01:00
import "strings"
2023-12-29 19:10:31 +01:00
const (
ReviewSourceIMDB = "imdb"
2024-01-06 14:40:36 +01:00
MentionsSeparator = "|"
2023-12-29 19:10:31 +01:00
)
type ReviewSource string
type Review struct {
2024-01-06 14:22:22 +01:00
ID string
MovieID string
Source ReviewSource
URL string
Review string
Quality int
Mentions []string
2023-12-29 19:10:31 +01:00
}
type ReviewRepository struct {
db *SQLite
}
func NewReviewRepository(db *SQLite) *ReviewRepository {
return &ReviewRepository{
db: db,
}
}
func (rr *ReviewRepository) Store(r Review) error {
2024-01-06 14:22:22 +01:00
if _, err := rr.db.Exec(`REPLACE INTO review (id, movie_id, source, url, review, quality, mentions) VALUES (?, ?, ?, ?, ?, ?, ?)`,
2024-01-06 14:40:36 +01:00
r.ID, r.MovieID, r.Source, r.URL, r.Review, r.Quality, strings.Join(r.Mentions, MentionsSeparator)); err != nil {
2023-12-29 19:10:31 +01:00
return err
}
return nil
}
func (rr *ReviewRepository) FindOne(id string) (Review, error) {
2024-01-06 14:22:22 +01:00
row := rr.db.QueryRow(`SELECT id, movie_id, source, url, review, quality, mentions FROM review WHERE id=?`, id)
2023-12-29 19:10:31 +01:00
if row.Err() != nil {
return Review{}, row.Err()
}
2024-01-06 14:22:22 +01:00
r := Review{}
var mentions string
if err := row.Scan(&r.ID, &r.MovieID, &r.Source, &r.URL, &r.Review, &r.Quality, &mentions); err != nil {
2023-12-29 19:10:31 +01:00
return Review{}, err
}
2024-01-06 14:58:29 +01:00
r.Mentions = make([]string, 0)
if mentions != "" {
r.Mentions = strings.Split(mentions, MentionsSeparator)
}
2024-01-06 14:22:22 +01:00
return r, nil
2023-12-29 19:10:31 +01:00
}
func (rr *ReviewRepository) FindByMovieID(movieID string) ([]Review, error) {
2024-01-06 14:22:22 +01:00
rows, err := rr.db.Query(`SELECT id, movie_id, source, url, review, quality, mentions FROM review WHERE movie_id=?`, movieID)
if err != nil {
return nil, err
}
reviews := make([]Review, 0)
var mentions string
for rows.Next() {
r := Review{}
if err := rows.Scan(&r.ID, &r.MovieID, &r.Source, &r.URL, &r.Review, &r.Quality, &mentions); err != nil {
return nil, err
}
2024-01-06 14:58:29 +01:00
r.Mentions = make([]string, 0)
if mentions != "" {
r.Mentions = strings.Split(mentions, MentionsSeparator)
}
2024-01-06 14:22:22 +01:00
reviews = append(reviews, r)
}
rows.Close()
return reviews, nil
}
2024-01-06 14:55:00 +01:00
func (rr *ReviewRepository) FindNextUnrated() (Review, error) {
row := rr.db.QueryRow(`SELECT id, movie_id, source, url, review, quality, mentions FROM review WHERE quality=0 LIMIT 1`)
if row.Err() != nil {
return Review{}, row.Err()
}
r := Review{}
var mentions string
if err := row.Scan(&r.ID, &r.MovieID, &r.Source, &r.URL, &r.Review, &r.Quality, &mentions); err != nil {
return Review{}, err
}
2024-01-06 14:58:29 +01:00
r.Mentions = make([]string, 0)
if mentions != "" {
r.Mentions = strings.Split(mentions, MentionsSeparator)
}
2024-01-06 14:55:00 +01:00
return r, nil
}
2024-01-06 14:22:22 +01:00
func (rr *ReviewRepository) FindUnrated() ([]Review, error) {
rows, err := rr.db.Query(`SELECT id, movie_id, source, url, review, quality, mentions FROM review WHERE quality=0`)
2023-12-29 19:10:31 +01:00
if err != nil {
return nil, err
}
reviews := make([]Review, 0)
2024-01-06 14:22:22 +01:00
var mentions string
2023-12-29 19:10:31 +01:00
for rows.Next() {
r := Review{}
2024-01-06 14:22:22 +01:00
if err := rows.Scan(&r.ID, &r.MovieID, &r.Source, &r.URL, &r.Review, &r.Quality, &mentions); err != nil {
2023-12-29 19:10:31 +01:00
return nil, err
}
2024-01-06 14:58:29 +01:00
r.Mentions = make([]string, 0)
if mentions != "" {
r.Mentions = strings.Split(mentions, MentionsSeparator)
}
2023-12-29 19:10:31 +01:00
reviews = append(reviews, r)
}
rows.Close()
return reviews, nil
}
2023-12-30 09:19:53 +01:00
func (rr *ReviewRepository) DeleteByMovieID(id string) error {
if _, err := rr.db.Exec(`DELETE FROM review WHERE movie_id=?`, id); err != nil {
return err
}
return nil
}