Add repousers add migrations, add finbyid film repo

main
Shuhrat Tultaganov 2023-01-07 14:19:53 +03:00
parent 5b1b385531
commit 4165956d45
11 changed files with 191 additions and 37 deletions

View File

@ -11,6 +11,7 @@ type Bd struct {
config *ConfigBD config *ConfigBD
db *pgx.Conn db *pgx.Conn
filmsrepo *Filmsrepo filmsrepo *Filmsrepo
userrepo *Userrepo
} }
func New(config *ConfigBD) *Bd { func New(config *ConfigBD) *Bd {
@ -46,3 +47,13 @@ func (b *Bd) Films() *Filmsrepo {
return b.filmsrepo return b.filmsrepo
} }
func (b *Bd) User() *Userrepo {
if b.userrepo != nil {
return b.userrepo
}
b.userrepo = &Userrepo{
db: *b,
}
return b.userrepo
}

View File

@ -2,6 +2,7 @@ package bd
import ( import (
"context" "context"
"fmt"
"git.ukamnya.ru/stulyaganov/RestApi/internal/bd/model" "git.ukamnya.ru/stulyaganov/RestApi/internal/bd/model"
) )
@ -26,7 +27,7 @@ func FindDyId(id int) (*model.Films, error) {
func (f *Filmsrepo) FindByAll() (*[]model.Films, error) { func (f *Filmsrepo) FindByAll() (*[]model.Films, error) {
var buffs []model.Films var buffs []model.Films
rows, err := f.db.db.Query(context.Background(), "SELECT * FROM films") rows, err := f.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 FROM films")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -37,7 +38,7 @@ func (f *Filmsrepo) FindByAll() (*[]model.Films, error) {
&buff.Id, &buff.Ru_title, &buff.Orig_title, &buff.Imdb_id, &buff.Kinopoisk_id, &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.PosterUrl, &buff.PosterUrlPreview, &buff.Countries, &buff.Genres, &buff.Year,
&buff.Description, &buff.RatingKinopoisk, &buff.RatingImdb, &buff.Iframe_src, &buff.RatingImdbVoteCount, &buff.Description, &buff.RatingKinopoisk, &buff.RatingImdb, &buff.Iframe_src, &buff.RatingImdbVoteCount,
&buff.RatingKinopoiskVoteCount, &buff.Media) &buff.RatingKinopoiskVoteCount)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -49,3 +50,26 @@ func (f *Filmsrepo) FindByAll() (*[]model.Films, error) {
} }
return &buffs, nil return &buffs, nil
} }
func (f *Filmsrepo) FindById(id string) (*model.Films, error) {
query := fmt.Sprintf("SELECT * FROM films WHERE Id = %s;", id)
var buff model.Films
rows, err := f.db.db.Query(context.Background(), query)
if err != nil {
return nil, err
}
for rows.Next() {
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 rows.Err() != nil {
return nil, err
}
return &buff, nil
}

View File

@ -8,8 +8,8 @@ type Films struct {
Kinopoisk_id string Kinopoisk_id string
PosterUrl string PosterUrl string
PosterUrlPreview string PosterUrlPreview string
Countries string Countries interface{}
Genres string Genres interface{}
Year int Year int
Description string Description string
RatingKinopoisk int RatingKinopoisk int
@ -17,5 +17,5 @@ type Films struct {
Iframe_src string Iframe_src string
RatingImdbVoteCount int RatingImdbVoteCount int
RatingKinopoiskVoteCount int RatingKinopoiskVoteCount int
Media string Media interface{}
} }

View File

@ -0,0 +1,11 @@
package model
type User struct {
Id int
Login string
Email string
Password string
Avatar_Url string
Token string
PermisionLVL int
}

View File

@ -0,0 +1,37 @@
package bd
import (
"context"
"fmt"
"git.ukamnya.ru/stulyaganov/RestApi/internal/bd/model"
)
type Userrepo struct {
db Bd
}
func (u *Userrepo) Create(user *model.User) (*model.User, error) {
err := u.db.db.QueryRow(context.Background(),
"INSERT INTO users (id, login, email, password, avatar_url, token, permissionLVL) VALUES($1, $2, $3, $4, $5, $6, $7)").
Scan(user.Id, user.Login, user.Email, user.Password, user.Avatar_Url, user.Token, user.PermisionLVL)
if err != nil {
return nil, err
}
return user, nil
}
func (u *Userrepo) FindById(id string) (*model.User, error) {
var user model.User
query := fmt.Sprintf("SELECT * FROM users WHERE Id = %s;", id)
err := u.db.db.QueryRow(context.Background(), query).
Scan(user.Id, user.Login, user.Email, user.Password, user.Avatar_Url, user.Token, user.PermisionLVL)
if err != nil {
return nil, err
}
return &user, nil
}
func (u *Userrepo) FindByAll() (*model.User, error) {
return nil, nil
}

View File

@ -0,0 +1,56 @@
package restserver
import (
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/gorilla/mux"
)
func (r *RestServer) configureRouterFilms() {
r.router.HandleFunc("/api/hello", r.HandleHello()).Methods("GET")
r.router.HandleFunc("/api/films", r.HendleFindAll()).Methods("GET")
r.router.HandleFunc("/api/films/{id:[0-9]+}", r.HendleFindID()).Methods("GET")
}
func (r *RestServer) HandleHello() http.HandlerFunc {
return func(w http.ResponseWriter, res *http.Request) {
id := res.URL.Query().Get("id")
fmt.Println(id)
io.WriteString(w, id)
}
}
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, res *http.Request) {
io.WriteString(w, string(jsonData))
}
}
func (r *RestServer) HendleFindID() http.HandlerFunc {
return func(w http.ResponseWriter, res *http.Request) {
id := mux.Vars(res)["id"]
film, err := r.db.Films().FindById(id)
if err != nil {
r.logger.Errorln(err)
}
jsonData, err := json.MarshalIndent(film, "", " ")
if err != nil {
r.logger.Errorln(err)
}
io.WriteString(w, string(jsonData))
}
}

