add vote infra and ui prep for connection ui to backend

This commit is contained in:
2024-12-01 23:43:52 -07:00
parent a1a112ed11
commit 091e55fc2f
11 changed files with 197 additions and 31 deletions

View File

@ -15,6 +15,7 @@ type Server interface {
ShortenLinkHandler(w http.ResponseWriter, r *http.Request)
AddComment(w http.ResponseWriter, r *http.Request)
ListComments(w http.ResponseWriter, r *http.Request)
AddVote(w http.ResponseWriter, r *http.Request)
}
type server struct {
@ -109,7 +110,11 @@ func (s *server) AddComment(w http.ResponseWriter, r *http.Request) {
}
func (s *server) ListComments(w http.ResponseWriter, r *http.Request) {
comments, err := s.mapper.GetComments(r.URL.Query().Get("url"))
orderBy := "created_at" // can be "upvotes" or "created_at"
if r.URL.Query().Get("order_by") != "" {
orderBy = r.URL.Query().Get("order_by")
}
comments, err := s.mapper.GetComments(r.URL.Query().Get("url"), orderBy)
if err != nil {
http.Error(w, fmt.Sprintf("error getting comments: %s", err), http.StatusInternalServerError)
return
@ -126,6 +131,18 @@ func (s *server) ListComments(w http.ResponseWriter, r *http.Request) {
}
}
func (s *server) AddVote(w http.ResponseWriter, r *http.Request) {
// TODO: get userID from the authentication token
userID := "u_12345"
err := s.mapper.AddVote(r.URL.Query().Get("id"), userID, r.URL.Query().Get("vote"))
if err != nil {
http.Error(w, fmt.Sprintf("error adding vote: %s", err), http.StatusInternalServerError)
return
}
}
// CorsMiddleware is a middleware that allows CORS
func CorsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {