2023-12-29 19:10:31 +01:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2023-12-30 09:19:53 +01:00
|
|
|
"encoding/json"
|
2023-12-29 19:10:31 +01:00
|
|
|
"fmt"
|
|
|
|
"log/slog"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"ewintr.nl/emdb/cmd/api-service/moviestore"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ReviewAPI struct {
|
|
|
|
repo *moviestore.ReviewRepository
|
|
|
|
logger *slog.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewReviewAPI(repo *moviestore.ReviewRepository, logger *slog.Logger) *ReviewAPI {
|
|
|
|
return &ReviewAPI{
|
|
|
|
repo: repo,
|
|
|
|
logger: logger.With("api", "review"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (reviewAPI *ReviewAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
logger := reviewAPI.logger.With("method", "serveHTTP")
|
|
|
|
|
2024-01-06 14:55:00 +01:00
|
|
|
subPath, subTrail := ShiftPath(r.URL.Path)
|
|
|
|
subSubPath, _ := ShiftPath(subTrail)
|
2023-12-29 19:10:31 +01:00
|
|
|
switch {
|
2024-01-13 18:17:37 +01:00
|
|
|
case r.Method == http.MethodGet && subPath == "":
|
|
|
|
reviewAPI.List(w, r)
|
2024-01-06 14:55:00 +01:00
|
|
|
case r.Method == http.MethodGet && subPath == "unrated" && subSubPath == "":
|
2024-01-06 14:22:22 +01:00
|
|
|
reviewAPI.ListUnrated(w, r)
|
2024-01-06 14:55:00 +01:00
|
|
|
case r.Method == http.MethodGet && subPath == "unrated" && subSubPath == "next":
|
|
|
|
reviewAPI.NextUnrated(w, r)
|
2024-01-20 11:22:58 +01:00
|
|
|
case r.Method == http.MethodGet && subPath == "no-titles" && subSubPath == "":
|
2024-01-20 11:19:15 +01:00
|
|
|
reviewAPI.ListNoTitles(w, r)
|
2024-01-20 11:39:37 +01:00
|
|
|
case r.Method == http.MethodGet && subPath == "no-titles" && subSubPath == "next":
|
2024-01-20 11:36:05 +01:00
|
|
|
reviewAPI.NextNoTitles(w, r)
|
2024-01-13 18:24:08 +01:00
|
|
|
case r.Method == http.MethodGet && subPath != "":
|
|
|
|
reviewAPI.Get(w, r, subPath)
|
2024-01-06 14:40:36 +01:00
|
|
|
case r.Method == http.MethodPut && subPath != "":
|
|
|
|
reviewAPI.Store(w, r, subPath)
|
2023-12-29 19:10:31 +01:00
|
|
|
default:
|
|
|
|
Error(w, http.StatusNotFound, "unregistered path", fmt.Errorf("method %q with subpath %q was not registered in /review", r.Method, subPath), logger)
|
|
|
|
}
|
|
|
|
}
|
2023-12-30 09:19:53 +01:00
|
|
|
|
2024-01-13 13:59:13 +01:00
|
|
|
func (reviewAPI *ReviewAPI) Get(w http.ResponseWriter, r *http.Request, id string) {
|
|
|
|
logger := reviewAPI.logger.With("method", "get")
|
|
|
|
|
|
|
|
review, err := reviewAPI.repo.FindOne(id)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, http.StatusInternalServerError, "could not get review", err, logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewEncoder(w).Encode(review); err != nil {
|
|
|
|
Error(w, http.StatusInternalServerError, "could not encode review", err, logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-13 18:17:37 +01:00
|
|
|
func (reviewAPI *ReviewAPI) List(w http.ResponseWriter, r *http.Request) {
|
|
|
|
logger := reviewAPI.logger.With("method", "list")
|
|
|
|
|
|
|
|
reviews, err := reviewAPI.repo.FindAll()
|
|
|
|
if err != nil {
|
|
|
|
Error(w, http.StatusInternalServerError, "could not get reviews", err, logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewEncoder(w).Encode(reviews); err != nil {
|
|
|
|
Error(w, http.StatusInternalServerError, "could not encode reviews", err, logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-06 14:22:22 +01:00
|
|
|
func (reviewAPI *ReviewAPI) ListUnrated(w http.ResponseWriter, r *http.Request) {
|
|
|
|
logger := reviewAPI.logger.With("method", "listUnrated")
|
2023-12-30 09:19:53 +01:00
|
|
|
|
2024-01-06 14:22:22 +01:00
|
|
|
reviews, err := reviewAPI.repo.FindUnrated()
|
2023-12-30 09:19:53 +01:00
|
|
|
if err != nil {
|
|
|
|
Error(w, http.StatusInternalServerError, "could not get reviews", err, logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewEncoder(w).Encode(reviews); err != nil {
|
|
|
|
Error(w, http.StatusInternalServerError, "could not encode reviews", err, logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2024-01-06 14:40:36 +01:00
|
|
|
|
2024-01-06 14:55:00 +01:00
|
|
|
func (reviewAPI *ReviewAPI) NextUnrated(w http.ResponseWriter, r *http.Request) {
|
|
|
|
logger := reviewAPI.logger.With("method", "nextUnrated")
|
|
|
|
|
|
|
|
review, err := reviewAPI.repo.FindNextUnrated()
|
|
|
|
if err != nil {
|
|
|
|
Error(w, http.StatusInternalServerError, "could not get review", err, logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewEncoder(w).Encode(review); err != nil {
|
|
|
|
Error(w, http.StatusInternalServerError, "could not encode review", err, logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-20 11:19:15 +01:00
|
|
|
func (reviewAPI *ReviewAPI) ListNoTitles(w http.ResponseWriter, r *http.Request) {
|
|
|
|
logger := reviewAPI.logger.With("method", "listNoTitles")
|
|
|
|
|
|
|
|
reviews, err := reviewAPI.repo.FindNoTitles()
|
|
|
|
if err != nil {
|
|
|
|
Error(w, http.StatusInternalServerError, "could not get reviews", err, logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewEncoder(w).Encode(reviews); err != nil {
|
|
|
|
Error(w, http.StatusInternalServerError, "could not encode reviews", err, logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-20 11:22:58 +01:00
|
|
|
func (reviewAPI *ReviewAPI) NextNoTitles(w http.ResponseWriter, r *http.Request) {
|
|
|
|
logger := reviewAPI.logger.With("method", "nextNoTitles")
|
|
|
|
|
|
|
|
review, err := reviewAPI.repo.FindNextNoTitles()
|
|
|
|
if err != nil {
|
|
|
|
Error(w, http.StatusInternalServerError, "could not get review", err, logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewEncoder(w).Encode(review); err != nil {
|
|
|
|
Error(w, http.StatusInternalServerError, "could not encode review", err, logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-06 14:40:36 +01:00
|
|
|
func (reviewAPI *ReviewAPI) Store(w http.ResponseWriter, r *http.Request, id string) {
|
|
|
|
logger := reviewAPI.logger.With("method", "store")
|
|
|
|
|
|
|
|
var review moviestore.Review
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&review); err != nil {
|
|
|
|
Error(w, http.StatusBadRequest, "could not decode review", err, logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if id != review.ID {
|
|
|
|
Error(w, http.StatusBadRequest, "id in path does not match id in body", fmt.Errorf("id in path %q does not match id in body %q", id, review.ID), logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := reviewAPI.repo.Store(review); err != nil {
|
|
|
|
Error(w, http.StatusInternalServerError, "could not store review", err, logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
}
|