jokes-bapak2/api/handler/joke/get.go

64 lines
1.7 KiB
Go
Raw Normal View History

2022-09-03 10:53:46 +00:00
package joke
import (
core "jokes-bapak2-api/core/joke"
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
)
func (d *Dependencies) TodayJoke(w http.ResponseWriter, r *http.Request) {
joke, contentType, err := core.GetTodaysJoke(r.Context(), d.Bucket, d.Redis, d.Memory)
if err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"error":` + strconv.Quote(err.Error()) + `}`))
return
}
w.Header().Set("Content-Type", contentType)
w.WriteHeader(http.StatusOK)
w.Write(joke)
}
func (d *Dependencies) SingleJoke(w http.ResponseWriter, r *http.Request) {
joke, contentType, err := core.GetRandomJoke(r.Context(), d.Bucket, d.Redis, d.Memory)
if err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"error":` + strconv.Quote(err.Error()) + `}`))
return
}
w.Header().Set("Content-Type", contentType)
w.WriteHeader(http.StatusOK)
w.Write(joke)
}
func (d *Dependencies) JokeByID(w http.ResponseWriter, r *http.Request) {
id := chi.URLParamFromCtx(r.Context(), "id")
// Parse id to int
parsedId, err := strconv.Atoi(id)
if err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{"error":` + strconv.Quote(err.Error()) + `}`))
return
}
joke, contentType, err := core.GetJokeById(r.Context(), d.Bucket, d.Redis, d.Memory, parsedId)
if err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"error":` + strconv.Quote(err.Error()) + `}`))
return
}
w.Header().Set("Content-Type", contentType)
w.WriteHeader(http.StatusOK)
w.Write(joke)
}