mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
clean: Article to Post in post.go
Changes was made to keep consistency in terminology across the application.
This commit is contained in:
+20
-20
@@ -8,66 +8,66 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ArticlePost struct {
|
type PostPost struct {
|
||||||
Title string `json:"title" validate:"required,min=3,max=50"`
|
Title string `json:"title" validate:"required,min=3,max=50"`
|
||||||
Body string `json:"body" validate:"required,max=10000"`
|
Body string `json:"body" validate:"required,max=10000"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func getArticle(w http.ResponseWriter, r *http.Request) {
|
func getPost(w http.ResponseWriter, r *http.Request) {
|
||||||
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 32)
|
id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "The server expects the ID to be in the format of an unsigned 32-bit integer (uint32).", http.StatusBadRequest)
|
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, err := Container.GetArticleUseCase.GetArticle(uint32(id))
|
post, err := Container.GetArticleUseCase.GetArticle(uint32(id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "The requested resource, identified by its unique ID, could not be found on the server.", 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)
|
postJson, _ := json.Marshal(post)
|
||||||
_, _ = w.Write(articleJson)
|
_, _ = w.Write(postJson)
|
||||||
}
|
}
|
||||||
|
|
||||||
func postArticle(w http.ResponseWriter, r *http.Request) {
|
func postPost(w http.ResponseWriter, r *http.Request) {
|
||||||
var article ArticlePost
|
var post PostPost
|
||||||
err := json.NewDecoder(r.Body).Decode(&article)
|
err := json.NewDecoder(r.Body).Decode(&post)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, bodyErrorMessage, http.StatusBadRequest)
|
http.Error(w, bodyErrorMessage, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = validate.Struct(article)
|
err = validate.Struct(post)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
createdArticle, err := Container.CreateArticleUseCase.CreatePost(useCases.CreatePostCommand{
|
createdArticle, err := Container.CreateArticleUseCase.CreatePost(useCases.CreatePostCommand{
|
||||||
Title: article.Title,
|
Title: post.Title,
|
||||||
Body: article.Body,
|
Body: post.Body,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
articleJson, _ := json.Marshal(createdArticle)
|
postJson, _ := json.Marshal(createdArticle)
|
||||||
|
|
||||||
_, _ = w.Write(articleJson)
|
_, _ = w.Write(postJson)
|
||||||
}
|
}
|
||||||
|
|
||||||
func listArticles(w http.ResponseWriter, _ *http.Request) {
|
func listPosts(w http.ResponseWriter, _ *http.Request) {
|
||||||
articles := Container.ListArticlesUseCase.ListArticles()
|
posts := Container.ListArticlesUseCase.ListArticles()
|
||||||
articlesJson, _ := json.Marshal(articles)
|
postsJson, _ := json.Marshal(posts)
|
||||||
|
|
||||||
_, _ = w.Write(articlesJson)
|
_, _ = w.Write(postsJson)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewArticleRouter() http.Handler {
|
func NewArticleRouter() http.Handler {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
r.Get("/{id}", getArticle)
|
r.Get("/{id}", getPost)
|
||||||
r.Post("/", postArticle)
|
r.Post("/", postPost)
|
||||||
r.Get("/", listArticles)
|
r.Get("/", listPosts)
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user