Add siries Router
parent
931153b0da
commit
313bcabbae
|
@ -8,10 +8,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Bd struct {
|
type Bd struct {
|
||||||
config *ConfigBD
|
config *ConfigBD
|
||||||
db *pgx.Conn
|
db *pgx.Conn
|
||||||
filmsrepo *Filmsrepo
|
filmsrepo *Filmsrepo
|
||||||
userrepo *Userrepo
|
siriesrepo *Siriesrepo
|
||||||
|
userrepo *Userrepo
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(config *ConfigBD) *Bd {
|
func New(config *ConfigBD) *Bd {
|
||||||
|
@ -47,6 +48,16 @@ func (b *Bd) Films() *Filmsrepo {
|
||||||
|
|
||||||
return b.filmsrepo
|
return b.filmsrepo
|
||||||
}
|
}
|
||||||
|
func (b *Bd) Siries() *Siriesrepo {
|
||||||
|
if b.siriesrepo != nil {
|
||||||
|
return b.siriesrepo
|
||||||
|
}
|
||||||
|
b.siriesrepo = &Siriesrepo{
|
||||||
|
db: *b,
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.siriesrepo
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Bd) User() *Userrepo {
|
func (b *Bd) User() *Userrepo {
|
||||||
if b.userrepo != nil {
|
if b.userrepo != nil {
|
||||||
|
|
|
@ -92,7 +92,7 @@ func (f *Filmsrepo) FindById(id string) (*model.Films, error) {
|
||||||
|
|
||||||
func (f *Filmsrepo) FindByName(name string) (*[]model.Films, error) {
|
func (f *Filmsrepo) FindByName(name string) (*[]model.Films, error) {
|
||||||
var films []model.Films
|
var films []model.Films
|
||||||
rows, err := f.db.db.Query(context.Background(), "SELECT * FROM films WHERE ru_title LIKE $1;", "%"+name+"%")
|
rows, err := f.db.db.Query(context.Background(), "SELECT * FROM films WHERE ru_title LIKE %$1%;", name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
package model
|
||||||
|
|
||||||
|
type Siries struct {
|
||||||
|
Id int
|
||||||
|
Ru_title *string
|
||||||
|
Orig_title *string
|
||||||
|
Imdb_id *string
|
||||||
|
Kinopoisk_id *int
|
||||||
|
PosterUrl *string
|
||||||
|
PosterUrlPreview *string
|
||||||
|
Countries *any
|
||||||
|
Genres *any
|
||||||
|
Year *int
|
||||||
|
Description *string
|
||||||
|
RatingKinopoisk *int
|
||||||
|
RatingImdb *int
|
||||||
|
Iframe_src *string
|
||||||
|
RatingImdbVoteCount *int
|
||||||
|
RatingKinopoiskVoteCount *int
|
||||||
|
Created *string
|
||||||
|
Translations *any
|
||||||
|
Episodes *any
|
||||||
|
}
|
|
@ -0,0 +1,146 @@
|
||||||
|
package bd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"git.ukamnya.ru/stulyaganov/RestApi/internal/bd/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Siriesrepo struct {
|
||||||
|
db Bd
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Siriesrepo) FindById(id string) (*model.Siries, error) {
|
||||||
|
var buff model.Siries
|
||||||
|
rows, err := s.db.db.Query(context.Background(), "SELECT * FROM siries WHERE Id = $1;", id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
for rows.Next() {
|
||||||
|
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.Created, &buff.Translations, &buff.Episodes)
|
||||||
|
}
|
||||||
|
return &buff, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Siriesrepo) Pagination(page string) (*[]model.Siries, error) {
|
||||||
|
var buffs []model.Siries
|
||||||
|
pages, _ := strconv.Atoi(page)
|
||||||
|
limit := 32
|
||||||
|
offcet := limit * (pages - 1)
|
||||||
|
rows, err := s.db.db.Query(context.Background(), "SELECT Id, Ru_title, PosterUrlPreview, Countries, Genres, Year FROM siries ORDER BY year desc, created DESC LIMIT 32 OFFSET $1", offcet)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
for rows.Next() {
|
||||||
|
var buff model.Siries
|
||||||
|
err = rows.Scan(&buff.Id, &buff.Ru_title, &buff.PosterUrlPreview, &buff.Countries, &buff.Genres, &buff.Year)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
buffs = append(buffs, buff)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return &buffs, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Siriesrepo) LastItem() (*[]model.Siries, error) {
|
||||||
|
var buffs []model.Siries
|
||||||
|
rows, err := s.db.db.Query(context.Background(), "SELECT id, ru_title, posterurlpreview, countries, genres, year FROM siries ORDER BY year desc, created DESC LIMIT 8")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
var buff model.Siries
|
||||||
|
err = rows.Scan(&buff.Id, &buff.Ru_title, &buff.PosterUrlPreview, &buff.Countries, &buff.Genres, &buff.Year)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
buffs = append(buffs, buff)
|
||||||
|
}
|
||||||
|
return &buffs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Siriesrepo) SortByGanres(genre string) (*[]model.Siries, error) {
|
||||||
|
var buffs []model.Siries
|
||||||
|
rows, err := s.db.db.Query(context.Background(), "SELECT id, ru_title, posterurl, Countries, Genres, Year FROM siries WHERE genres::TEXT LIKE $1;", "%"+genre+"%")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
for rows.Next() {
|
||||||
|
var buff model.Siries
|
||||||
|
err = rows.Scan(&buff.Id, &buff.Ru_title, &buff.PosterUrl, &buff.Countries, &buff.Genres, buff.Year)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
buffs = append(buffs, buff)
|
||||||
|
}
|
||||||
|
return &buffs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Siriesrepo) GetCountFilms() (*int, error) {
|
||||||
|
rows, err := s.db.db.Query(context.Background(), "SELECT count(*) FROM siries")
|
||||||
|
var count int
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
for rows.Next() {
|
||||||
|
rows.Scan(&count)
|
||||||
|
}
|
||||||
|
return &count, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Siriesrepo) FindByAll() (*[]model.Siries, error) {
|
||||||
|
var buffs []model.Siries
|
||||||
|
rows, err := s.db.db.Query(context.Background(), "SELECT Id, Ru_title, Orig_title, Imdb_id, Kinopoisk_id, PosterUrl, PosterUrlPreview, Countries, Genres, Year, Description, RatingKinopoisk, RatingImdb, Iframe_src, RatingImdbVoteCount, RatingKinopoiskVoteCount, Created FROM siries")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
for rows.Next() {
|
||||||
|
var buff model.Siries
|
||||||
|
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.Created)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
buffs = append(buffs, buff)
|
||||||
|
}
|
||||||
|
return &buffs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Siriesrepo) FindByName(name string) (*[]model.Siries, error) {
|
||||||
|
var buffs []model.Siries
|
||||||
|
rows, err := s.db.db.Query(context.Background(), "SELECT * FROM siries WHERE ru_title LIKE $1", "%"+name+"%")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
for rows.Next() {
|
||||||
|
var buff model.Siries
|
||||||
|
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.Created, &buff.Translations, &buff.Episodes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
buffs = append(buffs, buff)
|
||||||
|
|
||||||
|
}
|
||||||
|
return &buffs, nil
|
||||||
|
}
|
|
@ -22,7 +22,6 @@ func (r *RestServer) configureRouterFilms() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RestServer) HandleHello() http.HandlerFunc {
|
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)
|
||||||
|
|
|
@ -32,6 +32,7 @@ func (r *RestServer) Start() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
r.configureRouterFilms()
|
r.configureRouterFilms()
|
||||||
|
r.configureRouterSiries()
|
||||||
r.configureRouterUser()
|
r.configureRouterUser()
|
||||||
|
|
||||||
r.logger.Info("Starting Server")
|
r.logger.Info("Starting Server")
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
package resultstruct
|
||||||
|
|
||||||
|
import "git.ukamnya.ru/stulyaganov/RestApi/internal/bd/model"
|
||||||
|
|
||||||
|
type Siries struct {
|
||||||
|
Result bool
|
||||||
|
Data []model.Siries
|
||||||
|
Page int
|
||||||
|
LastPage int
|
||||||
|
}
|
|
@ -0,0 +1,143 @@
|
||||||
|
package restserver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
resultstruct "git.ukamnya.ru/stulyaganov/RestApi/internal/restserver/resultStruct"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (r *RestServer) configureRouterSiries() {
|
||||||
|
r.router.HandleFunc("/api/siries", r.HendleFindAllSiries()).Methods("GET")
|
||||||
|
r.router.HandleFunc("/api/siries/{id:[0-9]+}", r.HendleFindIDSiries()).Methods("GET")
|
||||||
|
r.router.HandleFunc("/api/siries/{name}", r.HeandleSiriesFindName()).Methods("GET")
|
||||||
|
r.router.HandleFunc("/api/siries/genres/{name}", r.HeandleSiriesSortGenres()).Methods("GET")
|
||||||
|
r.router.HandleFunc("/api/siries/page/{page:[0-9]+}", r.HendlePaginationSiries()).Methods("GET")
|
||||||
|
r.router.HandleFunc("/api/siries/last/", r.HeadleGetLastItemSiries()).Methods("GET")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RestServer) HandleHelloSiries() http.HandlerFunc {
|
||||||
|
|
||||||
|
return func(w http.ResponseWriter, res *http.Request) {
|
||||||
|
id := res.URL.Query().Get("id")
|
||||||
|
fmt.Println(id)
|
||||||
|
cout, err := r.db.Siries().GetCountFilms()
|
||||||
|
if err != nil {
|
||||||
|
r.logger.Errorln(err)
|
||||||
|
}
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
||||||
|
io.WriteString(w, strconv.Itoa(*cout))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RestServer) HendleFindAllSiries() http.HandlerFunc {
|
||||||
|
var siriesData resultstruct.Siries
|
||||||
|
data, err := r.db.Siries().FindByAll()
|
||||||
|
if err != nil {
|
||||||
|
r.logger.Errorln(err)
|
||||||
|
}
|
||||||
|
cout, err := r.db.Siries().GetCountFilms()
|
||||||
|
if err != nil {
|
||||||
|
r.logger.Errorln(err)
|
||||||
|
siriesData.Result = false
|
||||||
|
} else {
|
||||||
|
siriesData.Result = true
|
||||||
|
siriesData.Data = *data
|
||||||
|
siriesData.LastPage = *cout
|
||||||
|
}
|
||||||
|
jsonData, err := json.Marshal(siriesData)
|
||||||
|
if err != nil {
|
||||||
|
r.logger.Errorln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return func(w http.ResponseWriter, res *http.Request) {
|
||||||
|
io.WriteString(w, string(jsonData))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RestServer) HeandleSiriesFindName() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, res *http.Request) {
|
||||||
|
name := mux.Vars(res)["name"]
|
||||||
|
siries, err := r.db.Siries().FindByName(name)
|
||||||
|
if err != nil {
|
||||||
|
r.logger.Errorln(err)
|
||||||
|
}
|
||||||
|
jsonData, err := json.Marshal(siries)
|
||||||
|
if err != nil {
|
||||||
|
r.logger.Errorln(err)
|
||||||
|
}
|
||||||
|
io.WriteString(w, string(jsonData))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RestServer) HeandleSiriesSortGenres() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, res *http.Request) {
|
||||||
|
name := mux.Vars(res)["name"]
|
||||||
|
siries, err := r.db.Siries().SortByGanres(name)
|
||||||
|
if err != nil {
|
||||||
|
r.logger.Errorln(err)
|
||||||
|
}
|
||||||
|
jsonData, err := json.Marshal(siries)
|
||||||
|
if err != nil {
|
||||||
|
r.logger.Errorln(err)
|
||||||
|
}
|
||||||
|
io.WriteString(w, string(jsonData))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RestServer) HendleFindIDSiries() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, res *http.Request) {
|
||||||
|
id := mux.Vars(res)["id"]
|
||||||
|
fmt.Println(mux.Vars(res))
|
||||||
|
siries, err := r.db.Siries().FindById(id)
|
||||||
|
if err != nil {
|
||||||
|
r.logger.Errorln(err)
|
||||||
|
}
|
||||||
|
jsonData, err := json.MarshalIndent(siries, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
r.logger.Errorln(err)
|
||||||
|
}
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
||||||
|
io.WriteString(w, string(jsonData))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RestServer) HendlePaginationSiries() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, res *http.Request) {
|
||||||
|
id := mux.Vars(res)["page"]
|
||||||
|
siries, err := r.db.Siries().Pagination(id)
|
||||||
|
if err != nil {
|
||||||
|
r.logger.Errorln(err)
|
||||||
|
}
|
||||||
|
jsondata, err := json.Marshal(siries)
|
||||||
|
if err != nil {
|
||||||
|
r.logger.Errorln(err)
|
||||||
|
}
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
||||||
|
io.WriteString(w, string(jsondata))
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RestServer) HeadleGetLastItemSiries() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, res *http.Request) {
|
||||||
|
siries, err := r.db.Siries().LastItem()
|
||||||
|
if err != nil {
|
||||||
|
r.logger.Errorln(err)
|
||||||
|
}
|
||||||
|
jsondata, err := json.Marshal(siries)
|
||||||
|
if err != nil {
|
||||||
|
r.logger.Errorln(err)
|
||||||
|
}
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
||||||
|
io.WriteString(w, string(jsondata))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue