clean: first clean archi sandbox implementation

This commit is contained in:
Florian Sylvain
2023-08-05 14:37:53 +02:00
parent 4187f0b2e2
commit 5b4b5a8622
13 changed files with 302 additions and 1 deletions
+4 -1
View File
@@ -1,3 +1,6 @@
# IDE # IDE
.idea .idea
.vscode .vscode
# DB
*.db
@@ -0,0 +1,48 @@
package gateways
import (
"GohCMS2/db"
. "GohCMS2/domain/article"
. "GohCMS2/domain/gateways"
"context"
)
type ArticleRepository struct {
client *db.PrismaClient
}
func NewArticleRepository() *ArticleRepository {
a := ArticleRepository{
client: db.NewClient(),
}
return &a
}
func (a *ArticleRepository) Get(id int) Article {
a.client.Connect()
defer a.client.Disconnect()
article, err := a.client.Article.FindUnique(db.Article.ID.Equals(id)).Exec(context.Background())
if err != nil {
panic(err)
}
return FromDb(article.ID, article.Title, article.Body, article.CreatedAt.String(), article.UpdatedAt.String())
}
func (a *ArticleRepository) Create(article Article) Article {
a.client.Connect()
defer a.client.Disconnect()
articleDb, err := a.client.Article.CreateOne(
db.Article.Title.Set(article.Title),
db.Article.Body.Set(article.Body),
).Exec(context.Background())
if err != nil {
panic(err)
}
return FromDb(articleDb.ID, articleDb.Title, articleDb.Body, articleDb.CreatedAt.String(), articleDb.UpdatedAt.String())
}
var _ IArticleRepository = &ArticleRepository{}
+14
View File
@@ -0,0 +1,14 @@
package api
import (
. "GohCMS2/domain/article"
. "GohCMS2/useCases"
)
func GetArticle(id int) Article {
return container.GetArticleUseCase.GetArticle(id)
}
func CreateArticle(title string, body string) Article {
return container.CreateArticleUseCase.CreateAarticle(CreateArticleCommand{Title: title, Body: body})
}
+41
View File
@@ -0,0 +1,41 @@
package api
import (
. "GohCMS2/useCases"
"errors"
"go.uber.org/dig"
)
type Container struct {
CreateArticleUseCase *CreateArticleUseCase
GetArticleUseCase *GetArticleUseCase
}
var container *Container
func InitContainer() *Container {
if container != nil {
return container
}
digContainer := dig.New()
err1 := digContainer.Provide(NewCreateArticleUseCase)
err2 := digContainer.Provide(NewGetArticleUseCase)
if err := errors.Join(err1, err2); err != nil {
panic(err)
}
err := digContainer.Invoke(func(createArticle *CreateArticleUseCase, getArticle *GetArticleUseCase) {
container = &Container{
CreateArticleUseCase: createArticle,
GetArticleUseCase: getArticle,
}
})
if err != nil {
panic(err)
}
return container
}
+13
View File
@@ -0,0 +1,13 @@
package main
import (
"GohCMS2/api"
"fmt"
)
func main() {
api.InitContainer()
fmt.Println(api.CreateArticle("Title", "Content"))
fmt.Println(api.GetArticle(1))
}
+2
View File
@@ -0,0 +1,2 @@
# gitignore generated by Prisma Client Go. DO NOT EDIT.
*_gen.go
+35
View File
@@ -0,0 +1,35 @@
package article
type Article struct {
Id int
Title string
Body string
createdAt string
updatedAt string
}
func FromApi(
title string,
body string,
) Article {
return Article{
Title: title,
Body: body,
}
}
func FromDb(
id int,
title string,
body string,
createdAt string,
updatedAt string,
) Article {
return Article{
Id: id,
Title: title,
Body: body,
createdAt: createdAt,
updatedAt: updatedAt,
}
}
+10
View File
@@ -0,0 +1,10 @@
package gateways
import (
. "GohCMS2/domain/article"
)
type IArticleRepository interface {
Get(id int) Article
Create(article Article) Article
}
+14
View File
@@ -0,0 +1,14 @@
module GohCMS2
go 1.20
require (
github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365
github.com/joho/godotenv v1.5.1
github.com/shopspring/decimal v1.3.1
github.com/steebchen/prisma-client-go v0.21.0
github.com/takuoki/gocase v1.0.0
golang.org/x/text v0.10.0
)
require go.uber.org/dig v1.17.0
+60
View File
@@ -0,0 +1,60 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365 h1:ECW73yc9MY7935nNYXUkK7Dz17YuSUI9yqRqYS8aBww=
github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/steebchen/prisma-client-go v0.21.0 h1:mWBW4eDbKKdLJ8ET2kj0U8myDIVEPuZFRT7En7Yf7YU=
github.com/steebchen/prisma-client-go v0.21.0/go.mod h1:3VhO5OCbQEAUH64bm15HobZMBVqgU93T/uIVRN0+wrM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/takuoki/gocase v1.0.0 h1:gPwLJTWVm2T1kUiCsKirg/faaIUGVTI0FA3SYr75a44=
github.com/takuoki/gocase v1.0.0/go.mod h1:QgOKJrbuJoDrtoKswBX1/Dw8mJrkOV9tbQZJaxaJ6zc=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.uber.org/dig v1.17.0 h1:5Chju+tUvcC+N7N6EV08BJz41UZuO3BmHcN4A287ZLI=
go.uber.org/dig v1.17.0/go.mod h1:rTxpf7l5I0eBTlE6/9RL+lDybC7WFwY2QH55ZSjy1mU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58=
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+16
View File
@@ -0,0 +1,16 @@
datasource db {
provider = "sqlite"
url = "file:dev.db"
}
generator db {
provider = "go run github.com/steebchen/prisma-client-go"
}
model Article {
id Int @id @default(autoincrement())
title String
body String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
+25
View File
@@ -0,0 +1,25 @@
package useCases
import (
. "GohCMS2/adapters/secondary/gateways"
. "GohCMS2/domain/article"
)
type CreateArticleUseCase struct {
articleRepository ArticleRepository
}
type CreateArticleCommand struct {
Title string
Body string
}
func NewCreateArticleUseCase() *CreateArticleUseCase {
return &CreateArticleUseCase{
articleRepository: *NewArticleRepository(),
}
}
func (g *CreateArticleUseCase) CreateAarticle(article CreateArticleCommand) Article {
return g.articleRepository.Create(FromApi(article.Title, article.Body))
}
+20
View File
@@ -0,0 +1,20 @@
package useCases
import (
. "GohCMS2/adapters/secondary/gateways"
. "GohCMS2/domain/article"
)
type GetArticleUseCase struct {
articleRepository ArticleRepository
}
func NewGetArticleUseCase() *GetArticleUseCase {
return &GetArticleUseCase{
articleRepository: *NewArticleRepository(),
}
}
func (g *GetArticleUseCase) GetArticle(id int) Article {
return g.articleRepository.Get(id)
}