update review
This commit is contained in:
parent
a24ba03c8b
commit
48234e18cc
|
@ -28,6 +28,8 @@ func (reviewAPI *ReviewAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
switch {
|
switch {
|
||||||
case r.Method == http.MethodGet && subPath == "unrated":
|
case r.Method == http.MethodGet && subPath == "unrated":
|
||||||
reviewAPI.ListUnrated(w, r)
|
reviewAPI.ListUnrated(w, r)
|
||||||
|
case r.Method == http.MethodPut && subPath != "":
|
||||||
|
reviewAPI.Store(w, r, subPath)
|
||||||
default:
|
default:
|
||||||
Error(w, http.StatusNotFound, "unregistered path", fmt.Errorf("method %q with subpath %q was not registered in /review", r.Method, subPath), logger)
|
Error(w, http.StatusNotFound, "unregistered path", fmt.Errorf("method %q with subpath %q was not registered in /review", r.Method, subPath), logger)
|
||||||
}
|
}
|
||||||
|
@ -47,3 +49,25 @@ func (reviewAPI *ReviewAPI) ListUnrated(w http.ResponseWriter, r *http.Request)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,8 @@ import "strings"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ReviewSourceIMDB = "imdb"
|
ReviewSourceIMDB = "imdb"
|
||||||
|
|
||||||
|
MentionsSeparator = "|"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ReviewSource string
|
type ReviewSource string
|
||||||
|
@ -30,7 +32,7 @@ func NewReviewRepository(db *SQLite) *ReviewRepository {
|
||||||
|
|
||||||
func (rr *ReviewRepository) Store(r Review) error {
|
func (rr *ReviewRepository) Store(r Review) error {
|
||||||
if _, err := rr.db.Exec(`REPLACE INTO review (id, movie_id, source, url, review, quality, mentions) VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
if _, err := rr.db.Exec(`REPLACE INTO review (id, movie_id, source, url, review, quality, mentions) VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
||||||
r.ID, r.MovieID, r.Source, r.URL, r.Review, r.Quality, strings.Join(r.Mentions, ",")); err != nil {
|
r.ID, r.MovieID, r.Source, r.URL, r.Review, r.Quality, strings.Join(r.Mentions, MentionsSeparator)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +50,7 @@ func (rr *ReviewRepository) FindOne(id string) (Review, error) {
|
||||||
if err := row.Scan(&r.ID, &r.MovieID, &r.Source, &r.URL, &r.Review, &r.Quality, &mentions); err != nil {
|
if err := row.Scan(&r.ID, &r.MovieID, &r.Source, &r.URL, &r.Review, &r.Quality, &mentions); err != nil {
|
||||||
return Review{}, err
|
return Review{}, err
|
||||||
}
|
}
|
||||||
r.Mentions = strings.Split(mentions, ",")
|
r.Mentions = strings.Split(mentions, MentionsSeparator)
|
||||||
|
|
||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
@ -66,7 +68,7 @@ func (rr *ReviewRepository) FindByMovieID(movieID string) ([]Review, error) {
|
||||||
if err := rows.Scan(&r.ID, &r.MovieID, &r.Source, &r.URL, &r.Review, &r.Quality, &mentions); err != nil {
|
if err := rows.Scan(&r.ID, &r.MovieID, &r.Source, &r.URL, &r.Review, &r.Quality, &mentions); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
r.Mentions = strings.Split(mentions, ",")
|
r.Mentions = strings.Split(mentions, MentionsSeparator)
|
||||||
reviews = append(reviews, r)
|
reviews = append(reviews, r)
|
||||||
}
|
}
|
||||||
rows.Close()
|
rows.Close()
|
||||||
|
@ -87,7 +89,7 @@ func (rr *ReviewRepository) FindUnrated() ([]Review, error) {
|
||||||
if err := rows.Scan(&r.ID, &r.MovieID, &r.Source, &r.URL, &r.Review, &r.Quality, &mentions); err != nil {
|
if err := rows.Scan(&r.ID, &r.MovieID, &r.Source, &r.URL, &r.Review, &r.Quality, &mentions); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
r.Mentions = strings.Split(mentions, ",")
|
r.Mentions = strings.Split(mentions, MentionsSeparator)
|
||||||
reviews = append(reviews, r)
|
reviews = append(reviews, r)
|
||||||
}
|
}
|
||||||
rows.Close()
|
rows.Close()
|
||||||
|
|
Loading…
Reference in New Issue