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"`
|
||||
PublishedAt time.Time `json:"publishedAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
PublishedAtFormatted string `json:"publishedAtFormatted"`
|
||||
UpdatedAtFormatted string `json:"updatedAtFormatted"`
|
||||
PublishedAtFormatted string `json:"publishedAtt"`
|
||||
UpdatedAtFormatted string `json:"updatedAtt"`
|
||||
}
|
||||
|
||||
type FormattedResponse struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
CoverImage string `json:"coverImage"`
|
||||
Author string `json:"author"`
|
||||
Heading string `json:"heading"`
|
||||
Content string `json:"content"`
|
||||
|
||||
PublishedAtFormatted string `json:"publishedAtFormatted"`
|
||||
UpdatedAtFormatted string `json:"updatedAtFormatted"`
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
CoverImage string `json:"coverImage"`
|
||||
Author string `json:"author"`
|
||||
Heading string `json:"heading"`
|
||||
Content string `json:"content"`
|
||||
PublishedAtFormatted string `json:"publishedAt"`
|
||||
UpdatedAtFormatted string `json:"updatedAt"`
|
||||
}
|
||||
type ArticleUpdateRequest struct {
|
||||
Title string `json:"title" validate:"required"`
|
||||
|
@ -45,7 +44,3 @@ type ArticleUpdateRequest struct {
|
|||
func (ar *ArticleRequest) Validate() error {
|
||||
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 {
|
||||
|
||||
articles, err := services.GetArticles()
|
||||
if err != nil {
|
||||
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(
|
||||
fiber.StatusOK,
|
||||
"Articles fetched successfully but data is empty",
|
||||
[]dto.ArticleResponse{},
|
||||
[]dto.FormattedResponse{},
|
||||
))
|
||||
}
|
||||
|
||||
for i := range articles {
|
||||
articles[i].PublishedAtFormatted = utils.FormatDateToIndonesianFormat(articles[i].PublishedAt)
|
||||
articles[i].UpdatedAtFormatted = utils.FormatDateToIndonesianFormat(articles[i].UpdatedAt)
|
||||
var formattedArticles []dto.FormattedResponse
|
||||
for _, article := range articles {
|
||||
|
||||
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(
|
||||
fiber.StatusOK,
|
||||
"Articles fetched successfully",
|
||||
articles,
|
||||
formattedArticles,
|
||||
))
|
||||
}
|
||||
|
||||
|
@ -94,10 +108,21 @@ func GetArticleByID(c *fiber.Ctx) error {
|
|||
article.PublishedAtFormatted = utils.FormatDateToIndonesianFormat(article.PublishedAt)
|
||||
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(
|
||||
fiber.StatusOK,
|
||||
"Article fetched successfully",
|
||||
article,
|
||||
response,
|
||||
))
|
||||
}
|
||||
|
||||
|
@ -136,10 +161,21 @@ func UpdateArticle(c *fiber.Ctx) error {
|
|||
updatedArticle.PublishedAtFormatted = utils.FormatDateToIndonesianFormat(updatedArticle.PublishedAt)
|
||||
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(
|
||||
fiber.StatusOK,
|
||||
"Article updated successfully",
|
||||
updatedArticle,
|
||||
response,
|
||||
))
|
||||
}
|
||||
|
||||
|
|
|
@ -43,6 +43,7 @@ func GetArticles() ([]dto.ArticleResponse, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var response []dto.ArticleResponse
|
||||
for _, article := range articles {
|
||||
response = append(response, dto.ArticleResponse{
|
||||
|
|
Loading…
Reference in New Issue