Last active
December 13, 2017 03:02
-
-
Save uzimith/bae22e85b57d470d5754787ef6b8f73b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"ghe.ca-tools.org/leicester/auction.git/rdb" | |
"github.com/facebookgo/inject" | |
_ "github.com/go-sql-driver/mysql" | |
"github.com/go-xorm/xorm" | |
) | |
// Repository | |
type AdSpotRepository interface { | |
FetchAdSpots() []rdb.AdSpot | |
} | |
type AdSpotRepositoryImpl struct { | |
DB *xorm.Engine `inject:""` | |
} | |
func (s *AdSpotRepositoryImpl) FetchAdSpots() []rdb.AdSpot { | |
adSpots := []rdb.AdSpot{} | |
_ = s.DB.Table("ad_spot").Find(&adSpots) | |
return adSpots | |
} | |
// - | |
type AdFormatRepository interface { | |
FetchAdFormats() []rdb.AdFormat | |
} | |
type AdFormatRepositoryImpl struct { | |
DB *xorm.Engine `inject:""` | |
} | |
func (s *AdFormatRepositoryImpl) FetchAdFormats() []rdb.AdFormat { | |
adFormats := []rdb.AdFormat{} | |
_ = s.DB.Table("ad_format").Find(&adFormats) | |
return adFormats | |
} | |
// Service | |
type AdSpotService interface { | |
GetAdSpots() []rdb.AdSpot | |
} | |
type AdSpotServiceImpl struct { | |
AdSpotRepository AdSpotRepository `inject:""` | |
} | |
func (s *AdSpotServiceImpl) GetAdSpots() []rdb.AdSpot { | |
return s.AdSpotRepository.FetchAdSpots() | |
} | |
// - | |
type AdFormatService interface { | |
GetAdFormats() []rdb.AdFormat | |
} | |
type AdFormatServiceImpl struct { | |
AdFormatRepository AdFormatRepository `inject:""` | |
} | |
func (s *AdFormatServiceImpl) GetAdFormats() []rdb.AdFormat { | |
return s.AdFormatRepository.FetchAdFormats() | |
} | |
// nanka | |
type App struct { | |
AdSpotService AdSpotService `inject:""` | |
AdFormatService AdFormatService `inject:""` | |
} | |
func (a *App) Run() { | |
fmt.Printf("%+v\n", a.AdSpotService.GetAdSpots()) | |
fmt.Printf("%+v\n", a.AdFormatService.GetAdFormats()) | |
} | |
func main() { | |
app := &App{} | |
adSpotService := &AdSpotServiceImpl{} | |
adSpotRepository := &AdSpotRepositoryImpl{} | |
adFormatService := &AdFormatServiceImpl{} | |
adFormatRepository := &AdFormatRepositoryImpl{} | |
engine, err := xorm.NewEngine("mysql", "root@tcp(localhost:4306)/leicester_local?parseTime=true&loc=Local") | |
if err != nil { | |
panic(err) | |
} | |
engine.ShowSQL(true) | |
if err := inject.Populate( | |
app, | |
adSpotService, | |
adSpotRepository, | |
adFormatService, | |
adFormatRepository, | |
engine, | |
); err != nil { | |
fmt.Fprintln(os.Stderr, err) | |
os.Exit(1) | |
} | |
app.Run() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment