list reviews
This commit is contained in:
parent
eaa920abf5
commit
c7e991b3ec
|
@ -29,6 +29,8 @@ func (reviewAPI *ReviewAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
switch {
|
||||
case r.Method == http.MethodGet && subPath != "":
|
||||
reviewAPI.Get(w, r, subPath)
|
||||
case r.Method == http.MethodGet && subPath == "":
|
||||
reviewAPI.List(w, r)
|
||||
case r.Method == http.MethodGet && subPath == "unrated" && subSubPath == "":
|
||||
reviewAPI.ListUnrated(w, r)
|
||||
case r.Method == http.MethodGet && subPath == "unrated" && subSubPath == "next":
|
||||
|
@ -55,6 +57,21 @@ func (reviewAPI *ReviewAPI) Get(w http.ResponseWriter, r *http.Request, id strin
|
|||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
func (reviewAPI *ReviewAPI) ListUnrated(w http.ResponseWriter, r *http.Request) {
|
||||
logger := reviewAPI.logger.With("method", "listUnrated")
|
||||
|
||||
|
|
|
@ -124,6 +124,30 @@ func (rr *ReviewRepository) FindUnrated() ([]Review, error) {
|
|||
return reviews, nil
|
||||
}
|
||||
|
||||
func (rr *ReviewRepository) FindAll() ([]Review, error) {
|
||||
rows, err := rr.db.Query(`SELECT id, movie_id, source, url, review, quality, mentions FROM review`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
reviews := make([]Review, 0)
|
||||
var mentions string
|
||||
for rows.Next() {
|
||||
r := Review{}
|
||||
if err := rows.Scan(&r.ID, &r.MovieID, &r.Source, &r.URL, &r.Review, &r.Quality, &mentions); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.Mentions = make([]string, 0)
|
||||
if mentions != "" {
|
||||
r.Mentions = strings.Split(mentions, MentionsSeparator)
|
||||
}
|
||||
reviews = append(reviews, r)
|
||||
}
|
||||
rows.Close()
|
||||
|
||||
return reviews, nil
|
||||
}
|
||||
|
||||
func (rr *ReviewRepository) DeleteByMovieID(id string) error {
|
||||
if _, err := rr.db.Exec(`DELETE FROM review WHERE movie_id=?`, id); err != nil {
|
||||
return err
|
||||
|
|
Loading…
Reference in New Issue