Add FindAll from database

main
Shuhrat Tultaganov 2023-01-05 17:21:03 +03:00
parent f5a652c13b
commit 5b1b385531
3 changed files with 45 additions and 1 deletions

View File

@ -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
}

View File

@ -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))
}
}

BIN
rest 100755

Binary file not shown.