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 {
|
||||
case r.Method == http.MethodGet && subPath == "unrated":
|
||||
reviewAPI.ListUnrated(w, r)
|
||||
case r.Method == http.MethodPut && subPath != "":
|
||||
reviewAPI.Store(w, r, subPath)
|
||||
default:
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
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 (
|
||||
ReviewSourceIMDB = "imdb"
|
||||
|
||||
MentionsSeparator = "|"
|
||||
)
|
||||
|
||||
type ReviewSource string
|
||||
|
@ -30,7 +32,7 @@ func NewReviewRepository(db *SQLite) *ReviewRepository {
|
|||
|
||||
func (rr *ReviewRepository) Store(r Review) error {
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
return Review{}, err
|
||||
}
|
||||
r.Mentions = strings.Split(mentions, ",")
|
||||
r.Mentions = strings.Split(mentions, MentionsSeparator)
|
||||
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
r.Mentions = strings.Split(mentions, ",")
|
||||
r.Mentions = strings.Split(mentions, MentionsSeparator)
|
||||
reviews = append(reviews, r)
|
||||
}
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
r.Mentions = strings.Split(mentions, ",")
|
||||
r.Mentions = strings.Split(mentions, MentionsSeparator)
|
||||
reviews = append(reviews, r)
|
||||
}
|
||||
rows.Close()
|
||||
|
|
Loading…
Reference in New Issue