2023-07-13 04:22:52 +00:00
|
|
|
package adapter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/gob"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gitea.dwysokinski.me/twhelp/dcbot/internal/domain"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
|
|
)
|
|
|
|
|
2023-07-22 06:30:31 +00:00
|
|
|
type VillageRedisRepository struct {
|
2023-07-13 04:22:52 +00:00
|
|
|
client redis.UniversalClient
|
|
|
|
}
|
|
|
|
|
2023-07-22 06:30:31 +00:00
|
|
|
func NewVillageRedisRepository(client redis.UniversalClient) *VillageRedisRepository {
|
|
|
|
return &VillageRedisRepository{client: client}
|
2023-07-13 04:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const translateVillageCoordsParamsExp = 72 * time.Hour
|
|
|
|
|
|
|
|
type translateVillageCoordsParamsRedis struct {
|
|
|
|
VersionCode string
|
|
|
|
ServerKey string
|
|
|
|
Coords []string
|
2024-05-02 05:57:24 +00:00
|
|
|
PerPage int
|
|
|
|
MaxPage int
|
2023-07-13 04:22:52 +00:00
|
|
|
SHA256 string
|
|
|
|
}
|
|
|
|
|
2023-07-22 06:30:31 +00:00
|
|
|
func (v *VillageRedisRepository) SaveTranslateCoordsParams(ctx context.Context, params domain.TranslateVillageCoordsParams) error {
|
2023-07-13 04:22:52 +00:00
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
|
|
|
|
if err := gob.NewEncoder(buf).Encode(translateVillageCoordsParamsRedis{
|
|
|
|
VersionCode: params.VersionCode(),
|
|
|
|
ServerKey: params.ServerKey(),
|
|
|
|
Coords: params.Coords(),
|
|
|
|
PerPage: params.PerPage(),
|
|
|
|
MaxPage: params.MaxPage(),
|
|
|
|
SHA256: params.SHA256(),
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := v.client.Set(
|
|
|
|
ctx,
|
|
|
|
v.buildTranslateCoordsParamsKey(params.SHA256()),
|
|
|
|
buf.Bytes(),
|
|
|
|
translateVillageCoordsParamsExp,
|
|
|
|
).Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-22 06:30:31 +00:00
|
|
|
func (v *VillageRedisRepository) GetTranslateCoordsParams(ctx context.Context, sha256Hash string) (domain.TranslateVillageCoordsParams, error) {
|
2023-07-13 04:22:52 +00:00
|
|
|
b, err := v.client.Get(ctx, v.buildTranslateCoordsParamsKey(sha256Hash)).Bytes()
|
|
|
|
if errors.Is(err, redis.Nil) {
|
|
|
|
return domain.TranslateVillageCoordsParams{}, domain.TranslateVillageCoordsParamsNotFoundError{
|
|
|
|
SHA256: sha256Hash,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return domain.TranslateVillageCoordsParams{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var params translateVillageCoordsParamsRedis
|
|
|
|
|
|
|
|
if err = gob.NewDecoder(bytes.NewReader(b)).Decode(¶ms); err != nil {
|
|
|
|
return domain.TranslateVillageCoordsParams{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := domain.UnmarshalTranslateVillageCoordsParamsFromDatabase(
|
|
|
|
params.VersionCode,
|
|
|
|
params.ServerKey,
|
|
|
|
params.Coords,
|
|
|
|
params.PerPage,
|
|
|
|
params.MaxPage,
|
|
|
|
params.SHA256,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return domain.TranslateVillageCoordsParams{}, fmt.Errorf("couldn't unmarshal TranslateVillageCoordsParams from database: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-07-22 06:30:31 +00:00
|
|
|
func (v *VillageRedisRepository) buildTranslateCoordsParamsKey(sha256Hash string) string {
|
2023-07-13 04:22:52 +00:00
|
|
|
return "translate_village_coords_params_" + sha256Hash
|
|
|
|
}
|