diff --git a/internal/bd/filmsrepo.go b/internal/bd/filmsrepo.go index 49151e4..38cdac6 100644 --- a/internal/bd/filmsrepo.go +++ b/internal/bd/filmsrepo.go @@ -23,3 +23,29 @@ func (f *Filmsrepo) Create(m *model.Films) (*model.Films, error) { func FindDyId(id int) (*model.Films, error) { return nil, nil } + +func (f *Filmsrepo) FindByAll() (*[]model.Films, error) { + var buffs []model.Films + rows, err := f.db.db.Query(context.Background(), "SELECT * FROM films") + if err != nil { + return nil, err + } + + for rows.Next() { + var buff model.Films + err = rows.Scan( + &buff.Id, &buff.Ru_title, &buff.Orig_title, &buff.Imdb_id, &buff.Kinopoisk_id, + &buff.PosterUrl, &buff.PosterUrlPreview, &buff.Countries, &buff.Genres, &buff.Year, + &buff.Description, &buff.RatingKinopoisk, &buff.RatingImdb, &buff.Iframe_src, &buff.RatingImdbVoteCount, + &buff.RatingKinopoiskVoteCount, &buff.Media) + if err != nil { + return nil, err + } + buffs = append(buffs, buff) + } + err = rows.Err() + if err != nil { + return nil, err + } + return &buffs, nil +} diff --git a/internal/restserver/restserver.go b/internal/restserver/restserver.go index 87d3b43..66ac155 100644 --- a/internal/restserver/restserver.go +++ b/internal/restserver/restserver.go @@ -1,6 +1,7 @@ package restserver import ( + "encoding/json" "io" "net/http" @@ -49,7 +50,8 @@ func (r *RestServer) configureLogger() error { } func (r *RestServer) configureRouter() { - r.router.HandleFunc("/api/films", r.HandleHello()) + r.router.HandleFunc("/api/hello", r.HandleHello()) + r.router.HandleFunc("/api/films", r.HendleFindAll()) } func (r *RestServer) configurebd() error { @@ -69,3 +71,19 @@ func (r *RestServer) HandleHello() http.HandlerFunc { io.WriteString(w, "TEsts Hello") } } + +func (r *RestServer) HendleFindAll() http.HandlerFunc { + film, err := r.db.Films().FindByAll() + if err != nil { + r.logger.Errorln(err) + } + + jsonData, err := json.Marshal(film) + if err != nil { + r.logger.Errorln(err) + } + + return func(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, string(jsonData)) + } +} diff --git a/rest b/rest new file mode 100755 index 0000000..63c1433 Binary files /dev/null and b/rest differ