Skip to content

Commit

Permalink
RedisCommander初期化時にクライアントを取得するがそこでクライアントのnil判定は行わないように修正
Browse files Browse the repository at this point in the history
  • Loading branch information
YutaKakiki authored and YutaKakiki committed Jan 11, 2025
1 parent 2bca22d commit db16eac
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
taskUsecase "github.com/kakkky/app/application/usecase/task"
"github.com/kakkky/app/domain/errors"
taskDomain "github.com/kakkky/app/domain/task"
// "github.com/kakkky/app/infrastructure/db/sqlc"
)

type taskQueryService struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package task

import "context"

//go:generate mockgen -package=task -source=./interface_task_query_service.go -destination=./mock_task_query_service.go
//go:generate mockgen -package=task -source=./interface_task_queryservice.go -destination=./mock_task_queryservice.go
type TaskQueryService interface {
FetchTaskById(ctx context.Context, id string) (*FetchTaskDTO, error)
FetchUserTasks(ctx context.Context, userId string) ([]*FetchTaskDTO, error)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/infrastructure/db/sqlc/sqlc_querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sqlc
import (
"context"

queryservice "github.com/kakkky/app/adapter/query_service"
"github.com/kakkky/app/adapter/queryservice"
"github.com/kakkky/app/adapter/repository"
"github.com/kakkky/app/infrastructure/db"
)
Expand Down
22 changes: 11 additions & 11 deletions app/infrastructure/kvs/redis_commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,41 @@ package kvs

import (
"context"
"errors"

"time"

"github.com/kakkky/app/domain/errors"
"github.com/redis/go-redis/v9"
)

type RedisCommander struct {
cli *redis.Client
}

func NewRedisCommander() *RedisCommander {
return &RedisCommander{}
return &RedisCommander{
cli: GetRedisClient(),
}
}

func (rc *RedisCommander) Save(ctx context.Context, duration time.Duration, userID, jwtID string) error {
cli := GetRedisClient()
// Redis クライアントが nil の場合はエラーを返す
if cli == nil {
if rc.cli == nil {
return errors.New("failed to get Redis client")
}
status := cli.Set(ctx, userID, jwtID, duration)
status := rc.cli.Set(ctx, userID, jwtID, duration)
if status.Err() != nil {
return status.Err()
}
return nil
}

func (rc *RedisCommander) Load(ctx context.Context, userID string) (string, error) {
cli := GetRedisClient()
// Redis クライアントが nil の場合はエラーを返す
if cli == nil {
if rc.cli == nil {
return "", errors.New("failed to get Redis client")
}
status := cli.Get(ctx, userID)
status := rc.cli.Get(ctx, userID)
if status.Err() != nil {
// nilだったら空文字を返す
if status.Err() == redis.Nil {
Expand All @@ -47,12 +48,11 @@ func (rc *RedisCommander) Load(ctx context.Context, userID string) (string, erro
}

func (rc *RedisCommander) Delete(ctx context.Context, userID string) error {
cli := GetRedisClient()
// Redis クライアントが nil の場合はエラーを返す
if cli == nil {
if rc.cli == nil {
return errors.New("failed to get Redis client")
}
status := cli.Del(ctx, userID)
status := rc.cli.Del(ctx, userID)
if status.Err() != nil {
if status.Err() == redis.Nil {
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
queryservice "github.com/kakkky/app/adapter/query_service"
"github.com/kakkky/app/adapter/queryservice"
"github.com/kakkky/app/application/usecase/task"
"github.com/kakkky/app/domain/errors"
"github.com/kakkky/app/infrastructure/db/sqlc"
Expand Down
2 changes: 1 addition & 1 deletion app/infrastructure/router/handle_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

taskHandler "github.com/kakkky/app/adapter/presentation/handler/task"
"github.com/kakkky/app/adapter/presentation/middleware"
queryservice "github.com/kakkky/app/adapter/query_service"
"github.com/kakkky/app/adapter/queryservice"
"github.com/kakkky/app/adapter/repository"
"github.com/kakkky/app/application/usecase/auth"
taskUsecase "github.com/kakkky/app/application/usecase/task"
Expand Down
3 changes: 1 addition & 2 deletions app/infrastructure/router/handle_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import (
)

func handleUser(mux *http.ServeMux) {
sqlc := sqlc.NewSqlcQuerier()
userRepository := repository.NewUserRepository(sqlc)
userRepository := repository.NewUserRepository(sqlc.NewSqlcQuerier())
authorization := middleware.Authorication(
auth.NewAuthorizationUsecase(
authInfra.NewJWTAuthenticator(),
Expand Down

0 comments on commit db16eac

Please sign in to comment.