Basic DI (dependency injection) working! Yay!!

This commit is contained in:
Deluan 2016-03-01 09:38:43 -05:00
parent 4efb8ab031
commit 4df6c43be8
5 changed files with 68 additions and 0 deletions

23
api/get_indexes.go Normal file
View file

@ -0,0 +1,23 @@
package api
import (
"github.com/astaxie/beego"
"github.com/deluan/gosonic/repositories"
"github.com/deluan/gosonic/utils"
"github.com/karlkfi/inject"
)
type GetIndexesController struct {
beego.Controller
repo repositories.ArtistIndex
}
func (c *GetIndexesController) Prepare() {
inject.ExtractAssignable(utils.Graph, &c.repo)
}
func (c *GetIndexesController) Get() {
if c.repo == nil {
c.CustomAbort(500, "INJECTION NOT WORKING")
}
}

20
api/get_indexes_test.go Normal file
View file

@ -0,0 +1,20 @@
package api_test
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
"github.com/deluan/gosonic/tests"
)
func TestGetIndexes(t *testing.T) {
tests.Init(t, false)
_, w := Get(AddParams("/rest/getIndexes.view"), "TestGetIndexes")
Convey("Subject: GetIndexes Endpoint", t, func() {
Convey("Status code should be 200", func() {
So(w.Code, ShouldEqual, 200)
})
})
}

View file

@ -0,0 +1,15 @@
package conf
import (
"github.com/deluan/gosonic/repositories"
"github.com/deluan/gosonic/utils"
"github.com/karlkfi/inject"
)
var (
indexRepository repositories.ArtistIndex
)
func init () {
utils.Graph.Define(&indexRepository, inject.NewProvider(repositories.NewArtistIndexRepository))
}

View file

@ -19,6 +19,7 @@ func mapEndpoints() {
beego.NSRouter("/ping.view", &api.PingController{}, "*:Get"),
beego.NSRouter("/getLicense.view", &api.GetLicenseController{}, "*:Get"),
beego.NSRouter("/getMusicFolders.view", &api.GetMusicFoldersController{}, "*:Get"),
beego.NSRouter("/getIndexes.view", &api.GetIndexesController{}, "*:Get"),
)
beego.AddNamespace(ns)

9
utils/graph.go Normal file
View file

@ -0,0 +1,9 @@
package utils
import "github.com/karlkfi/inject"
var Graph inject.Graph
func init() {
Graph = inject.NewGraph()
}