-
Notifications
You must be signed in to change notification settings - Fork 0
/
entities_response.go
98 lines (83 loc) · 2.97 KB
/
entities_response.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package snippets
import (
"net/http"
"time"
"github.com/titusjaka/go-sample/internal/infrastructure/service"
)
// snippetResponse represents a common snippet-response struct
type snippetResponse struct {
ID uint `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt time.Time `json:"expires_at"`
}
// listSnippetsResponse represents a response struct for GET /snippets?limit=<x>&offset=<y> method
type listSnippetsResponse struct {
Snippets []snippetResponse `json:"snippets,omitempty"`
Pagination service.Pagination `json:"pagination"`
err *service.Error
}
// ServiceError is an implementation of api.ServiceErrorer interface.
// It's used to handle business-logic errors as HTTP responses
func (r *listSnippetsResponse) ServiceError() *service.Error {
return r.err
}
// getSnippetResponse represents a response struct for GET /snippets/{snippet_id} method
type getSnippetResponse struct {
snippetResponse
err *service.Error
}
// ServiceError is an implementation of api.ServiceErrorer interface.
// It's used to handle business-logic errors as HTTP responses
func (r *getSnippetResponse) ServiceError() *service.Error {
return r.err
}
// createSnippetResponse represents a response struct for POST /snippets method
type createSnippetResponse struct {
snippetResponse
err *service.Error
}
// StatusCode is an implementation of go-kit StatusCoder interface.
// It's used to override default 200 OK response
func (r *createSnippetResponse) StatusCode() int {
return http.StatusCreated
}
// ServiceError is an implementation of api.ServiceErrorer interface.
// It's used to handle business-logic errors as HTTP responses
func (r *createSnippetResponse) ServiceError() *service.Error {
return r.err
}
// deleteSnippetResponse represents a response struct for DELETE /snippets/{snippet_id} method
type deleteSnippetResponse struct {
err *service.Error
}
// StatusCode is an implementation of go-kit StatusCoder interface.
// It's used to override default 200 OK response
func (r *deleteSnippetResponse) StatusCode() int {
return http.StatusNoContent
}
// ServiceError is an implementation of api.ServiceErrorer interface.
// It's used to handle business-logic errors as HTTP responses
func (r *deleteSnippetResponse) ServiceError() *service.Error {
return r.err
}
// convertToListSnippetsResponse is used to map []Snippet -> []snippetResponse
func convertToListSnippetsResponse(snippets []Snippet) []snippetResponse {
response := make([]snippetResponse, len(snippets))
for i := range snippets {
response[i] = convertToSnippetResponse(snippets[i])
}
return response
}
// convertToSnippetResponse is used to map Snippet -> snippetResponse
func convertToSnippetResponse(snippet Snippet) snippetResponse {
// nolint:gocritic
return snippetResponse{
ID: snippet.ID,
Title: snippet.Title,
Content: snippet.Content,
CreatedAt: snippet.CreatedAt,
ExpiresAt: snippet.ExpiresAt,
}
}