Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to use db.Transaction() #16

Merged
merged 3 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions pkg/fx/gormfx/config.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package gormfx

type Config struct {
DBDriver string `envconfig:"DB_DRIVER" default:""`
DBUser string `envconfig:"DB_USER" default:""`
DBPassword string `envconfig:"DB_PASSWORD" default:""`
DBHost string `envconfig:"DB_HOST" default:""`
DBName string `envconfig:"DB_NAME" default:""`
DBPort string `envconfig:"DB_PORT" default:""`
DBDriver string `envconfig:"DB_DRIVER" default:""`
DBUser string `envconfig:"DB_USER" default:""`
DBPassword string `envconfig:"DB_PASSWORD" default:""`
DBHost string `envconfig:"DB_HOST" default:""`
DBName string `envconfig:"DB_NAME" default:""`
DBPort string `envconfig:"DB_PORT" default:""`
ConnMaxLifetimeSeconds int64 `envconfig:"DB_CONN_MAX_LIFETIME_SECONDS" default:"0"`
MaxIdleConns int `envconfig:"DB_MAX_IDLE_CONNS" default:"0"`
MaxOpenConns int `envconfig:"DB_MAX_OPEN_CONNS" default:"0"`
}

func parseConfig() *Config {
return &Config{
DBDriver: "mysql",
DBUser: "root",
DBPassword: "password",
DBHost: "localhost",
DBName: "dittodining",
DBPort: "3306",
DBDriver: "mysql",
DBUser: "root",
DBPassword: "password",
DBHost: "localhost",
DBName: "dittodining",
DBPort: "3306",
ConnMaxLifetimeSeconds: 1800,
MaxIdleConns: 10, // TODO: DB 스펙에 따라 적절치로 조정
MaxOpenConns: 10,
}
}
29 changes: 26 additions & 3 deletions pkg/fx/gormfx/gormfx.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package gormfx

import (
"context"
"fmt"
"go.uber.org/fx"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"time"
)

type Params struct {
Expand All @@ -22,7 +24,6 @@ type Result struct {
var Module = fx.Module("gorm",
fx.Provide(parseConfig),
fx.Provide(New),
fx.Invoke(connectDB),
)

func New(lc fx.Lifecycle, p Params) (Result, error) {
Expand All @@ -45,7 +46,29 @@ func New(lc fx.Lifecycle, p Params) (Result, error) {
return Result{}, err
}

lc.Append(
fx.Hook{
OnStart: func(ctx context.Context) error {
sqlDB, err := db.DB()
if err != nil {
return err
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

if err := sqlDB.PingContext(ctx); err != nil {
return fmt.Errorf("database connection timeout: %w", err)
}

sqlDB.SetConnMaxLifetime(time.Duration(p.Config.ConnMaxLifetimeSeconds) * time.Second)
sqlDB.SetMaxIdleConns(p.Config.MaxIdleConns)
sqlDB.SetMaxOpenConns(p.Config.MaxOpenConns)

return nil
},
},
)

return Result{DB: db}, nil
}

func connectDB(db *gorm.DB) {}
8 changes: 5 additions & 3 deletions server/api/recommendation/recommendation_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (c *RecommendationAPIController) requestRestaurantRecommendation() fiber.Ha
}

result, err := c.restaurantRecommendationService.RequestRestaurantRecommendation(
ctx.UserContext(),
nil,
recommendation.UserLocation{
Latitude: (*request).UserLocation.Latitude.Decimal,
Expand Down Expand Up @@ -115,6 +116,7 @@ func (c *RecommendationAPIController) listRecommendedRestaurants() fiber.Handler
}

listRecommendedRestaurantsResult, err := c.restaurantRecommendationService.ListRecommendedRestaurants(
ctx.UserContext(),
int64(restaurantRecommendationRequestID),
cursorRestaurantRecommendationID,
lo.ToPtr(int64(limit)),
Expand Down Expand Up @@ -147,7 +149,7 @@ func (c *RecommendationAPIController) selectRestaurantRecommendations() fiber.Ha
}
}

if _, err := c.restaurantRecommendationService.SelectRestaurantRecommendation(int64(restaurantRecommendationRequestID), request.RestaurantRecommendationIDs); err != nil {
if _, err := c.restaurantRecommendationService.SelectRestaurantRecommendation(ctx.UserContext(), int64(restaurantRecommendationRequestID), request.RestaurantRecommendationIDs); err != nil {
return err
}

Expand All @@ -165,7 +167,7 @@ func (c *RecommendationAPIController) getRestaurantRecommendationResult() fiber.
}
}

result, err := c.restaurantRecommendationService.GetRestaurantRecommendationResult(int64(restaurantRecommendationRequestID))
result, err := c.restaurantRecommendationService.GetRestaurantRecommendationResult(ctx.UserContext(), int64(restaurantRecommendationRequestID))
if err != nil {
return err
}
Expand All @@ -186,7 +188,7 @@ func (c *RecommendationAPIController) getRestaurantRecommendation() fiber.Handle
}
}