View File

@ -1,8 +1,6 @@
package restserver package restserver
import ( import (
"encoding/json"
"io"
"net/http" "net/http"
"git.ukamnya.ru/stulyaganov/RestApi/internal/bd" "git.ukamnya.ru/stulyaganov/RestApi/internal/bd"
@ -33,7 +31,8 @@ func (r *RestServer) Start() error {
return err return err
} }
r.configureRouter() r.configureRouterFilms()
r.configureRouterUser()
r.logger.Info("Starting Server") r.logger.Info("Starting Server")
r.logger.Info("Listen server on ", r.config.BindPort, " Port") r.logger.Info("Listen server on ", r.config.BindPort, " Port")
@ -49,11 +48,6 @@ func (r *RestServer) configureLogger() error {
return nil return nil
} }
func (r *RestServer) configureRouter() {
r.router.HandleFunc("/api/hello", r.HandleHello())
r.router.HandleFunc("/api/films", r.HendleFindAll())
}
func (r *RestServer) configurebd() error { func (r *RestServer) configurebd() error {
dataBase := bd.New(r.config.DB) dataBase := bd.New(r.config.DB)
if err := dataBase.Open(); err != nil { if err := dataBase.Open(); err != nil {
@ -64,26 +58,3 @@ func (r *RestServer) configurebd() error {
return nil return nil
} }
func (r *RestServer) HandleHello() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
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))
}
}

View File

@ -0,0 +1,27 @@
package restserver
import (
"fmt"
"net/http"
)
func (r *RestServer) configureRouterUser() {
r.router.HandleFunc("/api/register", r.HandleFuncRegUser()).Methods("POST")
}
func (r *RestServer) HandleFuncRegUser() http.HandlerFunc {
return func(w http.ResponseWriter, res *http.Request) {
err := res.ParseForm()
if err != nil {
r.logger.Error(err)
}
user := res.Form.Get("user")
password := res.Form.Get("password")
mail := res.Form.Get("email")
borndata := res.Form.Get("born")
fmt.Println(user, password, mail, borndata)
}
}

View File

@ -0,0 +1 @@
DROP TABLE users;

View File

@ -0,0 +1,16 @@
CREATE TABLE users (
id INTEGER NOT NULL,
login VARCHAR(25) not NULL,
email VARCHAR(255) NOT NULL,
password VARCHAR() NOT NULL,
avatar_url VARCHAR(),
token VARCHAR() NOT NULL,
permisionLVL INTEGER NOT NULL
PRIMARY KEY(id)
PRIMARY KEY(email)
PRIMARY KEY(login)
PRIMARY KEY(token)
);