-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweetController.go
130 lines (102 loc) · 3.21 KB
/
tweetController.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
package controllers
import (
"encoding/json"
"net/http"
"strconv"
"time"
"github.com/jgromero7/gos-twitter/app/models"
jwtServices "github.com/jgromero7/gos-twitter/jwt"
)
// CreateTweet stora a register tweet
func CreateTweet(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-type", "application/json")
var tweet models.Tweet
err := json.NewDecoder(r.Body).Decode(&tweet)
if err != nil {
http.Error(w, "Content Invalid", http.StatusBadRequest)
return
}
auxTweet := models.Tweet{
UserID: jwtServices.UserID,
Message: tweet.Message,
CreateAt: time.Now(),
}
_, status, err := models.Create(auxTweet)
if err != nil {
http.Error(w, "An error occurred while trying an error inserting a record", http.StatusConflict)
return
}
if status == false {
http.Error(w, "Cannot perform to user storage", http.StatusConflict)
return
}
w.Header().Set("Content-type", "application/json")
w.WriteHeader(http.StatusOK)
}
// ReadTweet Listing all result
func ReadTweet(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-type", "application/json")
UserID := r.URL.Query().Get("userid")
if len(UserID) < 1 {
http.Error(w, "The param userid is required", http.StatusNotAcceptable)
return
}
if len(r.URL.Query().Get("page")) < 1 {
http.Error(w, "The param page is required", http.StatusNotAcceptable)
return
}
Page, err := strconv.Atoi(r.URL.Query().Get("page"))
if err != nil {
http.Error(w, "the page parameter must contain a value", http.StatusNotAcceptable)
return
}
numberPage := int64(Page)
tweets, status := models.Read(UserID, numberPage)
if status == false {
http.Error(w, "error reading tweets", http.StatusBadRequest)
}
w.Header().Set("Content-type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(tweets)
}
// ReadTweetFollowers Listing all result specific user
func ReadTweetFollowers(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-type", "application/json")
if len(r.URL.Query().Get("page")) < 1 {
http.Error(w, "The param page is required", http.StatusNotAcceptable)
return
}
Page, err := strconv.Atoi(r.URL.Query().Get("page"))
if err != nil {
http.Error(w, "the page parameter must contain a value", http.StatusNotAcceptable)
return
}
numberPage := int64(Page)
tweetsFollowers, status := models.ReadTweetFollowers(jwtServices.UserID, numberPage)
if status == false {
http.Error(w, "error reading tweets followers", http.StatusBadRequest)
}
w.Header().Set("Content-type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(tweetsFollowers)
}
// DeleteTweet delete a register tweet
func DeleteTweet(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-type", "application/json")
ID := r.URL.Query().Get("id")
if len(ID) < 1 {
http.Error(w, "The param id is required", http.StatusNotAcceptable)
return
}
UserID := r.URL.Query().Get("userid")
if len(UserID) < 1 {
http.Error(w, "The param userid is required", http.StatusNotAcceptable)
return
}
err := models.Delete(ID, UserID)
if err != nil {
http.Error(w, "error deleting tweet", http.StatusBadRequest)
}
w.Header().Set("Content-type", "application/json")
w.WriteHeader(http.StatusOK)
}