repository와 subscribe까지 잘 됨

This commit is contained in:
2025-12-08 21:57:18 +09:00
parent 619cdc7d6a
commit 94fae7c4fd
13 changed files with 336 additions and 11 deletions

View File

@@ -0,0 +1,7 @@
package flow
import "errors"
var (
ErrWeatherAlreadyExists = errors.New("weather already exists")
)

47
internal/app/flow/flow.go Normal file
View File

@@ -0,0 +1,47 @@
package flow
import (
"context"
"errors"
"fmt"
"github.com/neatflowcv/seven-skies/internal/pkg/domain"
"github.com/neatflowcv/seven-skies/internal/pkg/repository"
"github.com/oklog/ulid/v2"
)
type Flow struct {
repo repository.Repository
}
func NewFlow(repo repository.Repository) *Flow {
return &Flow{
repo: repo,
}
}
func (f *Flow) CreateWeather(ctx context.Context, weather *Weather) error {
id := ulid.Make().String()
domainWeather := domain.NewWeather(
id,
domain.WeatherSource(weather.Source),
weather.TargetDate,
weather.ForecastDate,
domain.WeatherCondition(weather.Condition),
domain.Temperature{
Value: domain.Celsius(weather.Temperature),
},
)
err := f.repo.CreateWeather(ctx, domainWeather)
if err != nil {
if errors.Is(err, repository.ErrWeatherAlreadyExists) {
return ErrWeatherAlreadyExists
}
return fmt.Errorf("error in create weather: %w", err)
}
return nil
}

View File

@@ -1,7 +0,0 @@
package flow
type Service struct {}
func NewService() *Service {
return &Service{}
}

View File

@@ -0,0 +1,11 @@
package flow
import "time"
type Weather struct {
Source string
TargetDate time.Time
ForecastDate time.Time
Condition string
Temperature float64 // Celsius
}

View File

@@ -0,0 +1,54 @@
package domain
import "time"
type Weather struct {
id string
source WeatherSource
targetDate time.Time
forecastDate time.Time
condition WeatherCondition
temperature Temperature
}
func NewWeather(
id string,
source WeatherSource,
targetDate time.Time,
forecastDate time.Time,
condition WeatherCondition,
temperature Temperature,
) *Weather {
return &Weather{
id: id,
source: source,
targetDate: targetDate,
forecastDate: forecastDate,
condition: condition,
temperature: temperature,
}
}
func (w *Weather) ID() string {
return w.id
}
func (w *Weather) Source() WeatherSource {
return w.source
}
func (w *Weather) TargetDate() time.Time {
return w.targetDate
}
func (w *Weather) ForecastDate() time.Time {
return w.forecastDate
}
func (w *Weather) Condition() WeatherCondition {
return w.condition
}
func (w *Weather) Temperature() Temperature {
return w.temperature
}

View File

@@ -0,0 +1,8 @@
package repository
import "errors"
var (
ErrWeatherAlreadyExists = errors.New("weather already exists")
ErrWeatherNotFound = errors.New("weather not found")
)

View 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
}

View 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),
}
}

View File

@@ -0,0 +1,11 @@
package repository
import (
"context"
"github.com/neatflowcv/seven-skies/internal/pkg/domain"
)
type Repository interface {
CreateWeather(ctx context.Context, weather *domain.Weather) error
}