fix: fixing formatting response in article
This commit is contained in:
parent
09124bae16
commit
8644e0f510
|
@ -19,20 +19,19 @@ type ArticleResponse struct {
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
PublishedAt time.Time `json:"publishedAt"`
|
PublishedAt time.Time `json:"publishedAt"`
|
||||||
UpdatedAt time.Time `json:"updatedAt"`
|
UpdatedAt time.Time `json:"updatedAt"`
|
||||||
PublishedAtFormatted string `json:"publishedAtFormatted"`
|
PublishedAtFormatted string `json:"publishedAtt"`
|
||||||
UpdatedAtFormatted string `json:"updatedAtFormatted"`
|
UpdatedAtFormatted string `json:"updatedAtt"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type FormattedResponse struct {
|
type FormattedResponse struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
CoverImage string `json:"coverImage"`
|
CoverImage string `json:"coverImage"`
|
||||||
Author string `json:"author"`
|
Author string `json:"author"`
|
||||||
Heading string `json:"heading"`
|
Heading string `json:"heading"`
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
|
PublishedAtFormatted string `json:"publishedAt"`
|
||||||
PublishedAtFormatted string `json:"publishedAtFormatted"`
|
UpdatedAtFormatted string `json:"updatedAt"`
|
||||||
UpdatedAtFormatted string `json:"updatedAtFormatted"`
|
|
||||||
}
|
}
|
||||||
type ArticleUpdateRequest struct {
|
type ArticleUpdateRequest struct {
|
||||||
Title string `json:"title" validate:"required"`
|
Title string `json:"title" validate:"required"`
|
||||||
|
@ -45,7 +44,3 @@ type ArticleUpdateRequest struct {
|
||||||
func (ar *ArticleRequest) Validate() error {
|
func (ar *ArticleRequest) Validate() error {
|
||||||
return validate.Struct(ar)
|
return validate.Struct(ar)
|
||||||
}
|
}
|
||||||
|
|
||||||
func FormatDateToIndonesianFormat(t time.Time) string {
|
|
||||||
return t.Format("02-01-2006 15:04")
|
|
||||||
}
|
|
||||||
|
|
|
@ -50,6 +50,7 @@ func CreateArticle(c *fiber.Ctx) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetArticles(c *fiber.Ctx) error {
|
func GetArticles(c *fiber.Ctx) error {
|
||||||
|
|
||||||
articles, err := services.GetArticles()
|
articles, err := services.GetArticles()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Status(fiber.StatusInternalServerError).JSON(utils.FormatResponse(
|
return c.Status(fiber.StatusInternalServerError).JSON(utils.FormatResponse(
|
||||||
|
@ -63,19 +64,32 @@ func GetArticles(c *fiber.Ctx) error {
|
||||||
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
||||||
fiber.StatusOK,
|
fiber.StatusOK,
|
||||||
"Articles fetched successfully but data is empty",
|
"Articles fetched successfully but data is empty",
|
||||||
[]dto.ArticleResponse{},
|
[]dto.FormattedResponse{},
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range articles {
|
var formattedArticles []dto.FormattedResponse
|
||||||
articles[i].PublishedAtFormatted = utils.FormatDateToIndonesianFormat(articles[i].PublishedAt)
|
for _, article := range articles {
|
||||||
articles[i].UpdatedAtFormatted = utils.FormatDateToIndonesianFormat(articles[i].UpdatedAt)
|
|
||||||
|
article.PublishedAtFormatted = utils.FormatDateToIndonesianFormat(article.PublishedAt)
|
||||||
|
article.UpdatedAtFormatted = utils.FormatDateToIndonesianFormat(article.UpdatedAt)
|
||||||
|
|
||||||
|
formattedArticles = append(formattedArticles, dto.FormattedResponse{
|
||||||
|
ID: article.ID,
|
||||||
|
Title: article.Title,
|
||||||
|
CoverImage: article.CoverImage,
|
||||||
|
Author: article.Author,
|
||||||
|
Heading: article.Heading,
|
||||||
|
Content: article.Content,
|
||||||
|
PublishedAtFormatted: article.PublishedAtFormatted,
|
||||||
|
UpdatedAtFormatted: article.UpdatedAtFormatted,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
||||||
fiber.StatusOK,
|
fiber.StatusOK,
|
||||||
"Articles fetched successfully",
|
"Articles fetched successfully",
|
||||||
articles,
|
formattedArticles,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,10 +108,21 @@ func GetArticleByID(c *fiber.Ctx) error {
|
||||||
article.PublishedAtFormatted = utils.FormatDateToIndonesianFormat(article.PublishedAt)
|
article.PublishedAtFormatted = utils.FormatDateToIndonesianFormat(article.PublishedAt)
|
||||||
article.UpdatedAtFormatted = utils.FormatDateToIndonesianFormat(article.UpdatedAt)
|
article.UpdatedAtFormatted = utils.FormatDateToIndonesianFormat(article.UpdatedAt)
|
||||||
|
|
||||||
|
response := dto.FormattedResponse{
|
||||||
|
ID: article.ID,
|
||||||
|
Title: article.Title,
|
||||||
|
CoverImage: article.CoverImage,
|
||||||
|
Author: article.Author,
|
||||||
|
Heading: article.Heading,
|
||||||
|
Content: article.Content,
|
||||||
|
PublishedAtFormatted: article.PublishedAtFormatted,
|
||||||
|
UpdatedAtFormatted: article.UpdatedAtFormatted,
|
||||||
|
}
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
||||||
fiber.StatusOK,
|
fiber.StatusOK,
|
||||||
"Article fetched successfully",
|
"Article fetched successfully",
|
||||||
article,
|
response,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,10 +161,21 @@ func UpdateArticle(c *fiber.Ctx) error {
|
||||||
updatedArticle.PublishedAtFormatted = utils.FormatDateToIndonesianFormat(updatedArticle.PublishedAt)
|
updatedArticle.PublishedAtFormatted = utils.FormatDateToIndonesianFormat(updatedArticle.PublishedAt)
|
||||||
updatedArticle.UpdatedAtFormatted = utils.FormatDateToIndonesianFormat(updatedArticle.UpdatedAt)
|
updatedArticle.UpdatedAtFormatted = utils.FormatDateToIndonesianFormat(updatedArticle.UpdatedAt)
|
||||||
|
|
||||||
|
response := dto.FormattedResponse{
|
||||||
|
ID: updatedArticle.ID,
|
||||||
|
Title: updatedArticle.Title,
|
||||||
|
CoverImage: updatedArticle.CoverImage,
|
||||||
|
Author: updatedArticle.Author,
|
||||||
|
Heading: updatedArticle.Heading,
|
||||||
|
Content: updatedArticle.Content,
|
||||||
|
PublishedAtFormatted: updatedArticle.PublishedAtFormatted,
|
||||||
|
UpdatedAtFormatted: updatedArticle.UpdatedAtFormatted,
|
||||||
|
}
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
||||||
fiber.StatusOK,
|
fiber.StatusOK,
|
||||||
"Article updated successfully",
|
"Article updated successfully",
|
||||||
updatedArticle,
|
response,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,6 +43,7 @@ func GetArticles() ([]dto.ArticleResponse, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var response []dto.ArticleResponse
|
var response []dto.ArticleResponse
|
||||||
for _, article := range articles {
|
for _, article := range articles {
|
||||||
response = append(response, dto.ArticleResponse{
|
response = append(response, dto.ArticleResponse{
|
||||||
|
|
Loading…
Reference in New Issue