diff --git a/README.md b/README.md index 284bf38..59bec54 100644 --- a/README.md +++ b/README.md @@ -9,23 +9,16 @@ An example Go application demonstrating The Clean Architecture. Install ------- -Create a new directory, where to host this project - - mkdir -p $GOPATH:src/github.com/manuelkiessling/ - Check out the source - cd $GOPATH:src/github.com/manuelkiessling/ - git clone https://github.com/manuelkiessling/go-cleanarchitecture + git clone https://github.com/manuelkiessling/go-cleanarchitecture && cd go-cleanarchitecture -Setup the GOPATH to include this path +Download modules - cd go-cleanarchitecture - export GOPATH=$GOPATH:`pwd` + go mod download Then build the project - go get go build Create the SQLite structure @@ -34,14 +27,13 @@ Create the SQLite structure Run the server - go-cleanarchitecture + ./go-cleanarchitecture Access the web endpoint at [http://localhost:8080/orders?userId=40&orderId=60](http://localhost:8080/orders?userId=40&orderId=60) To run the tests, for each module, run - cd src/infrastructure && go test - cd src/interfaces && go test + go test ./... Enjoy. diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8a759d5 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/manuelkiessling/go-cleanarchitecture + +go 1.16 + +require github.com/mattn/go-sqlite3 v1.14.8 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..0293daa --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/mattn/go-sqlite3 v1.14.8 h1:gDp86IdQsN/xWjIEmr9MF6o9mpksUgh0fu+9ByFxzIU= +github.com/mattn/go-sqlite3 v1.14.8/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= diff --git a/main.go b/main.go index b40fe56..3fe6160 100644 --- a/main.go +++ b/main.go @@ -1,16 +1,17 @@ package main import ( - "usecases" - "interfaces" - "infrastructure" "net/http" + + "github.com/manuelkiessling/go-cleanarchitecture/src/infrastructure" + "github.com/manuelkiessling/go-cleanarchitecture/src/interfaces" + "github.com/manuelkiessling/go-cleanarchitecture/src/usecases" ) func main() { dbHandler := infrastructure.NewSqliteHandler("/var/tmp/production.sqlite") - handlers := make(map[string] interfaces.DbHandler) + handlers := make(map[string]interfaces.DbHandler) handlers["DbUserRepo"] = dbHandler handlers["DbCustomerRepo"] = dbHandler handlers["DbItemRepo"] = dbHandler diff --git a/src/infrastructure/sqlitehandler.go b/src/infrastructure/sqlitehandler.go index 3bda120..c5b32e7 100644 --- a/src/infrastructure/sqlitehandler.go +++ b/src/infrastructure/sqlitehandler.go @@ -3,8 +3,9 @@ package infrastructure import ( "database/sql" "fmt" + + "github.com/manuelkiessling/go-cleanarchitecture/src/interfaces" _ "github.com/mattn/go-sqlite3" - "interfaces" ) type SqliteHandler struct { @@ -16,7 +17,7 @@ func (handler *SqliteHandler) Execute(statement string) { } func (handler *SqliteHandler) Query(statement string) interfaces.Row { - //fmt.Println(statement) + // fmt.Println(statement) rows, err := handler.Conn.Query(statement) if err != nil { fmt.Println(err) @@ -40,7 +41,10 @@ func (r SqliteRow) Next() bool { } func NewSqliteHandler(dbfileName string) *SqliteHandler { - conn, _ := sql.Open("sqlite3", dbfileName) + conn, err := sql.Open("sqlite3", dbfileName) + if err != nil { + panic(err) + } sqliteHandler := new(SqliteHandler) sqliteHandler.Conn = conn return sqliteHandler diff --git a/src/interfaces/repositories.go b/src/interfaces/repositories.go index 5fa0d19..51435af 100644 --- a/src/interfaces/repositories.go +++ b/src/interfaces/repositories.go @@ -1,9 +1,10 @@ package interfaces import ( - "domain" "fmt" - "usecases" + + "github.com/manuelkiessling/go-cleanarchitecture/src/domain" + "github.com/manuelkiessling/go-cleanarchitecture/src/usecases" ) type DbHandler interface { diff --git a/src/interfaces/repositories_test.go b/src/interfaces/repositories_test.go index 119f1e0..4e03205 100644 --- a/src/interfaces/repositories_test.go +++ b/src/interfaces/repositories_test.go @@ -1,12 +1,12 @@ package interfaces_test import ( - "domain" - _ "fmt" - "infrastructure" - "interfaces" "testing" - "usecases" + + "github.com/manuelkiessling/go-cleanarchitecture/src/domain" + "github.com/manuelkiessling/go-cleanarchitecture/src/infrastructure" + "github.com/manuelkiessling/go-cleanarchitecture/src/interfaces" + "github.com/manuelkiessling/go-cleanarchitecture/src/usecases" ) func Test_UserRepository(t *testing.T) { @@ -103,7 +103,7 @@ func Test_OrderRepository(t *testing.T) { o.Id = 39 o.Customer = c o.Add(i1) - o.Add(i2) //fails because it's not available + o.Add(i2) // fails because it's not available o.Add(i3) or.Store(o) diff --git a/src/interfaces/webservice.go b/src/interfaces/webservice.go index c7da6ec..bf06794 100644 --- a/src/interfaces/webservice.go +++ b/src/interfaces/webservice.go @@ -5,7 +5,8 @@ import ( "io" "net/http" "strconv" - "usecases" + + "github.com/manuelkiessling/go-cleanarchitecture/src/usecases" ) type OrderInteractor interface { @@ -18,9 +19,18 @@ type WebserviceHandler struct { } func (handler WebserviceHandler) ShowOrder(res http.ResponseWriter, req *http.Request) { - userId, _ := strconv.Atoi(req.FormValue("userId")) - orderId, _ := strconv.Atoi(req.FormValue("orderId")) - items, _ := handler.OrderInteractor.Items(userId, orderId) + userId, err := strconv.Atoi(req.FormValue("userId")) + if err != nil { + panic(err) + } + orderId, err := strconv.Atoi(req.FormValue("orderId")) + if err != nil { + panic(err) + } + items, err := handler.OrderInteractor.Items(userId, orderId) + if err != nil { + panic(err) + } for _, item := range items { io.WriteString(res, fmt.Sprintf("item id: %d\n", item.Id)) io.WriteString(res, fmt.Sprintf("item name: %v\n", item.Name)) diff --git a/src/usecases/usecases.go b/src/usecases/usecases.go index 685c9da..17288ab 100644 --- a/src/usecases/usecases.go +++ b/src/usecases/usecases.go @@ -1,8 +1,9 @@ package usecases import ( - "domain" "fmt" + + "github.com/manuelkiessling/go-cleanarchitecture/src/domain" ) type UserRepository interface {