/
api_mutes.go
157 lines (136 loc) · 3.77 KB
/
api_mutes.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* UsagiBooru Accounts API
*
* Accounts related api (required)
*
* API version: 2.0
* Contact: dsgamer777@gmail.com
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/
package gen
import (
"encoding/json"
"net/http"
"strings"
"github.com/gorilla/mux"
)
// A MutesApiController binds http requests to an api service and writes the service results to the http response
type MutesApiController struct {
service MutesApiServicer
}
// NewMutesApiController creates a default api controller
func NewMutesApiController(s MutesApiServicer) Router {
return &MutesApiController{service: s}
}
// Routes returns all of the api route for the MutesApiController
func (c *MutesApiController) Routes() Routes {
return Routes{
{
"AddMute",
strings.ToUpper("Post"),
"/accounts/{accountID}/mutes",
c.AddMute,
},
{
"DeleteMute",
strings.ToUpper("Delete"),
"/accounts/{accountID}/mutes/{muteID}",
c.DeleteMute,
},
{
"GetMute",
strings.ToUpper("Get"),
"/accounts/{accountID}/mutes/{muteID}",
c.GetMute,
},
{
"GetMutes",
strings.ToUpper("Get"),
"/accounts/{accountID}/mutes",
c.GetMutes,
},
}
}
// AddMute - Add mute
func (c *MutesApiController) AddMute(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
accountID, err := parseInt32Parameter(params["accountID"])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
muteStruct := &MuteStruct{}
if err := json.NewDecoder(r.Body).Decode(&muteStruct); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
result, err := c.service.AddMute(r.Context(), accountID, *muteStruct)
//If an error occurred, encode the error with the status code
if err != nil {
EncodeJSONResponse(err.Error(), &result.Code, w)
return
}
//If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}
// DeleteMute - Delete mute
func (c *MutesApiController) DeleteMute(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
accountID, err := parseInt32Parameter(params["accountID"])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
muteID, err := parseInt32Parameter(params["muteID"])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
result, err := c.service.DeleteMute(r.Context(), accountID, muteID)
//If an error occurred, encode the error with the status code
if err != nil {
EncodeJSONResponse(err.Error(), &result.Code, w)
return
}
//If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}
// GetMute - Get mute
func (c *MutesApiController) GetMute(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
accountID, err := parseInt32Parameter(params["accountID"])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
muteID, err := parseInt32Parameter(params["muteID"])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
result, err := c.service.GetMute(r.Context(), accountID, muteID)
//If an error occurred, encode the error with the status code
if err != nil {
EncodeJSONResponse(err.Error(), &result.Code, w)
return
}
//If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}
// GetMutes - Get mute list
func (c *MutesApiController) GetMutes(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
accountID, err := parseInt32Parameter(params["accountID"])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
result, err := c.service.GetMutes(r.Context(), accountID)
//If an error occurred, encode the error with the status code
if err != nil {
EncodeJSONResponse(err.Error(), &result.Code, w)
return
}
//If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}