rest-api/handler/articles.go

121 lines
2.7 KiB
Go
Raw Permalink Normal View History

package handler
import (
"go-rest-api/model"
2020-09-29 22:35:59 +02:00
"log"
"net/http"
"github.com/labstack/echo/v4"
"go.mongodb.org/mongo-driver/bson"
)
// AttachArticleHandlersToRouter func
func AttachArticleHandlersToRouter(e *echo.Echo) {
2020-09-29 22:35:59 +02:00
h := NewArticlesHandler()
e.GET("/rabbit", h.testRabbit)
g := e.Group("/articles")
2020-09-29 22:35:59 +02:00
g.GET("", h.getAllArticles)
g.GET("/:id", h.getOneArticle)
g.POST("", h.createArticle)
g.PUT("/:id", h.updateArticle)
g.DELETE("/:id", h.removeArticle)
}
2020-09-29 22:35:59 +02:00
// NewArticlesHandler return new Articles Handler
func NewArticlesHandler() ArticlesHandler {
return ArticlesHandler{BaseHandler: *NewHandler()}
}
// ArticlesHandler type
type ArticlesHandler struct {
2020-09-29 22:35:59 +02:00
BaseHandler
}
// ArticleGenerateHeavyPDFMessage Struct that holds all data necessary for demo action
type ArticleGenerateHeavyPDFMessage struct {
Title string `json:"title"`
Content string `json:"content"`
Pages int `json:"pages"`
}
func (h *ArticlesHandler) testRabbit(c echo.Context) error {
log.Println("test rabbit publish method calling...")
q := h.Queue.DeclareQueue("article_queue_test")
2020-09-29 22:35:59 +02:00
m := ArticleGenerateHeavyPDFMessage{
Title: "Test title",
Content: "Lorem ipsum dolor sit amet",
Pages: 10,
}
body, _ := h.JSONEncode(m)
h.Queue.Publish(q.Name, body, "application/json")
2020-09-29 22:35:59 +02:00
response := "Please wait until generating pdf or some other long task (in rabbitMQ queue) done"
return c.JSON(http.StatusOK, response)
}
2020-09-29 22:35:59 +02:00
func (h *ArticlesHandler) getAllArticles(c echo.Context) error {
coll := &model.Articles{}
2020-09-29 22:35:59 +02:00
articles := h.BaseHandler.GetAllObjects(&model.Article{}, coll, bson.D{})
return c.JSON(http.StatusOK, articles)
}
2020-09-29 22:35:59 +02:00
func (h *ArticlesHandler) getOneArticle(c echo.Context) error {
id := c.Param("id")
art := &model.Article{}
2020-09-29 22:35:59 +02:00
if _, err := h.BaseHandler.GetSingleObject(art, id); err != nil {
return err
}
return c.JSON(http.StatusOK, art)
}
2020-09-29 22:35:59 +02:00
func (h *ArticlesHandler) createArticle(c echo.Context) error {
art := &model.Article{}
c.Bind(art)
2020-09-29 22:35:59 +02:00
if _, err := h.BaseHandler.CreateObject(art); err != nil {
return err
}
2020-09-29 22:35:59 +02:00
return c.JSON(http.StatusCreated, art)
}
2020-09-29 22:35:59 +02:00
func (h *ArticlesHandler) updateArticle(c echo.Context) error {
id := c.Param("id")
model := &model.Article{}
2020-09-29 22:35:59 +02:00
art, err := h.BaseHandler.GetSingleObject(model, id)
if err != nil {
return err
}
DeserializeFromRequest(c.Request(), art)
2020-09-29 22:35:59 +02:00
if _, err := h.BaseHandler.UpdateObject(art); err != nil {
return err
}
return c.JSON(http.StatusOK, art)
}
2020-09-29 22:35:59 +02:00
func (h *ArticlesHandler) removeArticle(c echo.Context) error {
id := c.Param("id")
model := &model.Article{}
2020-09-29 22:35:59 +02:00
art, err := h.BaseHandler.GetSingleObject(model, id)
if err != nil {
return err
}
2020-09-29 22:35:59 +02:00
if err := h.BaseHandler.RemoveObject(art); err != nil {
return err
}
return c.NoContent(http.StatusOK)
}