next unrated review handler
This commit is contained in:
parent
53184470ea
commit
255afcc373
|
@ -24,10 +24,13 @@ func NewReviewAPI(repo *moviestore.ReviewRepository, logger *slog.Logger) *Revie
|
|||
func (reviewAPI *ReviewAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
logger := reviewAPI.logger.With("method", "serveHTTP")
|
||||
|
||||
subPath, _ := ShiftPath(r.URL.Path)
|
||||
subPath, subTrail := ShiftPath(r.URL.Path)
|
||||
subSubPath, _ := ShiftPath(subTrail)
|
||||
switch {
|
||||
case r.Method == http.MethodGet && subPath == "unrated":
|
||||
case r.Method == http.MethodGet && subPath == "unrated" && subSubPath == "":
|
||||
reviewAPI.ListUnrated(w, r)
|
||||
case r.Method == http.MethodGet && subPath == "unrated" && subSubPath == "next":
|
||||
reviewAPI.NextUnrated(w, r)
|
||||
case r.Method == http.MethodPut && subPath != "":
|
||||
reviewAPI.Store(w, r, subPath)
|
||||
default:
|
||||
|
@ -50,6 +53,21 @@ func (reviewAPI *ReviewAPI) ListUnrated(w http.ResponseWriter, r *http.Request)
|
|||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
func (reviewAPI *ReviewAPI) Store(w http.ResponseWriter, r *http.Request, id string) {
|
||||
logger := reviewAPI.logger.With("method", "store")
|
||||
|
||||
|
|
|
@ -76,6 +76,22 @@ func (rr *ReviewRepository) FindByMovieID(movieID string) ([]Review, error) {
|
|||
return reviews, nil
|
||||
}
|
||||
|
||||
func (rr *ReviewRepository) FindNextUnrated() (Review, error) {
|
||||
row := rr.db.QueryRow(`SELECT id, movie_id, source, url, review, quality, mentions FROM review WHERE quality=0 LIMIT 1`)
|
||||
if row.Err() != nil {
|
||||
return Review{}, row.Err()
|
||||
}
|
||||
|
||||
r := Review{}
|
||||
var mentions string
|
||||
if err := row.Scan(&r.ID, &r.MovieID, &r.Source, &r.URL, &r.Review, &r.Quality, &mentions); err != nil {
|
||||
return Review{}, err
|
||||
}
|
||||
r.Mentions = strings.Split(mentions, MentionsSeparator)
|
||||
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (rr *ReviewRepository) FindUnrated() ([]Review, error) {
|
||||
rows, err := rr.db.Query(`SELECT id, movie_id, source, url, review, quality, mentions FROM review WHERE quality=0`)
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue