mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
feat: go-chi and first routes setup
This commit is contained in:
+49
-4
@@ -3,12 +3,57 @@ package api
|
||||
import (
|
||||
. "GohCMS2/domain/article"
|
||||
. "GohCMS2/useCases"
|
||||
"encoding/json"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func GetArticle(id int) Article {
|
||||
return container.GetArticleUseCase.GetArticle(id)
|
||||
func getArticle(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.Atoi(chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
article := container.GetArticleUseCase.GetArticle(id)
|
||||
articleJson, err := json.Marshal(article)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
_, _ = w.Write(articleJson)
|
||||
}
|
||||
|
||||
func CreateArticle(title string, body string) Article {
|
||||
return container.CreateArticleUseCase.CreateAarticle(CreateArticleCommand{Title: title, Body: body})
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
||||
createdArticle := container.CreateArticleUseCase.CreateAarticle(CreateArticleCommand{
|
||||
Title: article.Title,
|
||||
Body: article.Body,
|
||||
})
|
||||
articleJson, _ := json.Marshal(createdArticle)
|
||||
|
||||
_, _ = w.Write(articleJson)
|
||||
}
|
||||
|
||||
func listArticles(w http.ResponseWriter, _ *http.Request) {
|
||||
articles := container.ListArticlesUseCase.ListArticles()
|
||||
articlesJson, _ := json.Marshal(articles)
|
||||
|
||||
_, _ = w.Write(articlesJson)
|
||||
}
|
||||
|
||||
func NewArticleRouter() http.Handler {
|
||||
r := chi.NewRouter()
|
||||
r.Get("/{id}", getArticle)
|
||||
r.Post("/", postArticle)
|
||||
r.Get("/", listArticles)
|
||||
return r
|
||||
}
|
||||
|
||||
+20
-20
@@ -2,40 +2,40 @@ package api
|
||||
|
||||
import (
|
||||
. "GohCMS2/useCases"
|
||||
"errors"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
type Container struct {
|
||||
CreateArticleUseCase *CreateArticleUseCase
|
||||
GetArticleUseCase *GetArticleUseCase
|
||||
ListArticlesUseCase *ListArticlesUseCase
|
||||
}
|
||||
|
||||
var container *Container
|
||||
|
||||
func InitContainer() *Container {
|
||||
func createContainer(
|
||||
createArticle *CreateArticleUseCase,
|
||||
getArticle *GetArticleUseCase,
|
||||
listArticle *ListArticlesUseCase,
|
||||
) *Container {
|
||||
container = &Container{
|
||||
CreateArticleUseCase: createArticle,
|
||||
GetArticleUseCase: getArticle,
|
||||
ListArticlesUseCase: listArticle,
|
||||
}
|
||||
return container
|
||||
}
|
||||
|
||||
func InitContainer() {
|
||||
if container != nil {
|
||||
return container
|
||||
return
|
||||
}
|
||||
|
||||
digContainer := dig.New()
|
||||
|
||||
err1 := digContainer.Provide(NewCreateArticleUseCase)
|
||||
err2 := digContainer.Provide(NewGetArticleUseCase)
|
||||
_ = digContainer.Provide(NewCreateArticleUseCase)
|
||||
_ = digContainer.Provide(NewGetArticleUseCase)
|
||||
_ = digContainer.Provide(NewListArticlesUseCase)
|
||||
|
||||
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
|
||||
_ = digContainer.Invoke(createContainer)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user