2020-09-17 20:33:58 +02:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go-rest-api/model"
|
2020-09-29 22:35:59 +02:00
|
|
|
"log"
|
2020-09-17 20:33:58 +02:00
|
|
|
"net/http"
|
|
|
|
|
2020-09-26 22:38:38 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
2020-09-17 20:33:58 +02:00
|
|
|
)
|
|
|
|
|
2020-09-26 22:38:38 +02:00
|
|
|
// AttachArticleHandlersToRouter func
|
|
|
|
func AttachArticleHandlersToRouter(e *echo.Echo) {
|
2020-09-29 22:35:59 +02:00
|
|
|
h := NewArticlesHandler()
|
|
|
|
|
|
|
|
e.GET("/rabbit", h.testRabbit)
|
2020-09-26 22:38:38 +02:00
|
|
|
|
|
|
|
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-26 22:38:38 +02:00
|
|
|
}
|
|
|
|
|
2020-09-29 22:35:59 +02:00
|
|
|
// NewArticlesHandler return new Articles Handler
|
|
|
|
func NewArticlesHandler() ArticlesHandler {
|
|
|
|
return ArticlesHandler{BaseHandler: *NewHandler()}
|
2020-09-26 22:38:38 +02:00
|
|
|
}
|
2020-09-17 20:33:58 +02:00
|
|
|
|
2020-09-26 22:38:38 +02:00
|
|
|
// 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...")
|
|
|
|
|
2020-09-30 22:12:50 +02:00
|
|
|
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)
|
2020-09-30 22:12:50 +02:00
|
|
|
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-17 20:33:58 +02:00
|
|
|
}
|
|
|
|
|
2020-09-29 22:35:59 +02:00
|
|
|
func (h *ArticlesHandler) getAllArticles(c echo.Context) error {
|
2020-09-26 22:38:38 +02:00
|
|
|
coll := &model.Articles{}
|
2020-09-29 22:35:59 +02:00
|
|
|
articles := h.BaseHandler.GetAllObjects(&model.Article{}, coll, bson.D{})
|
2020-09-17 20:33:58 +02:00
|
|
|
|
2020-09-26 22:38:38 +02:00
|
|
|
return c.JSON(http.StatusOK, articles)
|
2020-09-17 20:33:58 +02:00
|
|
|
}
|
|
|
|
|
2020-09-29 22:35:59 +02:00
|
|
|
func (h *ArticlesHandler) getOneArticle(c echo.Context) error {
|
2020-09-26 22:38:38 +02:00
|
|
|
id := c.Param("id")
|
|
|
|
art := &model.Article{}
|
|
|
|
|
2020-09-29 22:35:59 +02:00
|
|
|
if _, err := h.BaseHandler.GetSingleObject(art, id); err != nil {
|
2020-09-26 22:38:38 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, art)
|
2020-09-17 20:33:58 +02:00
|
|
|
}
|
|
|
|
|
2020-09-29 22:35:59 +02:00
|
|
|
func (h *ArticlesHandler) createArticle(c echo.Context) error {
|
2020-09-26 22:38:38 +02:00
|
|
|
art := &model.Article{}
|
|
|
|
c.Bind(art)
|
|
|
|
|
2020-09-29 22:35:59 +02:00
|
|
|
if _, err := h.BaseHandler.CreateObject(art); err != nil {
|
2020-09-26 22:38:38 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-29 22:35:59 +02:00
|
|
|
return c.JSON(http.StatusCreated, art)
|
2020-09-17 20:33:58 +02:00
|
|
|
}
|
|
|
|
|
2020-09-29 22:35:59 +02:00
|
|
|
func (h *ArticlesHandler) updateArticle(c echo.Context) error {
|
2020-09-26 22:38:38 +02:00
|
|
|
id := c.Param("id")
|
|
|
|
model := &model.Article{}
|
|
|
|
|
2020-09-29 22:35:59 +02:00
|
|
|
art, err := h.BaseHandler.GetSingleObject(model, id)
|
2020-09-26 22:38:38 +02:00
|
|
|
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 {
|
2020-09-26 22:38:38 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, art)
|
|
|
|
}
|
|
|
|
|
2020-09-29 22:35:59 +02:00
|
|
|
func (h *ArticlesHandler) removeArticle(c echo.Context) error {
|
2020-09-26 22:38:38 +02:00
|
|
|
id := c.Param("id")
|
|
|
|
model := &model.Article{}
|
2020-09-29 22:35:59 +02:00
|
|
|
|
|
|
|
art, err := h.BaseHandler.GetSingleObject(model, id)
|
2020-09-26 22:38:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-29 22:35:59 +02:00
|
|
|
if err := h.BaseHandler.RemoveObject(art); err != nil {
|
2020-09-26 22:38:38 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.NoContent(http.StatusOK)
|
2020-09-17 20:33:58 +02:00
|
|
|
}
|