diff --git a/dto/article_dto.go b/dto/article_dto.go index b947674..dea36b8 100644 --- a/dto/article_dto.go +++ b/dto/article_dto.go @@ -11,7 +11,7 @@ type ArticleResponseDTO struct { Author string `json:"author"` Heading string `json:"heading"` Content string `json:"content"` - PublishedAt string `json:"createdAt"` + PublishedAt string `json:"publishedAt"` UpdatedAt string `json:"updatedAt"` } diff --git a/internal/handler/article_handler.go b/internal/handler/article_handler.go index 6040324..d2d9751 100644 --- a/internal/handler/article_handler.go +++ b/internal/handler/article_handler.go @@ -1,6 +1,8 @@ package handler import ( + "strconv" + "github.com/gofiber/fiber/v2" "github.com/pahmiudahgede/senggoldong/dto" "github.com/pahmiudahgede/senggoldong/internal/services" @@ -33,3 +35,27 @@ func (h *ArticleHandler) CreateArticle(c *fiber.Ctx) error { return utils.CreateResponse(c, articleResponse, "Article created successfully") } + +func (h *ArticleHandler) GetAllArticles(c *fiber.Ctx) error { + + page, err := strconv.Atoi(c.Query("page", "0")) + if err != nil { + page = 0 + } + limit, err := strconv.Atoi(c.Query("limit", "0")) + if err != nil { + limit = 0 + } + + article, totalArticle, err := h.ArticleService.GetAllArticles(page, limit) + if err != nil { + return utils.GenericErrorResponse(c, fiber.StatusInternalServerError, "Failed to fetch article") + } + + if page > 0 && limit > 0 { + return utils.PaginatedResponse(c, article, page, limit, totalArticle, "Article fetched successfully") + } + + return utils.NonPaginatedResponse(c, article, totalArticle, "Article fetched successfully") + +} diff --git a/internal/services/article_service.go b/internal/services/article_service.go index 3572d3b..6fff972 100644 --- a/internal/services/article_service.go +++ b/internal/services/article_service.go @@ -12,6 +12,7 @@ import ( type ArticleService interface { CreateArticle(articleDTO dto.RequestArticleDTO) (*dto.ArticleResponseDTO, error) + GetAllArticles(page, limit int) ([]dto.ArticleResponseDTO, int, error) } type articleService struct { @@ -62,3 +63,63 @@ func (s *articleService) CreateArticle(articleDTO dto.RequestArticleDTO) (*dto.A return articleResponseDTO, nil } + +func (s *articleService) GetAllArticles(page, limit int) ([]dto.ArticleResponseDTO, int, error) { + + cacheKey := fmt.Sprintf("articles_page:%d_limit:%d", page, limit) + + cachedData, err := utils.GetJSONData(cacheKey) + if err == nil && cachedData != nil { + var articles []dto.ArticleResponseDTO + if data, ok := cachedData["data"].([]interface{}); ok { + for _, item := range data { + articleData, ok := item.(map[string]interface{}) + if ok { + articles = append(articles, dto.ArticleResponseDTO{ + ID: articleData["article_id"].(string), + Title: articleData["title"].(string), + CoverImage: articleData["coverImage"].(string), + Author: articleData["author"].(string), + Heading: articleData["heading"].(string), + Content: articleData["content"].(string), + PublishedAt: articleData["publishedAt"].(string), + UpdatedAt: articleData["updatedAt"].(string), + }) + } + } + return articles, len(articles), nil + } + } + + articles, total, err := s.ArticleRepo.FindAllArticles(page, limit) + if err != nil { + return nil, 0, fmt.Errorf("failed to fetch articles: %v", err) + } + + var articleDTOs []dto.ArticleResponseDTO + for _, article := range articles { + publishedAt, _ := utils.FormatDateToIndonesianFormat(article.PublishedAt) + updatedAt, _ := utils.FormatDateToIndonesianFormat(article.UpdatedAt) + + articleDTOs = append(articleDTOs, dto.ArticleResponseDTO{ + ID: article.ID, + Title: article.Title, + CoverImage: article.CoverImage, + Author: article.Author, + Heading: article.Heading, + Content: article.Content, + PublishedAt: publishedAt, + UpdatedAt: updatedAt, + }) + } + + cacheData := map[string]interface{}{ + "data": articleDTOs, + } + err = utils.SetJSONData(cacheKey, cacheData, time.Hour*24) + if err != nil { + fmt.Printf("Error caching articles to Redis: %v\n", err) + } + + return articleDTOs, total, nil +} diff --git a/presentation/article_route.go b/presentation/article_route.go index 8610a19..6fae6a4 100644 --- a/presentation/article_route.go +++ b/presentation/article_route.go @@ -18,4 +18,5 @@ func ArticleRouter(api fiber.Router) { articleAPI := api.Group("/article-rijik") articleAPI.Post("/create-article", middleware.AuthMiddleware, middleware.RoleMiddleware(utils.RoleAdministrator), articleHandler.CreateArticle) + articleAPI.Get("/view-article", middleware.AuthMiddleware, articleHandler.GetAllArticles) }