-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_tweets.go
152 lines (134 loc) · 4.24 KB
/
handler_tweets.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
package main
import (
"bytes"
"fmt"
"htmxx/model"
"htmxx/service"
"htmxx/templ"
"net/http"
"strconv"
"strings"
"time"
)
func (h *application) GetTweet(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
// handle error
http.Error(w, "Invalid tweet ID", http.StatusBadRequest)
return
}
tweet, err := h.GetTweetData(id, r.Context())
if err != nil {
// handle error
fmt.Println(err)
http.Error(w, "Tweet not found", http.StatusNotFound)
return
}
// handle tweet
tweetComponent := templ.IndivTweet(tweet, r.Context().Value("user").(string))
err = templ.Layout(tweetComponent, "Tweet", false).Render(r.Context(), w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func (h *application) CreateTweet(w http.ResponseWriter, r *http.Request) {
var content = r.FormValue("content")
// envrypt the ip
author := r.Context().Value("user").(string)
tweet := &model.Tweet{
Author: author,
Content: content,
}
newid, err := h.CreateTweetData(tweet, r.Context())
if err != nil {
// handle error
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tweet.ID = newid
tweet.Created = time.Now().UTC()
// handle success
var stringTmpl bytes.Buffer
insertTweetComponent := templ.InsertTweet(tweet)
insertTweetComponent.Render(r.Context(), &stringTmpl)
h.eventsService.AddMessage(service.Event{Data: fmt.Sprintf("%s", strings.ReplaceAll(stringTmpl.String(), "\n", "")), EventName: "new-tweet"})
http.Error(w, "", http.StatusCreated)
confirmationContainer := templ.CreateNewTweetConfirmation()
confirmationContainer.Render(r.Context(), w)
}
func (h *application) AddLike(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
// handle error
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
newLikeCount, likedBySelf, err := h.AddLikeData(id, r.Context())
if err != nil {
// handle error
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// handle success
h.eventsService.AddMessage(service.Event{Data: fmt.Sprintf("%d", newLikeCount), EventName: fmt.Sprintf("new-like-count-%d", id)})
heartComponent := templ.Heart(id, likedBySelf, newLikeCount, true)
// fmt.Fprintf(w, "%d", newLikeCount)
heartComponent.Render(r.Context(), w)
}
func (h *application) RemoveLike(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
// handle error
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
newLikeCount, likedBySelf, err := h.RemoveLikeData(id, r.Context())
if err != nil {
// handle error
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// handle success
h.eventsService.AddMessage(service.Event{Data: fmt.Sprintf("%d", newLikeCount), EventName: fmt.Sprintf("new-like-count-%d", id)})
heartComponent := templ.Heart(id, likedBySelf, newLikeCount, false)
heartComponent.Render(r.Context(), w)
}
func (h *application) DeleteTweet(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
// handle error
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
deletedID, err := h.DeleteTweetData(id, r.Context())
if err != nil || deletedID == 0 {
// handle error
http.Error(w, err.Error(), http.StatusForbidden)
return
}
// handle success
deleteTweetComponent := templ.DeletedTweet(deletedID)
deleteTweetComponent.Render(r.Context(), w)
}
func (h *application) ReplyTweet(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
// handle error
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
content := r.FormValue("content")
tweetid, err := h.ReplyTweetData(id, content, r.Context()) // handle success
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tweet, err := h.GetTweetData(tweetid, r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
newTweetComponent := templ.Tweet(tweet[0], true)
newTweetComponent.Render(r.Context(), w)
http.Error(w, "", http.StatusCreated)
}