-
Notifications
You must be signed in to change notification settings - Fork 1
/
statement.go
143 lines (109 loc) · 3.1 KB
/
statement.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package v1
import (
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/jinzhu/gorm"
"github.com/neverhaveiever-io/api/internal/app"
"github.com/neverhaveiever-io/api/internal/category"
"github.com/neverhaveiever-io/api/internal/statement"
"github.com/neverhaveiever-io/api/pkg/problem"
"github.com/neverhaveiever-io/api/pkg/unique"
"net/http"
"strings"
)
func AddStatement(ctx *gin.Context) {
g := app.Gin{C: ctx}
var s statement.Statement
if err := ctx.ShouldBind(&s); err != nil {
g.ErrorResponse(problem.Default(http.StatusBadRequest))
return
}
if err := s.Validate(); err != nil {
g.ErrorResponse(problem.ValidationError(err))
return
}
if err := s.Save(); err != nil {
_ = g.C.Error(err)
g.ErrorResponse(problem.Default(http.StatusInternalServerError))
return
}
g.Response(http.StatusCreated, s)
}
func GetStatement(ctx *gin.Context) {
if r := strings.Split(ctx.Request.URL.String(), "/random"); len(r) == 2 { // && r[1] == ""
GetRandomStatement(ctx)
} else {
GetStatementByID(ctx)
}
}
func GetStatementByID(ctx *gin.Context) {
g := app.Gin{C: ctx}
// ctx.Params.ByName("id") returns an empty string if no matching key is found
id, err := uuid.Parse(ctx.Params.ByName("id"))
if err != nil {
g.ErrorResponse(problem.Default(http.StatusBadRequest))
return
}
s, err := statement.GetByID(id)
if err != nil {
if gorm.IsRecordNotFoundError(err) {
g.ErrorResponse(problem.NoSuchStatement())
return
}
_ = g.C.Error(err)
g.ErrorResponse(problem.Default(http.StatusInternalServerError))
return
}
g.Response(http.StatusOK, s)
}
func GetRandomStatement(ctx *gin.Context) {
g := app.Gin{C: ctx}
var s statement.Statement
var categories []category.Category
for _, v := range unique.Strings(append(ctx.QueryArray("category"), ctx.QueryArray("category[]")...)) {
c := category.Category(v)
if err := c.Validate(); err != nil {
g.ErrorResponse(problem.ValidationError(err))
return
}
categories = append(categories, c)
}
s, err := statement.GetRandomByCategory(category.GetRandom())
if err != nil {
_ = g.C.Error(err)
g.ErrorResponse(problem.Default(http.StatusInternalServerError))
return
}
// a redirect might make sense but the resulting round trip is just not worth it
g.Response(http.StatusOK, s)
}
func DeleteStatement(ctx *gin.Context) {
g := app.Gin{C: ctx}
// ctx.Params.ByName("id") returns an empty string if no matching key is found
id, err := uuid.Parse(ctx.Params.ByName("id"))
if err != nil {
g.ErrorResponse(problem.Default(http.StatusBadRequest))
return
}
s, err := statement.GetByID(id)
if err != nil {
if gorm.IsRecordNotFoundError(err) {
g.ErrorResponse(problem.NoSuchStatement())
return
}
_ = g.C.Error(err)
g.ErrorResponse(problem.Default(http.StatusInternalServerError))
return
}
if err := s.Delete(); err != nil {
_ = g.C.Error(err)
g.ErrorResponse(problem.Default(http.StatusInternalServerError))
return
}
g.Response(http.StatusNoContent)
}
func EditStatement(ctx *gin.Context) {
g := app.Gin{C: ctx}
// TODO: implement
g.ErrorResponse(problem.Default(http.StatusNotImplemented))
}