diff --git a/adapters/secondary/gateways/articleRepository.go b/adapters/secondary/gateways/articleRepository.go index fd2a7f6..b14dd7d 100644 --- a/adapters/secondary/gateways/articleRepository.go +++ b/adapters/secondary/gateways/articleRepository.go @@ -1,77 +1,57 @@ package gateways import ( - "GohCMS2/db" - . "GohCMS2/domain/article" + entity "GohCMS2/adapters/secondary/gateways/models" + domain "GohCMS2/domain/article" . "GohCMS2/domain/gateways" - "context" + "gorm.io/gorm" ) type ArticleRepository struct { - client *db.PrismaClient + db *gorm.DB } -func NewArticleRepository() *ArticleRepository { - a := ArticleRepository{ - client: db.NewClient(), - } +func NewArticleRepository(db *gorm.DB) *ArticleRepository { + a := ArticleRepository{db} return &a } -func (a *ArticleRepository) connectClient() { - err := a.client.Connect() +func (a *ArticleRepository) Get(id uint32) (domain.Article, error) { + var article domain.Article + err := a.db.Model(&entity.Article{}).First(&article, id).Error if err != nil { - panic(err) + return article, err } + + return domain.FromDb(article.ID, article.Title, article.Body, article.CreatedAt, article.UpdatedAt), nil } -func (a *ArticleRepository) disconnectClient() { - err := a.client.Disconnect() - if err != nil { - panic(err) +func (a *ArticleRepository) Create(article domain.Article) (domain.Article, error) { + creationResult := a.db.Create(&entity.Article{ + Title: article.Title, + Body: article.Body, + }) + if creationResult.Error != nil { + return domain.Article{}, creationResult.Error } - a.client = db.NewClient() + + var createdArticle entity.Article + creationResult.Scan(&createdArticle) + + return domain.FromDb( + createdArticle.ID, + createdArticle.Title, + createdArticle.Body, + createdArticle.CreatedAt, + createdArticle.UpdatedAt), + nil } -func (a *ArticleRepository) Get(id int) Article { - a.connectClient() - defer a.disconnectClient() - - article, err := a.client.Article.FindUnique(db.Article.ID.Equals(id)).Exec(context.Background()) +func (a *ArticleRepository) GetAll() []domain.Article { + var articles []domain.Article + err := a.db.Model(&entity.Article{}).Find(&articles).Error 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.connectClient() - defer a.disconnectClient() - - 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()) -} - -func (a *ArticleRepository) GetAll() []Article { - a.connectClient() - defer a.disconnectClient() - - articlesDb, err := a.client.Article.FindMany().Exec(context.Background()) - if err != nil { - panic(err) - } - - var articles []Article - for _, articleDb := range articlesDb { - articles = append(articles, FromDb(articleDb.ID, articleDb.Title, articleDb.Body, articleDb.CreatedAt.String(), articleDb.UpdatedAt.String())) + return []domain.Article{} } return articles diff --git a/adapters/secondary/gateways/models/article.go b/adapters/secondary/gateways/models/article.go new file mode 100644 index 0000000..1c22d92 --- /dev/null +++ b/adapters/secondary/gateways/models/article.go @@ -0,0 +1,15 @@ +package models + +import ( + "gorm.io/gorm" + "time" +) + +type Article struct { + gorm.Model + ID uint32 `gorm:"primary_key;auto_increment;not_null"` + Title string + Body string + CreatedAt time.Time `gorm:"autoCreateTime"` + UpdatedAt time.Time `gorm:"autoUpdateTime"` +} diff --git a/api/article.go b/api/article.go index 82b29bf..b8c5914 100644 --- a/api/article.go +++ b/api/article.go @@ -10,19 +10,19 @@ import ( ) func getArticle(w http.ResponseWriter, r *http.Request) { - id, err := strconv.Atoi(chi.URLParam(r, "id")) + id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 32) if err != nil { - http.Error(w, err.Error(), http.StatusNotFound) + http.Error(w, "The server expects the ID to be in the format of an unsigned 32-bit integer (uint32).", http.StatusBadRequest) return } - article := container.GetArticleUseCase.GetArticle(id) - articleJson, err := json.Marshal(article) + article, err := container.GetArticleUseCase.GetArticle(uint32(id)) if err != nil { - http.Error(w, err.Error(), http.StatusNotFound) + http.Error(w, "The requested resource, identified by its unique ID, could not be found on the server.", http.StatusNotFound) return } + articleJson, _ := json.Marshal(article) _, _ = w.Write(articleJson) } @@ -30,14 +30,18 @@ func postArticle(w http.ResponseWriter, r *http.Request) { var article Article err := json.NewDecoder(r.Body).Decode(&article) if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) + http.Error(w, "The request cannot be processed due to a mismatch in the format of the body.", http.StatusBadRequest) return } - createdArticle := container.CreateArticleUseCase.CreateAarticle(CreateArticleCommand{ + createdArticle, err := container.CreateArticleUseCase.CreateArticle(CreateArticleCommand{ Title: article.Title, Body: article.Body, }) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } articleJson, _ := json.Marshal(createdArticle) _, _ = w.Write(articleJson) diff --git a/api/dependecyInjection.go b/api/dependecyInjection.go index b6ea891..b32f647 100644 --- a/api/dependecyInjection.go +++ b/api/dependecyInjection.go @@ -1,8 +1,11 @@ package api import ( + "GohCMS2/adapters/secondary/gateways/models" . "GohCMS2/useCases" + "github.com/glebarez/sqlite" "go.uber.org/dig" + "gorm.io/gorm" ) type Container struct { @@ -13,7 +16,7 @@ type Container struct { var container *Container -func createContainer( +func setContainer( createArticle *CreateArticleUseCase, getArticle *GetArticleUseCase, listArticle *ListArticlesUseCase, @@ -33,9 +36,17 @@ func InitContainer() { digContainer := dig.New() - _ = digContainer.Provide(NewCreateArticleUseCase) - _ = digContainer.Provide(NewGetArticleUseCase) - _ = digContainer.Provide(NewListArticlesUseCase) + db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{}) + if err != nil { + panic(err) + } + _ = db.AutoMigrate(&models.Article{}) - _ = digContainer.Invoke(createContainer) + _ = digContainer.Provide(func() *gorm.DB { return db }) + + _ = digContainer.Provide(func(db *gorm.DB) *CreateArticleUseCase { return NewCreateArticleUseCase(db) }) + _ = digContainer.Provide(func(db *gorm.DB) *GetArticleUseCase { return NewGetArticleUseCase(db) }) + _ = digContainer.Provide(func(db *gorm.DB) *ListArticlesUseCase { return NewListArticlesUseCase(db) }) + + _ = digContainer.Invoke(setContainer) } diff --git a/cmd/main.go b/cmd/main.go index 935ce51..f29df75 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -9,6 +9,13 @@ import ( "net/http" ) +func jsonContentTypeMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + next.ServeHTTP(w, r) + }) +} + func main() { api.InitContainer() @@ -21,6 +28,7 @@ func main() { apiRouter := chi.NewRouter() apiRouter.Use(httplog.RequestLogger(httplog.NewLogger("GohCMS2"))) + apiRouter.Use(jsonContentTypeMiddleware) apiRouter.Mount("/v1", r) fmt.Println("Server starting on port 8080") diff --git a/db/.gitignore b/db/.gitignore deleted file mode 100644 index a0c7514..0000000 --- a/db/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -# gitignore generated by Prisma Client Go. DO NOT EDIT. -*_gen.go diff --git a/domain/article/article.go b/domain/article/article.go index b932822..184b76a 100644 --- a/domain/article/article.go +++ b/domain/article/article.go @@ -1,11 +1,13 @@ package article +import "time" + type Article struct { - Id int `json:"id"` - Title string `json:"title"` - Body string `json:"body"` - CreatedAt string `json:"created_at"` - UpdatedAt string `json:"updated_at"` + ID uint32 `json:"id"` + Title string `json:"title"` + Body string `json:"body"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` } func FromApi( @@ -19,14 +21,14 @@ func FromApi( } func FromDb( - id int, + id uint32, title string, body string, - createdAt string, - updatedAt string, + createdAt time.Time, + updatedAt time.Time, ) Article { return Article{ - Id: id, + ID: id, Title: title, Body: body, CreatedAt: createdAt, diff --git a/domain/gateways/IArticleRepository.go b/domain/gateways/IArticleRepository.go index 4f8b504..0dc25b8 100644 --- a/domain/gateways/IArticleRepository.go +++ b/domain/gateways/IArticleRepository.go @@ -5,7 +5,7 @@ import ( ) type IArticleRepository interface { - Get(id int) Article + Get(id uint32) (Article, error) GetAll() []Article - Create(article Article) Article + Create(article Article) (Article, error) } diff --git a/go.mod b/go.mod index 18880af..8268f4e 100644 --- a/go.mod +++ b/go.mod @@ -3,17 +3,23 @@ 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 + github.com/glebarez/sqlite v1.9.0 + github.com/go-chi/httplog v0.3.1 + go.uber.org/dig v1.17.0 + gorm.io/gorm v1.25.2 ) require ( - github.com/go-chi/httplog v0.3.1 - go.uber.org/dig v1.17.0 + github.com/dustin/go-humanize v1.0.1 // indirect + github.com/glebarez/go-sqlite v1.21.2 // indirect + github.com/google/uuid v1.3.0 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect + modernc.org/libc v1.24.1 // indirect + modernc.org/mathutil v1.5.0 // indirect + modernc.org/memory v1.6.0 // indirect + modernc.org/sqlite v1.25.0 // indirect ) require ( diff --git a/go.sum b/go.sum index 32f4b84..01ee92a 100644 --- a/go.sum +++ b/go.sum @@ -1,17 +1,24 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= -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/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9gAXWo= +github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k= +github.com/glebarez/sqlite v1.9.0 h1:Aj6bPA12ZEx5GbSF6XADmCkYXlljPNUY+Zf1EQxynXs= +github.com/glebarez/sqlite v1.9.0/go.mod h1:YBYCoyupOao60lzp1MVBLEjZfgkq0tdB1voAQ09K9zw= github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk= github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-chi/httplog v0.3.1 h1:uC3IUWCZagtbCinb3ypFh36SEcgd6StWw2Bu0XSXRtg= github.com/go-chi/httplog v0.3.1/go.mod h1:UoiQQ/MTZH5V6JbNB2FzF0DynTh5okpXxlhsyxoP5m8= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= -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/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= @@ -21,64 +28,29 @@ github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APP github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 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/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.29.1 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc= github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU= -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= +github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= 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-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/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.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= golang.org/x/sys v0.10.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= +gorm.io/gorm v1.25.2 h1:gs1o6Vsa+oVKG/a9ElL3XgyGfghFfkKA2SInQaCyMho= +gorm.io/gorm v1.25.2/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= +modernc.org/libc v1.24.1 h1:uvJSeCKL/AgzBo2yYIPPTy82v21KgGnizcGYfBHaNuM= +modernc.org/libc v1.24.1/go.mod h1:FmfO1RLrU3MHJfyi9eYYmZBfi/R+tqZ6+hQ3yQQUkak= +modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ= +modernc.org/mathutil v1.5.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= +modernc.org/memory v1.6.0 h1:i6mzavxrE9a30whzMfwf7XWVODx2r5OYXvU46cirX7o= +modernc.org/memory v1.6.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU= +modernc.org/sqlite v1.25.0 h1:AFweiwPNd/b3BoKnBOfFm+Y260guGMF+0UFk0savqeA= +modernc.org/sqlite v1.25.0/go.mod h1:FL3pVXie73rg3Rii6V/u5BoHlSoyeZeIgKZEgHARyCU= diff --git a/schema.prisma b/schema.prisma deleted file mode 100644 index 8364499..0000000 --- a/schema.prisma +++ /dev/null @@ -1,16 +0,0 @@ -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 -} diff --git a/useCases/CreateArticleUseCase.go b/useCases/CreateArticleUseCase.go index 3bccf7f..5bef35c 100644 --- a/useCases/CreateArticleUseCase.go +++ b/useCases/CreateArticleUseCase.go @@ -3,6 +3,7 @@ package useCases import ( . "GohCMS2/adapters/secondary/gateways" . "GohCMS2/domain/article" + "gorm.io/gorm" ) type CreateArticleUseCase struct { @@ -14,12 +15,12 @@ type CreateArticleCommand struct { Body string } -func NewCreateArticleUseCase() *CreateArticleUseCase { +func NewCreateArticleUseCase(db *gorm.DB) *CreateArticleUseCase { return &CreateArticleUseCase{ - articleRepository: *NewArticleRepository(), + articleRepository: *NewArticleRepository(db), } } -func (g *CreateArticleUseCase) CreateAarticle(article CreateArticleCommand) Article { +func (g *CreateArticleUseCase) CreateArticle(article CreateArticleCommand) (Article, error) { return g.articleRepository.Create(FromApi(article.Title, article.Body)) } diff --git a/useCases/GetArticleUseCase.go b/useCases/GetArticleUseCase.go index 552de0b..b83f5d7 100644 --- a/useCases/GetArticleUseCase.go +++ b/useCases/GetArticleUseCase.go @@ -3,18 +3,19 @@ package useCases import ( . "GohCMS2/adapters/secondary/gateways" . "GohCMS2/domain/article" + "gorm.io/gorm" ) type GetArticleUseCase struct { articleRepository ArticleRepository } -func NewGetArticleUseCase() *GetArticleUseCase { +func NewGetArticleUseCase(db *gorm.DB) *GetArticleUseCase { return &GetArticleUseCase{ - articleRepository: *NewArticleRepository(), + articleRepository: *NewArticleRepository(db), } } -func (g *GetArticleUseCase) GetArticle(id int) Article { +func (g *GetArticleUseCase) GetArticle(id uint32) (Article, error) { return g.articleRepository.Get(id) } diff --git a/useCases/ListArticlesUseCase.go b/useCases/ListArticlesUseCase.go index b1d1fd4..36a70d7 100644 --- a/useCases/ListArticlesUseCase.go +++ b/useCases/ListArticlesUseCase.go @@ -3,15 +3,16 @@ package useCases import ( . "GohCMS2/adapters/secondary/gateways" . "GohCMS2/domain/article" + "gorm.io/gorm" ) type ListArticlesUseCase struct { articleRepository ArticleRepository } -func NewListArticlesUseCase() *ListArticlesUseCase { +func NewListArticlesUseCase(db *gorm.DB) *ListArticlesUseCase { return &ListArticlesUseCase{ - articleRepository: *NewArticleRepository(), + articleRepository: *NewArticleRepository(db), } }