result, err := c.restaurantRecommendationService.GetRestaurantRecommendation(int64(restaurantRecommendationID))
result, err := c.restaurantRecommendationService.GetRestaurantRecommendation(ctx.UserContext(), int64(restaurantRecommendationID))
if err != nil {
if errors.Is(err, restaurant_repository.ErrRestaurantNotFound) {
return &customerrors.ApplicationError{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package recommendation

import (
"context"
"github.com/BOAZ-LKVK/LKVK-server/server/domain/recommendation"
"github.com/pkg/errors"
"gorm.io/gorm"
Expand All @@ -9,21 +10,21 @@ import (
var ErrRestaurantRecommendationNotFound = errors.New("restaurant recommendation not found")

type RestaurantRecommendationRepository interface {
FindAllByRestaurantRecommendationRequestID(restaurantRecommendationRequestID int64, cursorRestaurantRecommendationRequestID *int64, limit *int64) ([]*recommendation.RestaurantRecommendation, error)
FindAllByIDs(restaurantRecommendationIDs []int64) ([]*recommendation.RestaurantRecommendation, error)
FindByID(restaurantRecommendationID int64) (*recommendation.RestaurantRecommendation, error)
SaveAll(recommendations []*recommendation.RestaurantRecommendation) error
FindAllByRestaurantRecommendationRequestID(ctx context.Context, db *gorm.DB, restaurantRecommendationRequestID int64, cursorRestaurantRecommendationRequestID *int64, limit *int64) ([]*recommendation.RestaurantRecommendation, error)
FindAllByIDs(ctx context.Context, db *gorm.DB, restaurantRecommendationIDs []int64) ([]*recommendation.RestaurantRecommendation, error)
FindByID(ctx context.Context, db *gorm.DB, restaurantRecommendationID int64) (*recommendation.RestaurantRecommendation, error)
SaveAll(ctx context.Context, db *gorm.DB, recommendations []*recommendation.RestaurantRecommendation) error
}

func NewRestaurantRecommendationRepository(db *gorm.DB) RestaurantRecommendationRepository {
return &restaurantRecommendationRepository{db: db}
func NewRestaurantRecommendationRepository() RestaurantRecommendationRepository {
return &restaurantRecommendationRepository{}
}

type restaurantRecommendationRepository struct {
db *gorm.DB
}
type restaurantRecommendationRepository struct{}

func (r *restaurantRecommendationRepository) FindAllByRestaurantRecommendationRequestID(
ctx context.Context,
db *gorm.DB,
restaurantRecommendationRequestID int64,
cursorRestaurantRecommendationID *int64,
limit *int64,
Expand All @@ -40,7 +41,7 @@ func (r *restaurantRecommendationRepository) FindAllByRestaurantRecommendationRe
limitQuery = int(*limit)
}

result := r.db.
result := db.
Where(
recommendation.RestaurantRecommendation{
RestaurantRecommendationRequestID: restaurantRecommendationRequestID,
Expand All @@ -56,10 +57,14 @@ func (r *restaurantRecommendationRepository) FindAllByRestaurantRecommendationRe
return recommendations, nil
}

func (r *restaurantRecommendationRepository) FindAllByIDs(restaurantRecommendationIDs []int64) ([]*recommendation.RestaurantRecommendation, error) {
func (r *restaurantRecommendationRepository) FindAllByIDs(
ctx context.Context,
db *gorm.DB,
restaurantRecommendationIDs []int64,
) ([]*recommendation.RestaurantRecommendation, error) {
var recommendations []*recommendation.RestaurantRecommendation

result := r.db.
result := db.
Where("restaurant_recommendation_id IN ?", restaurantRecommendationIDs).
Find(&recommendations)
if result.Error != nil {
Expand All @@ -69,10 +74,14 @@ func (r *restaurantRecommendationRepository) FindAllByIDs(restaurantRecommendati
return recommendations, nil
}

func (r *restaurantRecommendationRepository) FindByID(restaurantRecommendationID int64) (*recommendation.RestaurantRecommendation, error) {
func (r *restaurantRecommendationRepository) FindByID(
ctx context.Context,
db *gorm.DB,
restaurantRecommendationID int64,
) (*recommendation.RestaurantRecommendation, error) {
var existingRecommendation recommendation.RestaurantRecommendation

result := r.db.
result := db.
Where(&recommendation.RestaurantRecommendation{
RestaurantRecommendationID: restaurantRecommendationID,
}).
Expand All @@ -88,8 +97,12 @@ func (r *restaurantRecommendationRepository) FindByID(restaurantRecommendationID
return &existingRecommendation, nil
}

func (r *restaurantRecommendationRepository) SaveAll(recommendations []*recommendation.RestaurantRecommendation) error {
result := r.db.Save(recommendations)
func (r *restaurantRecommendationRepository) SaveAll(
ctx context.Context,
db *gorm.DB,
recommendations []*recommendation.RestaurantRecommendation,
) error {
result := db.Save(recommendations)
if result.Error != nil {
return result.Error
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package recommendation

import (
"context"
"errors"
"github.com/BOAZ-LKVK/LKVK-server/server/domain/recommendation"
"gorm.io/gorm"
Expand All @@ -9,28 +10,28 @@ import (
var ErrRestaurantRecommendationRequestNotFound = errors.New("restaurant recommendation request not found")

type RestaurantRecommendationRequestRepository interface {
Save(request *recommendation.RestaurantRecommendationRequest) (*recommendation.RestaurantRecommendationRequest, error)
FindByID(restaurantRecommendationRequestID int64) (*recommendation.RestaurantRecommendationRequest, error)
Save(ctx context.Context, db *gorm.DB, request *recommendation.RestaurantRecommendationRequest) (*recommendation.RestaurantRecommendationRequest, error)
FindByID(ctx context.Context, db *gorm.DB, restaurantRecommendationRequestID int64) (*recommendation.RestaurantRecommendationRequest, error)
}

func NewRestaurantRecommendationRequestRepository(db *gorm.DB) RestaurantRecommendationRequestRepository {
return &restaurantRecommendationRequestRepository{db: db}
func NewRestaurantRecommendationRequestRepository() RestaurantRecommendationRequestRepository {
return &restaurantRecommendationRequestRepository{}
}

type restaurantRecommendationRequestRepository struct {
db *gorm.DB
}

func (r *restaurantRecommendationRequestRepository) Save(request *recommendation.RestaurantRecommendationRequest) (*recommendation.RestaurantRecommendationRequest, error) {
result := r.db.Create(request)
func (r *restaurantRecommendationRequestRepository) Save(ctx context.Context, db *gorm.DB, request *recommendation.RestaurantRecommendationRequest) (*recommendation.RestaurantRecommendationRequest, error) {
result := db.Create(request)
if result.Error != nil {
return nil, result.Error
}

return request, nil
}

func (r *restaurantRecommendationRequestRepository) FindByID(restaurantRecommendationRequestID int64) (*recommendation.RestaurantRecommendationRequest, error) {
func (r *restaurantRecommendationRequestRepository) FindByID(ctx context.Context, db *gorm.DB, restaurantRecommendationRequestID int64) (*recommendation.RestaurantRecommendationRequest, error) {
var request *recommendation.RestaurantRecommendationRequest

result := r.db.Where(recommendation.RestaurantRecommendationRequest{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
package recommendation

import (
"context"
recommendation_domain "github.com/BOAZ-LKVK/LKVK-server/server/domain/recommendation"
"gorm.io/gorm"
)

type SelectedRestaurantRecommendationRepository interface {
SaveAll(selectedRestaurantRecommendations []*recommendation_domain.SelectedRestaurantRecommendation) error
FindAllByRestaurantRecommendationRequestID(restaurantRecommendationRequestID int64) ([]*recommendation_domain.SelectedRestaurantRecommendation, error)
SaveAll(ctx context.Context, db *gorm.DB, selectedRestaurantRecommendations []*recommendation_domain.SelectedRestaurantRecommendation) error
FindAllByRestaurantRecommendationRequestID(ctx context.Context, db *gorm.DB, restaurantRecommendationRequestID int64) ([]*recommendation_domain.SelectedRestaurantRecommendation, error)
}

func NewSelectedRestaurantRecommendationRepository(db *gorm.DB) SelectedRestaurantRecommendationRepository {
return &selectedRestaurantRecommendationRepository{db: db}
func NewSelectedRestaurantRecommendationRepository() SelectedRestaurantRecommendationRepository {
return &selectedRestaurantRecommendationRepository{}
}

type selectedRestaurantRecommendationRepository struct {
db *gorm.DB
}

func (r *selectedRestaurantRecommendationRepository) SaveAll(selectedRestaurantRecommendations []*recommendation_domain.SelectedRestaurantRecommendation) error {
result := r.db.Save(selectedRestaurantRecommendations)
func (r *selectedRestaurantRecommendationRepository) SaveAll(ctx context.Context, db *gorm.DB, selectedRestaurantRecommendations []*recommendation_domain.SelectedRestaurantRecommendation) error {
result := db.Save(selectedRestaurantRecommendations)
if result.Error != nil {
return result.Error
}

return nil
}

func (r *selectedRestaurantRecommendationRepository) FindAllByRestaurantRecommendationRequestID(restaurantRecommendationRequestID int64) ([]*recommendation_domain.SelectedRestaurantRecommendation, error) {
func (r *selectedRestaurantRecommendationRepository) FindAllByRestaurantRecommendationRequestID(ctx context.Context, db *gorm.DB, restaurantRecommendationRequestID int64) ([]*recommendation_domain.SelectedRestaurantRecommendation, error) {
var selectedRestaurantRecommendations []*recommendation_domain.SelectedRestaurantRecommendation
result := r.db.
result := db.
Where(recommendation_domain.SelectedRestaurantRecommendation{
RestaurantRecommendationRequestID: restaurantRecommendationRequestID,
}).
Expand Down
23 changes: 10 additions & 13 deletions server/repository/restaurant/restaurant_image_repository.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
package restaurant

import (
"context"
"github.com/BOAZ-LKVK/LKVK-server/server/domain/restaurant"
"gorm.io/gorm"
)

type RestaurantImageRepository interface {
FindAllByRestaurantID(restaurantID int64) ([]*restaurant.RestaurantImage, error)
FindAllByRestaurantIDs(restaurantIDs []int64) ([]*restaurant.RestaurantImage, error)
FindAllByRestaurantID(ctx context.Context, db *gorm.DB, restaurantID int64) ([]*restaurant.RestaurantImage, error)
FindAllByRestaurantIDs(ctx context.Context, db *gorm.DB, restaurantIDs []int64) ([]*restaurant.RestaurantImage, error)
}

func NewRestaurantImageRepository(db *gorm.DB) RestaurantImageRepository {
return &restaurantImageRepository{
db: db,
}
func NewRestaurantImageRepository() RestaurantImageRepository {
return &restaurantImageRepository{}
}

type restaurantImageRepository struct {
db *gorm.DB
}
type restaurantImageRepository struct{}

func (r *restaurantImageRepository) FindAllByRestaurantID(restaurantID int64) ([]*restaurant.RestaurantImage, error) {
func (r *restaurantImageRepository) FindAllByRestaurantID(ctx context.Context, db *gorm.DB, restaurantID int64) ([]*restaurant.RestaurantImage, error) {
var images []*restaurant.RestaurantImage
result := r.db.
result := db.
Where(restaurant.RestaurantImage{
RestaurantID: restaurantID,
}).
Expand All @@ -34,9 +31,9 @@ func (r *restaurantImageRepository) FindAllByRestaurantID(restaurantID int64) ([
return images, nil
}

func (r *restaurantImageRepository) FindAllByRestaurantIDs(restaurantIDs []int64) ([]*restaurant.RestaurantImage, error) {
func (r *restaurantImageRepository) FindAllByRestaurantIDs(ctx context.Context, db *gorm.DB, restaurantIDs []int64) ([]*restaurant.RestaurantImage, error) {
var images []*restaurant.RestaurantImage
result := r.db.
result := db.
Where("restaurant_id IN ?", restaurantIDs).
Find(&images)
if result.Error != nil {
Expand Down
Loading