repository와 subscribe까지 잘 됨
This commit is contained in:
44
internal/pkg/repository/gorm/repository.go
Normal file
44
internal/pkg/repository/gorm/repository.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package gorm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/neatflowcv/seven-skies/internal/pkg/domain"
|
||||
"github.com/neatflowcv/seven-skies/internal/pkg/repository"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var _ repository.Repository = (*Repository)(nil)
|
||||
|
||||
type Repository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewRepository(dsn string) (*Repository, error) {
|
||||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) //nolint:exhaustruct
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open database: %w", err)
|
||||
}
|
||||
|
||||
err = db.AutoMigrate(&Weather{}) //nolint:exhaustruct
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to migrate database: %w", err)
|
||||
}
|
||||
|
||||
return &Repository{
|
||||
db: db,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *Repository) CreateWeather(ctx context.Context, weather *domain.Weather) error {
|
||||
modelWeather := newModelWeather(weather)
|
||||
|
||||
err := gorm.G[Weather](r.db).Create(ctx, modelWeather)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create weather: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
27
internal/pkg/repository/gorm/weather.go
Normal file
27
internal/pkg/repository/gorm/weather.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package gorm
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/neatflowcv/seven-skies/internal/pkg/domain"
|
||||
)
|
||||
|
||||
type Weather struct {
|
||||
ID string `gorm:"primaryKey"`
|
||||
Source string
|
||||
TargetDate time.Time
|
||||
ForecastDate time.Time
|
||||
Condition string
|
||||
Temperature float64
|
||||
}
|
||||
|
||||
func newModelWeather(weather *domain.Weather) *Weather {
|
||||
return &Weather{
|
||||
ID: weather.ID(),
|
||||
Source: string(weather.Source()),
|
||||
TargetDate: weather.TargetDate(),
|
||||
ForecastDate: weather.ForecastDate(),
|
||||
Condition: string(weather.Condition()),
|
||||
Temperature: float64(weather.Temperature().Value),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user