2023-09-08 22:24:58 +07:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2023-09-17 16:32:48 +07:00
|
|
|
"context"
|
2023-10-03 19:44:31 +07:00
|
|
|
"fmt"
|
2023-09-14 13:49:03 +07:00
|
|
|
|
2024-02-06 11:55:25 +07:00
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
2023-09-08 22:24:58 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
type Store interface {
|
|
|
|
Querier
|
2023-09-17 16:32:48 +07:00
|
|
|
GetTopListLocations(ctx context.Context, arg GetTopListLocationsParams) ([]GetTopListLocationsRow, error)
|
2023-09-19 21:49:48 +07:00
|
|
|
GetImagesByLocation(ctx context.Context, arg GetImagesByLocationParams) ([]GetImagesByLocationRow, error)
|
2023-09-27 21:58:14 +07:00
|
|
|
GetLocation(ctx context.Context, location_id int32) (GetLocationRow, error)
|
2023-09-23 15:24:21 +07:00
|
|
|
GetUser(ctx context.Context, username string) (GetUserRow, error)
|
2023-10-09 16:25:39 +07:00
|
|
|
UpdateUser(ctx context.Context, arg UpdateUserParams) (UpdateUserRow, error)
|
2023-10-04 19:51:29 +07:00
|
|
|
GetUserStats(ctx context.Context, user_id int32) (GetUserStatsRow, error)
|
2023-09-27 21:58:14 +07:00
|
|
|
CreateReview(ctx context.Context, arg CreateReviewParams) (Review, error)
|
|
|
|
GetListLocationReviews(ctx context.Context, arg GetListLocationReviewsParams) ([]GetListLocationReviewsRow, error)
|
2023-10-03 14:56:57 +07:00
|
|
|
CreateLocation(ctx context.Context, arg CreateLocationParams) (int32, error)
|
|
|
|
CreateImage(ctx context.Context, arg []CreateImageParams) error
|
2023-10-03 19:44:31 +07:00
|
|
|
CreateLocationTx(ctx context.Context, arg CreateLocationTxParams) error
|
2023-10-11 16:31:52 +07:00
|
|
|
GetNewsEventsList(ctx context.Context, arg GetNewsEventsListParams) ([]NewsEventRow, error)
|
2024-03-16 19:23:15 +07:00
|
|
|
GetListRecentLocationsWithRatings(ctx context.Context, arg GetListRecentLocationsParams) ([]GetListRecentLocationsWithRatingsRow, error)
|
2023-09-08 22:24:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
type SQLStore struct {
|
|
|
|
*Queries
|
2024-02-06 11:55:25 +07:00
|
|
|
pool *pgxpool.Pool
|
2023-09-08 22:24:58 +07:00
|
|
|
}
|
|
|
|
|
2024-02-06 11:55:25 +07:00
|
|
|
func NewStore(pool *pgxpool.Pool) Store {
|
2023-09-08 22:24:58 +07:00
|
|
|
return &SQLStore{
|
2024-02-06 11:55:25 +07:00
|
|
|
pool: pool,
|
|
|
|
Queries: New(pool),
|
2023-09-08 22:24:58 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TRANSACTION QUERY FUNCTION
|
2023-10-03 19:44:31 +07:00
|
|
|
func (store *SQLStore) execTx(ctx context.Context, fn func(*Queries) error) error {
|
2024-02-06 11:55:25 +07:00
|
|
|
tx, err := store.pool.Begin(ctx)
|
2023-10-03 19:44:31 +07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-08 22:24:58 +07:00
|
|
|
|
2023-10-03 19:44:31 +07:00
|
|
|
q := New(tx)
|
|
|
|
err = fn(q)
|
|
|
|
if err != nil {
|
2024-02-06 11:55:25 +07:00
|
|
|
if rbErr := tx.Rollback(ctx); rbErr != nil {
|
2023-10-03 19:44:31 +07:00
|
|
|
return fmt.Errorf("tx err: %v, rb err : %v", err, rbErr)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2024-02-06 11:55:25 +07:00
|
|
|
return tx.Commit(ctx)
|
2023-10-03 19:44:31 +07:00
|
|
|
}
|