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
+11 -7
View File
@@ -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)
+16 -5
View File
@@ -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)
}