feat: migrated prisma -> gorm

This commit is contained in:
Florian Sylvain
2023-08-06 19:13:49 +02:00
parent 195c247235
commit e258dccc1c
14 changed files with 149 additions and 166 deletions
@@ -1,77 +1,57 @@
package gateways package gateways
import ( import (
"GohCMS2/db" entity "GohCMS2/adapters/secondary/gateways/models"
. "GohCMS2/domain/article" domain "GohCMS2/domain/article"
. "GohCMS2/domain/gateways" . "GohCMS2/domain/gateways"
"context" "gorm.io/gorm"
) )
type ArticleRepository struct { type ArticleRepository struct {
client *db.PrismaClient db *gorm.DB
} }
func NewArticleRepository() *ArticleRepository { func NewArticleRepository(db *gorm.DB) *ArticleRepository {
a := ArticleRepository{ a := ArticleRepository{db}
client: db.NewClient(),
}
return &a return &a
} }
func (a *ArticleRepository) connectClient() { func (a *ArticleRepository) Get(id uint32) (domain.Article, error) {
err := a.client.Connect() var article domain.Article
err := a.db.Model(&entity.Article{}).First(&article, id).Error
if err != nil { if err != nil {
panic(err) return article, err
}
} }
func (a *ArticleRepository) disconnectClient() { return domain.FromDb(article.ID, article.Title, article.Body, article.CreatedAt, article.UpdatedAt), nil
err := a.client.Disconnect() }
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
}
var createdArticle entity.Article
creationResult.Scan(&createdArticle)
return domain.FromDb(
createdArticle.ID,
createdArticle.Title,
createdArticle.Body,
createdArticle.CreatedAt,
createdArticle.UpdatedAt),
nil
}
func (a *ArticleRepository) GetAll() []domain.Article {
var articles []domain.Article
err := a.db.Model(&entity.Article{}).Find(&articles).Error
if err != nil { if err != nil {
panic(err) return []domain.Article{}
}
a.client = db.NewClient()
}
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())
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 articles return articles
@@ -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"`
}
+11 -7
View File
@@ -10,19 +10,19 @@ import (
) )
func getArticle(w http.ResponseWriter, r *http.Request) { 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 { 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 return
} }
article := container.GetArticleUseCase.GetArticle(id) article, err := container.GetArticleUseCase.GetArticle(uint32(id))
articleJson, err := json.Marshal(article)
if err != nil { 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 return
} }
articleJson, _ := json.Marshal(article)
_, _ = w.Write(articleJson) _, _ = w.Write(articleJson)
} }
@@ -30,14 +30,18 @@ func postArticle(w http.ResponseWriter, r *http.Request) {
var article Article var article Article
err := json.NewDecoder(r.Body).Decode(&article) err := json.NewDecoder(r.Body).Decode(&article)
if err != nil { 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 return
} }
createdArticle := container.CreateArticleUseCase.CreateAarticle(CreateArticleCommand{ createdArticle, err := container.CreateArticleUseCase.CreateArticle(CreateArticleCommand{
Title: article.Title, Title: article.Title,
Body: article.Body, Body: article.Body,
}) })
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
articleJson, _ := json.Marshal(createdArticle) articleJson, _ := json.Marshal(createdArticle)
_, _ = w.Write(articleJson) _, _ = w.Write(articleJson)
+16 -5
View File
@@ -1,8 +1,11 @@
package api package api
import ( import (
"GohCMS2/adapters/secondary/gateways/models"
. "GohCMS2/useCases" . "GohCMS2/useCases"
"github.com/glebarez/sqlite"
"go.uber.org/dig" "go.uber.org/dig"
"gorm.io/gorm"
) )
type Container struct { type Container struct {
@@ -13,7 +16,7 @@ type Container struct {
var container *Container var container *Container
func createContainer( func setContainer(
createArticle *CreateArticleUseCase, createArticle *CreateArticleUseCase,
getArticle *GetArticleUseCase, getArticle *GetArticleUseCase,
listArticle *ListArticlesUseCase, listArticle *ListArticlesUseCase,
@@ -33,9 +36,17 @@ func InitContainer() {
digContainer := dig.New() digContainer := dig.New()
_ = digContainer.Provide(NewCreateArticleUseCase) db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
_ = digContainer.Provide(NewGetArticleUseCase) if err != nil {
_ = digContainer.Provide(NewListArticlesUseCase) 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)
} }
+8
View File
@@ -9,6 +9,13 @@ import (
"net/http" "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() { func main() {
api.InitContainer() api.InitContainer()
@@ -21,6 +28,7 @@ func main() {
apiRouter := chi.NewRouter() apiRouter := chi.NewRouter()
apiRouter.Use(httplog.RequestLogger(httplog.NewLogger("GohCMS2"))) apiRouter.Use(httplog.RequestLogger(httplog.NewLogger("GohCMS2")))
apiRouter.Use(jsonContentTypeMiddleware)
apiRouter.Mount("/v1", r) apiRouter.Mount("/v1", r)
fmt.Println("Server starting on port 8080") fmt.Println("Server starting on port 8080")
-2
View File
@@ -1,2 +0,0 @@
# gitignore generated by Prisma Client Go. DO NOT EDIT.
*_gen.go
+9 -7
View File
@@ -1,11 +1,13 @@
package article package article
import "time"
type Article struct { type Article struct {
Id int `json:"id"` ID uint32 `json:"id"`
Title string `json:"title"` Title string `json:"title"`
Body string `json:"body"` Body string `json:"body"`
CreatedAt string `json:"created_at"` CreatedAt time.Time `json:"created_at"`
UpdatedAt string `json:"updated_at"` UpdatedAt time.Time `json:"updated_at"`
} }
func FromApi( func FromApi(
@@ -19,14 +21,14 @@ func FromApi(
} }
func FromDb( func FromDb(
id int, id uint32,
title string, title string,
body string, body string,
createdAt string, createdAt time.Time,
updatedAt string, updatedAt time.Time,
) Article { ) Article {
return Article{ return Article{
Id: id, ID: id,
Title: title, Title: title,
Body: body, Body: body,
CreatedAt: createdAt, CreatedAt: createdAt,
+2 -2
View File
@@ -5,7 +5,7 @@ import (
) )
type IArticleRepository interface { type IArticleRepository interface {
Get(id int) Article Get(id uint32) (Article, error)
GetAll() []Article GetAll() []Article
Create(article Article) Article Create(article Article) (Article, error)
} }
+14 -8
View File
@@ -3,17 +3,23 @@ module GohCMS2
go 1.20 go 1.20
require ( require (
github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365 github.com/glebarez/sqlite v1.9.0
github.com/joho/godotenv v1.5.1 github.com/go-chi/httplog v0.3.1
github.com/shopspring/decimal v1.3.1 go.uber.org/dig v1.17.0
github.com/steebchen/prisma-client-go v0.21.0 gorm.io/gorm v1.25.2
github.com/takuoki/gocase v1.0.0
golang.org/x/text v0.10.0
) )
require ( require (
github.com/go-chi/httplog v0.3.1 github.com/dustin/go-humanize v1.0.1 // indirect
go.uber.org/dig v1.17.0 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 ( require (
+27 -55
View File
@@ -1,17 +1,24 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= 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 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.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 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk=
github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= 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 h1:uC3IUWCZagtbCinb3ypFh36SEcgd6StWw2Bu0XSXRtg=
github.com/go-chi/httplog v0.3.1/go.mod h1:UoiQQ/MTZH5V6JbNB2FzF0DynTh5okpXxlhsyxoP5m8= 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/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365 h1:ECW73yc9MY7935nNYXUkK7Dz17YuSUI9yqRqYS8aBww= github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= 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.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= 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/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/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 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/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 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc=
github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU= github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8= github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
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 h1:5Chju+tUvcC+N7N6EV08BJz41UZuO3BmHcN4A287ZLI=
go.uber.org/dig v1.17.0/go.mod h1:rTxpf7l5I0eBTlE6/9RL+lDybC7WFwY2QH55ZSjy1mU= 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-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-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.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.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= 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/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 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=
-16
View File
@@ -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
}
+4 -3
View File
@@ -3,6 +3,7 @@ package useCases
import ( import (
. "GohCMS2/adapters/secondary/gateways" . "GohCMS2/adapters/secondary/gateways"
. "GohCMS2/domain/article" . "GohCMS2/domain/article"
"gorm.io/gorm"
) )
type CreateArticleUseCase struct { type CreateArticleUseCase struct {
@@ -14,12 +15,12 @@ type CreateArticleCommand struct {
Body string Body string
} }
func NewCreateArticleUseCase() *CreateArticleUseCase { func NewCreateArticleUseCase(db *gorm.DB) *CreateArticleUseCase {
return &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)) return g.articleRepository.Create(FromApi(article.Title, article.Body))
} }
+4 -3
View File
@@ -3,18 +3,19 @@ package useCases
import ( import (
. "GohCMS2/adapters/secondary/gateways" . "GohCMS2/adapters/secondary/gateways"
. "GohCMS2/domain/article" . "GohCMS2/domain/article"
"gorm.io/gorm"
) )
type GetArticleUseCase struct { type GetArticleUseCase struct {
articleRepository ArticleRepository articleRepository ArticleRepository
} }
func NewGetArticleUseCase() *GetArticleUseCase { func NewGetArticleUseCase(db *gorm.DB) *GetArticleUseCase {
return &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) return g.articleRepository.Get(id)
} }
+3 -2
View File
@@ -3,15 +3,16 @@ package useCases
import ( import (
. "GohCMS2/adapters/secondary/gateways" . "GohCMS2/adapters/secondary/gateways"
. "GohCMS2/domain/article" . "GohCMS2/domain/article"
"gorm.io/gorm"
) )
type ListArticlesUseCase struct { type ListArticlesUseCase struct {
articleRepository ArticleRepository articleRepository ArticleRepository
} }
func NewListArticlesUseCase() *ListArticlesUseCase { func NewListArticlesUseCase(db *gorm.DB) *ListArticlesUseCase {
return &ListArticlesUseCase{ return &ListArticlesUseCase{
articleRepository: *NewArticleRepository(), articleRepository: *NewArticleRepository(db),
} }
} }