Skip to content

Commit

Permalink
♻️ Fix: connect container
Browse files Browse the repository at this point in the history
  • Loading branch information
naohito-T committed Apr 14, 2024
1 parent 3770ccf commit fcd8ded
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
8 changes: 4 additions & 4 deletions backend/internal/repository/dynamo/shorturl.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type ItemKey struct {
ID string `dynamodbav:"id"`
}

func (r *ShortURLRepository) GetByShortURL(ctx context.Context, id string) (domain.ShortURL, error) {
func (r *ShortURLRepository) GetByShortURL(id string) (domain.ShortURL, error) {
r.logger.Debug("GetItemInput: %v", id)

itemKey := ItemKey{
Expand All @@ -56,7 +56,7 @@ func (r *ShortURLRepository) GetByShortURL(ctx context.Context, id string) (doma
return domain.ShortURL{}, err
}

result, err := r.Client.Conn.GetItem(ctx, &dynamodb.GetItemInput{
result, err := r.Client.Conn.GetItem(context.TODO(), &dynamodb.GetItemInput{
TableName: aws.String("offline-tinyurls"),
Key: av,
})
Expand All @@ -82,7 +82,7 @@ func (r *ShortURLRepository) GetByShortURL(ctx context.Context, id string) (doma
return shortURL, nil
}

func (r *ShortURLRepository) CreateShortURL(ctx context.Context, params *domain.ShortURL) error {
func (r *ShortURLRepository) CreateShortURL(params *domain.ShortURL) error {
r.logger.Debug("PutItemInput: %v", params)

item := TableItem{
Expand All @@ -95,7 +95,7 @@ func (r *ShortURLRepository) CreateShortURL(ctx context.Context, params *domain.
log.Fatal(err)
}

_, err = r.Client.Conn.PutItem(ctx, &dynamodb.PutItemInput{
_, err = r.Client.Conn.PutItem(context.TODO(), &dynamodb.PutItemInput{
TableName: aws.String("offline-tinyurls"),
Item: av,
})
Expand Down
2 changes: 0 additions & 2 deletions backend/internal/rest/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ type GuestContainer struct {
}

var newGuestContainer = sync.OnceValue(func() *GuestContainer {
// repositoryにinfrastructuresを追加する
dynamoRepo := repoDynamo.NewShortURLRepository(dynamo.NewDynamoConnection(), slog.NewLogger())
// usecaseにrepositoryを追加する
return &GuestContainer{
URLUsecase: usecase.NewURLUsecase(dynamoRepo),
}
Expand Down
5 changes: 4 additions & 1 deletion backend/internal/rest/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import (

"github.com/labstack/echo/v4"
"github.com/naohito-T/tinyurl/backend/internal/infrastructures/slog"
// "github.com/naohito-T/tinyurl/backend/internal/rest/container"
)

// https://tinyurl.com/app/api/url/create"
// NewRouter これもシングルトンにした場合の例が気になる
func NewRouter(e *echo.Echo) {
e.GET("/health", health)
// container := container.NewGuestContainer()

e.GET("/health", hello)
e.GET("/api/v1/urls/:shortUrl", hello)
e.POST("/api/v1/urls", hello)
}
Expand Down
18 changes: 10 additions & 8 deletions backend/internal/usecase/shorturl.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package usecase

import (
"context"

"github.com/naohito-T/tinyurl/backend/domain"
)

type IShortURLRepo interface {
GetByShortURL(ctx context.Context, id string) (domain.ShortURL, error)
CreateShortURL(ctx context.Context, params *domain.ShortURL) error
GetByShortURL(id string) (domain.ShortURL, error)
CreateShortURL(params *domain.ShortURL) error
}

type ShortURLUsecase struct {
Expand All @@ -21,10 +19,14 @@ func NewURLUsecase(u IShortURLRepo) *ShortURLUsecase {
}
}

func (u *ShortURLUsecase) GetByShortURL(ctx context.Context, id string) (domain.ShortURL, error) {
return u.shortURLRepo.GetByShortURL(ctx, id)
func (u *ShortURLUsecase) GetByShortURL(id string) domain.ShortURL {
shortURL, err := u.shortURLRepo.GetByShortURL(id)
if err != nil {
panic(err)
}
return shortURL
}

func (u *ShortURLUsecase) CreateShortURL(ctx context.Context, params *domain.ShortURL) error {
return u.shortURLRepo.CreateShortURL(ctx, params)
func (u *ShortURLUsecase) CreateShortURL(params *domain.ShortURL) error {
return u.shortURLRepo.CreateShortURL(params)
}

0 comments on commit fcd8ded

Please sign in to comment.