-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_all_blogs.go
More file actions
73 lines (66 loc) · 2.16 KB
/
get_all_blogs.go
File metadata and controls
73 lines (66 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package http
import (
"net/http"
"time"
"github.com/hammer-code/lms-be/domain"
"github.com/hammer-code/lms-be/utils"
"github.com/sirupsen/logrus"
)
// GetAllBlogPosts implements domain.BlogPostHandler.
func (h Handler) GetAllBlogPosts(w http.ResponseWriter, r *http.Request) {
// Ambil parameter pagination dari request
pagination, err := domain.GetPaginationFromCtx(r)
if err != nil {
logrus.Error("failed to parse pagination parameters: ", err)
utils.Response(domain.HttpResponse{
Code: http.StatusBadRequest,
Message: "Invalid pagination parameters",
}, w)
return
}
// Panggil usecase dengan parameter pagination
data, paginationResponse, err := h.usecase.GetAllBlogPosts(r.Context(), pagination)
if err != nil {
resp := utils.CustomErrorResponse(err)
utils.Response(resp, w)
return
}
type response struct {
Id int `json:"id" gorm:"primaryKey"`
Title string `json:"title"`
Excerpt string `json:"excerpt"`
Author domain.Author `json:"author" gorm:"foreignKey:AuthorID;references:UserId"`
AuthorID int `json:"author_id" gorm:"column:author_id"`
Tags []string `json:"tags" gorm:"-"`
Category string `json:"category"`
Status string `json:"status" gorm:"type:enum('draft', 'published', 'archived')"`
Slug string `json:"slug"`
PublishedAt *time.Time `json:"published_at"`
UpdatedAt *time.Time `json:"updated_at"`
CreatedAt *time.Time `json:"created_at"`
}
responseDTO := []response{}
for _, post := range data {
resp := response{
Id: post.Id,
Title: post.Title,
Excerpt: post.Excerpt,
Author: post.Author,
AuthorID: post.AuthorID,
Tags: post.Tags,
Category: post.Category,
Status: post.Status,
Slug: post.Slug,
PublishedAt: post.PublishedAt,
UpdatedAt: post.UpdatedAt,
CreatedAt: post.CreatedAt,
}
responseDTO = append(responseDTO, resp)
}
utils.Response(domain.HttpResponse{
Code: http.StatusOK,
Message: "Blog posts retrieved successfully",
Data: responseDTO,
Pagination: &paginationResponse,
}, w)
}