/
api_mylist.go
93 lines (80 loc) · 2.28 KB
/
api_mylist.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
/*
* 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 MylistApiController binds http requests to an api service and writes the service results to the http response
type MylistApiController struct {
service MylistApiServicer
}
// NewMylistApiController creates a default api controller
func NewMylistApiController(s MylistApiServicer) Router {
return &MylistApiController{service: s}
}
// Routes returns all of the api route for the MylistApiController
func (c *MylistApiController) Routes() Routes {
return Routes{
{
"CreateMylist",
strings.ToUpper("Post"),
"/accounts/{accountID}/mylists",
c.CreateMylist,
},
{
"GetUserMylists",
strings.ToUpper("Get"),
"/accounts/{accountID}/mylists",
c.GetUserMylists,
},
}
}
// CreateMylist - Create user mylist
func (c *MylistApiController) CreateMylist(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
accountID, err := parseInt32Parameter(params["accountID"])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
mylistStruct := &MylistStruct{}
if err := json.NewDecoder(r.Body).Decode(&mylistStruct); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
result, err := c.service.CreateMylist(r.Context(), accountID, *mylistStruct)
//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)
}
// GetUserMylists - Get user mylists
func (c *MylistApiController) GetUserMylists(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.GetUserMylists(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)
}