/
countriesRepository.go
96 lines (78 loc) · 2.49 KB
/
countriesRepository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package postgresrepository
import (
"context"
"errors"
"fmt"
"strings"
"github.com/Falokut/movies_service/internal/models"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/jmoiron/sqlx"
"github.com/sirupsen/logrus"
)
type CountriesRepository struct {
db *sqlx.DB
logger *logrus.Logger
}
const (
countriesTableName = "countries"
moviesCountriesTableName = "movies_countries"
)
func NewCountriesRepository(db *sqlx.DB, logger *logrus.Logger) *CountriesRepository {
return &CountriesRepository{db: db, logger: logger}
}
func (r *CountriesRepository) GetCountries(ctx context.Context, movieId int32) (countries []string, err error) {
defer handleError(ctx, &err)
defer r.logError(err, "GetCountries")
query := fmt.Sprintf("SELECT name FROM %s JOIN %s ON country_id=id WHERE movie_id=$1 ORDER BY id",
moviesCountriesTableName, countriesTableName)
err = r.db.SelectContext(ctx, &countries, query, movieId)
return
}
func (r *CountriesRepository) GetAllCountries(ctx context.Context) (countries []models.Country, err error) {
defer handleError(ctx, &err)
defer r.logError(err, "GetAllCountries")
query := fmt.Sprintf("SELECT * FROM %s ORDER BY id", countriesTableName)
err = r.db.SelectContext(ctx, &countries, query)
return
}
func (r *CountriesRepository) GetCountriesForMovies(ctx context.Context, ids []string) (countries map[int32][]string, err error) {
defer handleError(ctx, &err)
defer r.logError(err, "GetCountriesForMovies")
query := fmt.Sprintf("SELECT movie_id, ARRAY_AGG(name) FROM %s JOIN %s ON country_id=id "+
"WHERE movie_id=ANY(ARRAY[%s]) GROUP BY movie_id",
moviesCountriesTableName, countriesTableName, strings.Join(ids, ","))
rows, err := r.db.QueryContext(ctx, query)
if err != nil {
return
}
countries = make(map[int32][]string, len(ids))
for rows.Next() {
var id int32
var names string
rows.Scan(&id, &names)
countries[id] = strings.Split(strings.Trim(names, "{}"), ",")
}
return
}
func (r *CountriesRepository) logError(err error, functionName string) {
if err == nil {
return
}
var repoErr = &models.ServiceError{}
if errors.As(err, &repoErr) {
r.logger.WithFields(
logrus.Fields{
"error.function.name": functionName,
"error.msg": repoErr.Msg,
"error.code": repoErr.Code,
},
).Error("countries repository error occurred")
} else {
r.logger.WithFields(
logrus.Fields{
"error.function.name": functionName,
"error.msg": err.Error(),
},
).Error("countries repository error occurred")
}
}