12
var NotAllowedComment = errors.New("backend: not allowed comment")
14
type CommentatorDTO struct {
15
Name string `json:"name"`
16
Email string `json:"email"`
17
Website string `json:"website"`
20
type CommentUserDTO struct {
24
type CommentDTO struct {
25
Commentator *CommentatorDTO `json:"commentator,omitempty"`
26
User *CommentUserDTO `json:"user,omitempty"`
27
Text string `json:"text"`
28
TopicID int `json:"topicId"`
29
ParentID int `json:"parentId"`
30
UserAgent string `json:"userAgent"`
31
IP string `json:"ipAddress"`
34
type CreatedCommentDTO struct {
36
Text string `json:"text"`
37
Name string `json:"username"`
38
Email string `json:"email"`
39
Website string `json:"website"`
40
Country string `json:"countryFlag"`
43
type CreateCommentResponse struct {
44
Violations []FormError `json:"errors,omitempty"`
45
Comment *CreatedCommentDTO `json:"comment,omitempty"`
48
func SendComment(comment CommentDTO) (*CreateCommentResponse, error) {
49
var result CreateCommentResponse
50
jsonBody, err := json.Marshal(comment)
55
request, err := http.NewRequest(http.MethodPost, apiURL()+"/api/comments/external", bytes.NewReader(jsonBody))
60
request.Header.Set("Content-Type", "application/json")
62
response, err := send(request)
67
if response.StatusCode == http.StatusForbidden {
68
return nil, NotAllowedComment
71
defer response.Body.Close()
72
buf, err := io.ReadAll(response.Body)
78
return nil, errors.New("invalid JSON string")
81
err = json.Unmarshal(buf, &result)
89
func PingGeolocation() {
90
request, err := http.NewRequest(http.MethodPost, apiURL()+"/api/comments/geo-location", http.NoBody)
98
func RefreshComment(id int) (*CreatedCommentDTO, error) {
99
request, err := http.NewRequest(http.MethodGet, apiURL()+"/api/comments/"+strconv.Itoa(id), http.NoBody)
104
response, err := send(request)
109
if response.StatusCode != http.StatusOK {
110
return nil, errors.New("status not OK")
113
defer response.Body.Close()
114
buf, err := io.ReadAll(response.Body)
119
if !json.Valid(buf) {
120
return nil, errors.New("invalid JSON string")
123
var result = struct {
124
Comment *CreatedCommentDTO `json:"comment,omitempty"`
126
err = json.Unmarshal(buf, &result)
131
return result.Comment, nil