/
api_timeline.go
134 lines (116 loc) · 3.46 KB
/
api_timeline.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
/*
* 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 TimelineApiController binds http requests to an api service and writes the service results to the http response
type TimelineApiController struct {
service TimelineApiServicer
}
// NewTimelineApiController creates a default api controller
func NewTimelineApiController(s TimelineApiServicer) Router {
return &TimelineApiController{service: s}
}
// Routes returns all of the api route for the TimelineApiController
func (c *TimelineApiController) Routes() Routes {
return Routes{
{
"FollowArtist",
strings.ToUpper("Post"),
"/accounts/{accountID}/timeline/follow",
c.FollowArtist,
},
{
"GetFollowingArtists",
strings.ToUpper("Get"),
"/accounts/{accountID}/timeline",
c.GetFollowingArtists,
},
{
"UnfollowArtist",
strings.ToUpper("Post"),
"/accounts/{accountID}/timeline/unfollow",
c.UnfollowArtist,
},
}
}
// FollowArtist - Follow artist
func (c *TimelineApiController) FollowArtist(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
accountID, err := parseInt32Parameter(params["accountID"])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
lightArtistStruct := &LightArtistStruct{}
if err := json.NewDecoder(r.Body).Decode(&lightArtistStruct); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
result, err := c.service.FollowArtist(r.Context(), accountID, *lightArtistStruct)
//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)
}
// GetFollowingArtists - Get timeline followings
func (c *TimelineApiController) GetFollowingArtists(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
query := r.URL.Query()
accountID, err := parseInt32Parameter(params["accountID"])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
sort := query.Get("sort")
order := query.Get("order")
page, err := parseInt32Parameter(query.Get("page"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
result, err := c.service.GetFollowingArtists(r.Context(), accountID, sort, order, page)
//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)
}
// UnfollowArtist - Unfollow artist
func (c *TimelineApiController) UnfollowArtist(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
accountID, err := parseInt32Parameter(params["accountID"])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
lightArtistStruct := &LightArtistStruct{}
if err := json.NewDecoder(r.Body).Decode(&lightArtistStruct); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
result, err := c.service.UnfollowArtist(r.Context(), accountID, *lightArtistStruct)
//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)
}