fix: fixing format time
This commit is contained in:
parent
a05edce16a
commit
09124bae16
|
@ -3,33 +3,49 @@ package dto
|
|||
import "time"
|
||||
|
||||
type ArticleRequest struct {
|
||||
Title string `json:"title" validate:"required"`
|
||||
CoverImage string `json:"coverImage" validate:"required"`
|
||||
Author string `json:"author" validate:"required"`
|
||||
Heading string `json:"heading" validate:"required"`
|
||||
Content string `json:"content" validate:"required"`
|
||||
PublishedAt time.Time `json:"publishedAt" validate:"required"`
|
||||
Title string `json:"title" validate:"required"`
|
||||
CoverImage string `json:"coverImage" validate:"required"`
|
||||
Author string `json:"author" validate:"required"`
|
||||
Heading string `json:"heading" validate:"required"`
|
||||
Content string `json:"content" validate:"required"`
|
||||
}
|
||||
|
||||
type ArticleResponse 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"`
|
||||
PublishedAt time.Time `json:"publishedAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
CoverImage string `json:"coverImage"`
|
||||
Author string `json:"author"`
|
||||
Heading string `json:"heading"`
|
||||
Content string `json:"content"`
|
||||
PublishedAt time.Time `json:"publishedAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
PublishedAtFormatted string `json:"publishedAtFormatted"`
|
||||
UpdatedAtFormatted string `json:"updatedAtFormatted"`
|
||||
}
|
||||
|
||||
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"`
|
||||
}
|
||||
type ArticleUpdateRequest struct {
|
||||
Title string `json:"title" validate:"required"`
|
||||
CoverImage string `json:"coverImage" validate:"required"`
|
||||
Author string `json:"author" validate:"required"`
|
||||
Heading string `json:"heading" validate:"required"`
|
||||
Content string `json:"content" validate:"required"`
|
||||
Title string `json:"title" validate:"required"`
|
||||
CoverImage string `json:"coverImage" validate:"required"`
|
||||
Author string `json:"author" validate:"required"`
|
||||
Heading string `json:"heading" validate:"required"`
|
||||
Content string `json:"content" validate:"required"`
|
||||
}
|
||||
|
||||
func (ar *ArticleRequest) Validate() error {
|
||||
return validate.Struct(ar)
|
||||
}
|
||||
}
|
||||
|
||||
func FormatDateToIndonesianFormat(t time.Time) string {
|
||||
return t.Format("02-01-2006 15:04")
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ import (
|
|||
|
||||
func CreateArticle(c *fiber.Ctx) error {
|
||||
var articleRequest dto.ArticleRequest
|
||||
|
||||
if err := c.BodyParser(&articleRequest); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(utils.FormatResponse(
|
||||
fiber.StatusBadRequest,
|
||||
|
@ -18,17 +19,7 @@ func CreateArticle(c *fiber.Ctx) error {
|
|||
))
|
||||
}
|
||||
|
||||
validate := validator.New()
|
||||
err := validate.Struct(articleRequest)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(utils.FormatResponse(
|
||||
fiber.StatusBadRequest,
|
||||
"Validation error",
|
||||
err.Error(),
|
||||
))
|
||||
}
|
||||
|
||||
createdArticle, err := services.CreateArticle(&articleRequest)
|
||||
articleResponse, err := services.CreateArticle(&articleRequest)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(utils.FormatResponse(
|
||||
fiber.StatusInternalServerError,
|
||||
|
@ -37,10 +28,24 @@ func CreateArticle(c *fiber.Ctx) error {
|
|||
))
|
||||
}
|
||||
|
||||
articleResponse.PublishedAtFormatted = utils.FormatDateToIndonesianFormat(articleResponse.PublishedAt)
|
||||
articleResponse.UpdatedAtFormatted = utils.FormatDateToIndonesianFormat(articleResponse.UpdatedAt)
|
||||
|
||||
response := dto.FormattedResponse{
|
||||
ID: articleResponse.ID,
|
||||
Title: articleResponse.Title,
|
||||
CoverImage: articleResponse.CoverImage,
|
||||
Author: articleResponse.Author,
|
||||
Heading: articleResponse.Heading,
|
||||
Content: articleResponse.Content,
|
||||
PublishedAtFormatted: articleResponse.PublishedAtFormatted,
|
||||
UpdatedAtFormatted: articleResponse.UpdatedAtFormatted,
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusCreated).JSON(utils.FormatResponse(
|
||||
fiber.StatusCreated,
|
||||
"Article created successfully",
|
||||
createdArticle,
|
||||
response,
|
||||
))
|
||||
}
|
||||
|
||||
|
@ -54,6 +59,19 @@ func GetArticles(c *fiber.Ctx) error {
|
|||
))
|
||||
}
|
||||
|
||||
if len(articles) == 0 {
|
||||
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
||||
fiber.StatusOK,
|
||||
"Articles fetched successfully but data is empty",
|
||||
[]dto.ArticleResponse{},
|
||||
))
|
||||
}
|
||||
|
||||
for i := range articles {
|
||||
articles[i].PublishedAtFormatted = utils.FormatDateToIndonesianFormat(articles[i].PublishedAt)
|
||||
articles[i].UpdatedAtFormatted = utils.FormatDateToIndonesianFormat(articles[i].UpdatedAt)
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
||||
fiber.StatusOK,
|
||||
"Articles fetched successfully",
|
||||
|
@ -73,6 +91,9 @@ func GetArticleByID(c *fiber.Ctx) error {
|
|||
))
|
||||
}
|
||||
|
||||
article.PublishedAtFormatted = utils.FormatDateToIndonesianFormat(article.PublishedAt)
|
||||
article.UpdatedAtFormatted = utils.FormatDateToIndonesianFormat(article.UpdatedAt)
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
||||
fiber.StatusOK,
|
||||
"Article fetched successfully",
|
||||
|
@ -84,6 +105,7 @@ func UpdateArticle(c *fiber.Ctx) error {
|
|||
id := c.Params("id")
|
||||
|
||||
var articleUpdateRequest dto.ArticleUpdateRequest
|
||||
|
||||
if err := c.BodyParser(&articleUpdateRequest); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(utils.FormatResponse(
|
||||
fiber.StatusBadRequest,
|
||||
|
@ -111,6 +133,9 @@ func UpdateArticle(c *fiber.Ctx) error {
|
|||
))
|
||||
}
|
||||
|
||||
updatedArticle.PublishedAtFormatted = utils.FormatDateToIndonesianFormat(updatedArticle.PublishedAt)
|
||||
updatedArticle.UpdatedAtFormatted = utils.FormatDateToIndonesianFormat(updatedArticle.UpdatedAt)
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
||||
fiber.StatusOK,
|
||||
"Article updated successfully",
|
||||
|
|
|
@ -8,23 +8,23 @@ import (
|
|||
"github.com/pahmiudahgede/senggoldong/internal/repositories"
|
||||
)
|
||||
|
||||
func CreateArticle(articleRequest *dto.ArticleRequest) (*dto.ArticleResponse, error) {
|
||||
func CreateArticle(articleRequest *dto.ArticleRequest) (dto.ArticleResponse, error) {
|
||||
article := domain.Article{
|
||||
Title: articleRequest.Title,
|
||||
CoverImage: articleRequest.CoverImage,
|
||||
Author: articleRequest.Author,
|
||||
Heading: articleRequest.Heading,
|
||||
Content: articleRequest.Content,
|
||||
PublishedAt: articleRequest.PublishedAt,
|
||||
UpdatedAt: articleRequest.PublishedAt,
|
||||
PublishedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
|
||||
err := repositories.CreateArticle(&article)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return dto.ArticleResponse{}, err
|
||||
}
|
||||
|
||||
return &dto.ArticleResponse{
|
||||
articleResponse := dto.ArticleResponse{
|
||||
ID: article.ID,
|
||||
Title: article.Title,
|
||||
CoverImage: article.CoverImage,
|
||||
|
@ -33,7 +33,9 @@ func CreateArticle(articleRequest *dto.ArticleRequest) (*dto.ArticleResponse, er
|
|||
Content: article.Content,
|
||||
PublishedAt: article.PublishedAt,
|
||||
UpdatedAt: article.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
return articleResponse, nil
|
||||
}
|
||||
|
||||
func GetArticles() ([]dto.ArticleResponse, error) {
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
func GetUserRoleByID(id string) (domain.UserRole, error) {
|
||||
role, err := repositories.GetUserRoleByID(id)
|
||||
if err != nil {
|
||||
return role, errors.New("UserRole tidak ditemukan")
|
||||
return role, errors.New("userRole tidak ditemukan")
|
||||
}
|
||||
return role, nil
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ func GetUserRoleByID(id string) (domain.UserRole, error) {
|
|||
func GetAllUserRoles() ([]domain.UserRole, error) {
|
||||
roles, err := repositories.GetAllUserRoles()
|
||||
if err != nil {
|
||||
return nil, errors.New("Gagal mengambil data UserRole")
|
||||
return nil, errors.New("gagal mengambil data UserRole")
|
||||
}
|
||||
return roles, nil
|
||||
}
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
func FormatDateToIndonesianFormat(t time.Time) string {
|
||||
|
||||
utcTime := t.UTC()
|
||||
|
||||
loc, err := time.LoadLocation("Asia/Jakarta")
|
||||
if err != nil {
|
||||
log.Printf("Error loading timezone: %v", err)
|
||||
return ""
|
||||
}
|
||||
|
||||
indonesianTime := utcTime.In(loc)
|
||||
|
||||
return indonesianTime.Format("02-01-2006 15:04")
|
||||
}
|
Loading…
Reference in New Issue