Add get count all items from db, and review result struct

main
Shuhrat Tultaganov 2023-01-26 22:07:44 +03:00
parent 88ae5fa188
commit f6bca0590f
3 changed files with 42 additions and 4 deletions

View File

@ -51,6 +51,19 @@ func (f *Filmsrepo) FindByAll() (*[]model.Films, error) {
return &buffs, nil return &buffs, nil
} }
func (f *Filmsrepo) GetCountFilms() (*int, error) {
rows, err := f.db.db.Query(context.Background(), "SELECT count(*) FROM films")
var count int
if err != nil {
return nil, err
}
for rows.Next() {
rows.Scan(&count)
}
return &count, nil
}
func (f *Filmsrepo) FindById(id string) (*model.Films, error) { func (f *Filmsrepo) FindById(id string) (*model.Films, error) {
query := fmt.Sprintf("SELECT * FROM films WHERE Id = %s;", id) query := fmt.Sprintf("SELECT * FROM films WHERE Id = %s;", id)
var buff model.Films var buff model.Films

View File

@ -5,7 +5,9 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"strconv"
resultstruct "git.ukamnya.ru/stulyaganov/RestApi/internal/restserver/resultStruct"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
@ -20,17 +22,30 @@ func (r *RestServer) HandleHello() http.HandlerFunc {
return func(w http.ResponseWriter, res *http.Request) { return func(w http.ResponseWriter, res *http.Request) {
id := res.URL.Query().Get("id") id := res.URL.Query().Get("id")
fmt.Println(id) fmt.Println(id)
io.WriteString(w, id) cout, err := r.db.Films().GetCountFilms()
if err != nil {
r.logger.Errorln(err)
}
io.WriteString(w, strconv.Itoa(*cout))
} }
} }
func (r *RestServer) HendleFindAll() http.HandlerFunc { func (r *RestServer) HendleFindAll() http.HandlerFunc {
film, err := r.db.Films().FindByAll() var filmData resultstruct.Films
data, err := r.db.Films().FindByAll()
if err != nil { if err != nil {
r.logger.Errorln(err) r.logger.Errorln(err)
} }
cout, err := r.db.Films().GetCountFilms()
jsonData, err := json.Marshal(film) if err != nil {
r.logger.Errorln(err)
filmData.Result = false
} else {
filmData.Result = true
filmData.Data = *data
filmData.LastPage = *cout
}
jsonData, err := json.Marshal(filmData)
if err != nil { if err != nil {
r.logger.Errorln(err) r.logger.Errorln(err)
} }

View File

@ -0,0 +1,10 @@
package resultstruct
import "git.ukamnya.ru/stulyaganov/RestApi/internal/bd/model"
type Films struct {
Result bool
Data []model.Films
Page int
LastPage int
}