/
render.go
86 lines (66 loc) · 1.94 KB
/
render.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
/*
* Copyright (c) 2022-2023. Author: Ruslan Bikchentaev. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
* Перший приватний програміст.
*/
package crud
import (
"bytes"
"fmt"
"strings"
"github.com/valyala/fasthttp"
"github.com/ruslanBik4/httpgo/views"
)
func RenderArrayResult[T any](ctx *fasthttp.RequestCtx, res []T, err error) (any, error) {
if err != nil {
return CreateErrResult(err)
}
if len(res) == 0 {
ctx.SetStatusCode(fasthttp.StatusNoContent)
return nil, nil
}
views.WriteJSONHeaders(ctx)
return res, nil
}
func RenderAny[T any](ctx *fasthttp.RequestCtx, res T, err error) (any, error) {
if err != nil {
return CreateErrResult(err)
}
views.WriteJSONHeaders(ctx)
return res, nil
}
func RenderCreatedResult(ctx *fasthttp.RequestCtx, id int64, buf *bytes.Buffer, colSel []string, url string) (any, error) {
msg := fmt.Sprintf("Success insert: %s, values:\n%s", strings.Join(colSel, ", "), buf.String())
ctx.SetStatusCode(fasthttp.StatusCreated)
if res, ok := createResponse(ctx, msg, url+"/form?html"); ok {
res.Id = id
return res, nil
}
return id, nil
}
func RenderAcceptedResult(ctx *fasthttp.RequestCtx, colSel []string, buf *bytes.Buffer, url string) (any, error) {
msg := fmt.Sprintf("Success update: %s, values:\n%s", strings.Join(colSel, ", "), buf.String())
ctx.SetStatusCode(fasthttp.StatusAccepted)
if res, ok := createResponse(ctx, msg, url+"/browse"); ok {
return res, nil
}
return msg, nil
}
func createResponse(ctx *fasthttp.RequestCtx, msg, url string) (*insertResult, bool) {
if g, ok := ctx.UserValue(ParamsGetFormActions.Name).(bool); ok && g {
if l, ok := ctx.UserValue(ParamsLang.Name).(string); ok {
url += "?lang=" + l
}
return &insertResult{
FormActions: []FormActions{
{
Typ: "redirect",
Url: url,
},
},
Msg: msg,
}, true
}
return nil, false
}