-
Notifications
You must be signed in to change notification settings - Fork 2
/
factory.go
55 lines (44 loc) · 1.43 KB
/
factory.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
package response
import (
"context"
"net/http"
)
func NewDummyFactory(fw FormatWriter, verbosity bool) *DummyFactory {
return &DummyFactory{fw: fw, verbosity: verbosity}
}
type DummyFactory struct {
fw FormatWriter
verbosity bool
}
func (f *DummyFactory) FormatWriter() FormatWriter {
return f.fw
}
func (f *DummyFactory) Response(_ context.Context, statusCode int, data any) *DataResponse {
return NewDataResponse(statusCode, data)
}
func (f *DummyFactory) SuccessResponse(ctx context.Context, data any) *DataResponse {
return f.Response(ctx, http.StatusOK, data)
}
func (f *DummyFactory) InternalServerErrorResponse(ctx context.Context, err error) *DataResponse {
var message string
if f.verbosity {
message = err.Error()
} else {
message = http.StatusText(http.StatusInternalServerError)
}
return f.Response(ctx, http.StatusInternalServerError, message)
}
func (f *DummyFactory) UnprocessableEntityResponse(
ctx context.Context,
message string,
attributesErrors map[string][]string,
) *DataResponse {
// TODO: convert attributes to string
return f.Response(ctx, http.StatusUnprocessableEntity, message)
}
func (f *DummyFactory) NotFoundEntityResponse(ctx context.Context, message string) *DataResponse {
return f.Response(ctx, http.StatusOK, "NotFound: "+message)
}
func (f *DummyFactory) ErrorResponse(ctx context.Context, statusCode int, message string) *DataResponse {
return f.Response(ctx, statusCode, message)
}