-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.comments.go
49 lines (39 loc) · 1 KB
/
handlers.comments.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
package main
import (
"io"
"log"
"net/http"
"strconv"
)
func handleCommentPost(w http.ResponseWriter, r *http.Request) {
recipe := r.URL.Query().Get("recipe")
username := getUsername(r)
if username == "" {
log.Println("User is not logged in")
return
}
comment := r.FormValue("comment")
rating, err := strconv.Atoi(r.FormValue("rating"))
if err != nil {
log.Println("Error converting rating to int", err)
return
}
addComment(recipe, Comment{username, rating, comment})
log.Println("Saved", recipe, comment, rating)
io.WriteString(w, "Success")
}
// func handleCommentGet(w http.ResponseWriter, r *http.Request) {
// recipe := r.URL.Query().Get("recipe")
// if recipe == "" {
// log.Println("Cannot get comments: Recipe is not specified")
// return
// }
// comments := getComments(recipe)
// // Marshal and return
// commentsData, err := json.Marshal(comments)
// if err != nil {
// log.Println("Error marshalling comments", recipe)
// return
// }
// w.Write(commentsData)
// }