fix: fixing format time
This commit is contained in:
parent
a05edce16a
commit
09124bae16
|
@ -3,33 +3,49 @@ package dto
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type ArticleRequest struct {
|
type ArticleRequest struct {
|
||||||
Title string `json:"title" validate:"required"`
|
Title string `json:"title" validate:"required"`
|
||||||
CoverImage string `json:"coverImage" validate:"required"`
|
CoverImage string `json:"coverImage" validate:"required"`
|
||||||
Author string `json:"author" validate:"required"`
|
Author string `json:"author" validate:"required"`
|
||||||
Heading string `json:"heading" validate:"required"`
|
Heading string `json:"heading" validate:"required"`
|
||||||
Content string `json:"content" validate:"required"`
|
Content string `json:"content" validate:"required"`
|
||||||
PublishedAt time.Time `json:"publishedAt" validate:"required"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ArticleResponse struct {
|
type ArticleResponse 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"`
|
||||||
PublishedAt time.Time `json:"publishedAt"`
|
PublishedAt time.Time `json:"publishedAt"`
|
||||||
UpdatedAt time.Time `json:"updatedAt"`
|
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 {
|
type ArticleUpdateRequest struct {
|
||||||
Title string `json:"title" validate:"required"`
|
Title string `json:"title" validate:"required"`
|
||||||
CoverImage string `json:"coverImage" validate:"required"`
|
CoverImage string `json:"coverImage" validate:"required"`
|
||||||
Author string `json:"author" validate:"required"`
|
Author string `json:"author" validate:"required"`
|
||||||
Heading string `json:"heading" validate:"required"`
|
Heading string `json:"heading" validate:"required"`
|
||||||
Content string `json:"content" validate:"required"`
|
Content string `json:"content" validate:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
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")
|
||||||
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
|
|
||||||
func CreateArticle(c *fiber.Ctx) error {
|
func CreateArticle(c *fiber.Ctx) error {
|
||||||
var articleRequest dto.ArticleRequest
|
var articleRequest dto.ArticleRequest
|
||||||
|
|
||||||
if err := c.BodyParser(&articleRequest); err != nil {
|
if err := c.BodyParser(&articleRequest); err != nil {
|
||||||
return c.Status(fiber.StatusBadRequest).JSON(utils.FormatResponse(
|
return c.Status(fiber.StatusBadRequest).JSON(utils.FormatResponse(
|
||||||
fiber.StatusBadRequest,
|
fiber.StatusBadRequest,
|
||||||
|
@ -18,17 +19,7 @@ func CreateArticle(c *fiber.Ctx) error {
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
validate := validator.New()
|
articleResponse, err := services.CreateArticle(&articleRequest)
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Status(fiber.StatusInternalServerError).JSON(utils.FormatResponse(
|
return c.Status(fiber.StatusInternalServerError).JSON(utils.FormatResponse(
|
||||||
fiber.StatusInternalServerError,
|
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(
|
return c.Status(fiber.StatusCreated).JSON(utils.FormatResponse(
|
||||||
fiber.StatusCreated,
|
fiber.StatusCreated,
|
||||||
"Article created successfully",
|
"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(
|
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
||||||
fiber.StatusOK,
|
fiber.StatusOK,
|
||||||
"Articles fetched successfully",
|
"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(
|
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
||||||
fiber.StatusOK,
|
fiber.StatusOK,
|
||||||
"Article fetched successfully",
|
"Article fetched successfully",
|
||||||
|
@ -84,6 +105,7 @@ func UpdateArticle(c *fiber.Ctx) error {
|
||||||
id := c.Params("id")
|
id := c.Params("id")
|
||||||
|
|
||||||
var articleUpdateRequest dto.ArticleUpdateRequest
|
var articleUpdateRequest dto.ArticleUpdateRequest
|
||||||
|
|
||||||
if err := c.BodyParser(&articleUpdateRequest); err != nil {
|
if err := c.BodyParser(&articleUpdateRequest); err != nil {
|
||||||
return c.Status(fiber.StatusBadRequest).JSON(utils.FormatResponse(
|
return c.Status(fiber.StatusBadRequest).JSON(utils.FormatResponse(
|
||||||
fiber.StatusBadRequest,
|
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(
|
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
||||||
fiber.StatusOK,
|
fiber.StatusOK,
|
||||||
"Article updated successfully",
|
"Article updated successfully",
|
||||||
|
|
|
@ -8,23 +8,23 @@ import (
|
||||||
"github.com/pahmiudahgede/senggoldong/internal/repositories"
|
"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{
|
article := domain.Article{
|
||||||
Title: articleRequest.Title,
|
Title: articleRequest.Title,
|
||||||
CoverImage: articleRequest.CoverImage,
|
CoverImage: articleRequest.CoverImage,
|
||||||
Author: articleRequest.Author,
|
Author: articleRequest.Author,
|
||||||
Heading: articleRequest.Heading,
|
Heading: articleRequest.Heading,
|
||||||
Content: articleRequest.Content,
|
Content: articleRequest.Content,
|
||||||
PublishedAt: articleRequest.PublishedAt,
|
PublishedAt: time.Now(),
|
||||||
UpdatedAt: articleRequest.PublishedAt,
|
UpdatedAt: time.Now(),
|
||||||
}
|
}
|
||||||
|
|
||||||
err := repositories.CreateArticle(&article)
|
err := repositories.CreateArticle(&article)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return dto.ArticleResponse{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &dto.ArticleResponse{
|
articleResponse := dto.ArticleResponse{
|
||||||
ID: article.ID,
|
ID: article.ID,
|
||||||
Title: article.Title,
|
Title: article.Title,
|
||||||
CoverImage: article.CoverImage,
|
CoverImage: article.CoverImage,
|
||||||
|
@ -33,7 +33,9 @@ func CreateArticle(articleRequest *dto.ArticleRequest) (*dto.ArticleResponse, er
|
||||||
Content: article.Content,
|
Content: article.Content,
|
||||||
PublishedAt: article.PublishedAt,
|
PublishedAt: article.PublishedAt,
|
||||||
UpdatedAt: article.UpdatedAt,
|
UpdatedAt: article.UpdatedAt,
|
||||||
}, nil
|
}
|
||||||
|
|
||||||
|
return articleResponse, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetArticles() ([]dto.ArticleResponse, error) {
|
func GetArticles() ([]dto.ArticleResponse, error) {
|
||||||
|
|
|
@ -10,7 +10,7 @@ import (
|
||||||
func GetUserRoleByID(id string) (domain.UserRole, error) {
|
func GetUserRoleByID(id string) (domain.UserRole, error) {
|
||||||
role, err := repositories.GetUserRoleByID(id)
|
role, err := repositories.GetUserRoleByID(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return role, errors.New("UserRole tidak ditemukan")
|
return role, errors.New("userRole tidak ditemukan")
|
||||||
}
|
}
|
||||||
return role, nil
|
return role, nil
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ func GetUserRoleByID(id string) (domain.UserRole, error) {
|
||||||
func GetAllUserRoles() ([]domain.UserRole, error) {
|
func GetAllUserRoles() ([]domain.UserRole, error) {
|
||||||
roles, err := repositories.GetAllUserRoles()
|
roles, err := repositories.GetAllUserRoles()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New("Gagal mengambil data UserRole")
|
return nil, errors.New("gagal mengambil data UserRole")
|
||||||
}
|
}
|
||||||
return roles, nil
|
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