-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_chirp.go
More file actions
38 lines (32 loc) · 864 Bytes
/
Copy pathget_chirp.go
File metadata and controls
38 lines (32 loc) · 864 Bytes
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
package main
import (
"database/sql"
"errors"
"net/http"
"time"
"github.com/google/uuid"
)
type chirpResponse struct {
ID uuid.UUID `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Body string `json:"body"`
UserID uuid.UUID `json:"user_id"`
}
func (cfg *apiConfig) handlerGetChirp(w http.ResponseWriter, r *http.Request) {
chirpID, err := uuid.Parse(r.PathValue("chirpID"))
if err != nil {
respondWithError(w, http.StatusBadRequest, "invalid chirp ID")
return
}
chirp, err := cfg.db.GetChirp(r.Context(), chirpID)
if errors.Is(err, sql.ErrNoRows) {
respondWithError(w, http.StatusNotFound, "chirp not found")
return
}
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
return
}
respondWithJSON(w, http.StatusOK, chirpResponse(chirp))
}