mirror of
https://github.com/aykhans/movier.git
synced 2025-07-17 16:04:02 +00:00
Rewritten in go and python
This commit is contained in:
16
server/pkg/storage/postgresql/db.go
Normal file
16
server/pkg/storage/postgresql/db.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package postgresql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
func NewDB(dbURL string) (*pgx.Conn, error) {
|
||||
conn, err := pgx.Connect(context.Background(), dbURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to connect to database: %w", err)
|
||||
}
|
||||
return conn, nil
|
||||
}
|
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS imdb;
|
@@ -0,0 +1,8 @@
|
||||
CREATE TABLE IF NOT EXISTS imdb (
|
||||
tconst VARCHAR(12) PRIMARY KEY NOT NULL,
|
||||
year SMALLINT NOT NULL DEFAULT 0,
|
||||
genres TEXT NOT NULL DEFAULT '',
|
||||
nconsts TEXT NOT NULL DEFAULT '',
|
||||
rating REAL NOT NULL DEFAULT 0.0,
|
||||
votes INTEGER NOT NULL DEFAULT 0
|
||||
);
|
107
server/pkg/storage/postgresql/repository/imdb.go
Normal file
107
server/pkg/storage/postgresql/repository/imdb.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"github.com/aykhans/movier/server/pkg/dto"
|
||||
)
|
||||
|
||||
type IMDbRepository struct {
|
||||
db *pgx.Conn
|
||||
}
|
||||
|
||||
func NewIMDbRepository(db *pgx.Conn) *IMDbRepository {
|
||||
return &IMDbRepository{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (repo *IMDbRepository) InsertMultipleBasics(basics []dto.Basic) error {
|
||||
batch := &pgx.Batch{}
|
||||
for _, basic := range basics {
|
||||
batch.Queue(
|
||||
`INSERT INTO imdb (tconst, year, genres)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (tconst) DO UPDATE
|
||||
SET year = EXCLUDED.year, genres = EXCLUDED.genres`,
|
||||
basic.Tconst, basic.StartYear, basic.Genres,
|
||||
)
|
||||
}
|
||||
|
||||
results := repo.db.SendBatch(context.Background(), batch)
|
||||
if err := results.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repo *IMDbRepository) GetAllTconsts() ([]string, error) {
|
||||
rows, err := repo.db.Query(
|
||||
context.Background(),
|
||||
"SELECT tconst FROM imdb",
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var tconsts []string
|
||||
for rows.Next() {
|
||||
var tconst string
|
||||
if err := rows.Scan(&tconst); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tconsts = append(tconsts, tconst)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return tconsts, nil
|
||||
}
|
||||
|
||||
func (repo *IMDbRepository) UpdateMultiplePrincipals(principals []dto.Principal) error {
|
||||
batch := &pgx.Batch{}
|
||||
for _, principal := range principals {
|
||||
batch.Queue(
|
||||
`UPDATE imdb SET nconsts = $1 WHERE tconst = $2`,
|
||||
principal.Nconsts, principal.Tconst,
|
||||
)
|
||||
}
|
||||
|
||||
results := repo.db.SendBatch(context.Background(), batch)
|
||||
if err := results.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repo *IMDbRepository) UpdateMultipleRatings(ratings []dto.Ratings) error {
|
||||
batch := &pgx.Batch{}
|
||||
for _, rating := range ratings {
|
||||
batch.Queue(
|
||||
`UPDATE imdb SET rating = $1, votes = $2 WHERE tconst = $3`,
|
||||
rating.Rating, rating.Votes, rating.Tconst,
|
||||
)
|
||||
}
|
||||
|
||||
results := repo.db.SendBatch(context.Background(), batch)
|
||||
if err := results.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repo *IMDbRepository) GetMinMax() (*dto.MinMax, error) {
|
||||
var minMax dto.MinMax
|
||||
|
||||
err := repo.db.QueryRow(
|
||||
context.Background(),
|
||||
"SELECT MIN(votes), MAX(votes), MIN(year), MAX(year), MIN(rating), MAX(rating) FROM imdb LIMIT 1",
|
||||
).Scan(&minMax.MinVotes, &minMax.MaxVotes, &minMax.MinYear, &minMax.MaxYear, &minMax.MinRating, &minMax.MaxRating)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &minMax, nil
|
||||
}
|
Reference in New Issue
Block a user