From 4165956d45e2fc3dc94f7b6a3c6db8ecb25d9cd9 Mon Sep 17 00:00:00 2001 From: Shuhrat Tultaganov Date: Sat, 7 Jan 2023 14:19:53 +0300 Subject: [PATCH] Add repousers add migrations, add finbyid film repo --- internal/bd/bd.go | 11 ++++ internal/bd/filmsrepo.go | 28 +++++++++- internal/bd/model/films.go | 6 +-- internal/bd/model/user.go | 11 ++++ internal/bd/userrepo.go | 37 +++++++++++++ internal/restserver/filmhandle.go | 56 ++++++++++++++++++++ internal/restserver/restserver.go | 33 +----------- internal/restserver/userHendle.go | 27 ++++++++++ migrates/20221213130458_films.up.sql | 2 +- migrates/users/20230106180150_users.down.sql | 1 + migrates/users/20230106180150_users.up.sql | 16 ++++++ 11 files changed, 191 insertions(+), 37 deletions(-) create mode 100644 internal/bd/model/user.go create mode 100644 internal/bd/userrepo.go create mode 100644 internal/restserver/filmhandle.go create mode 100644 internal/restserver/userHendle.go create mode 100644 migrates/users/20230106180150_users.down.sql create mode 100644 migrates/users/20230106180150_users.up.sql diff --git a/internal/bd/bd.go b/internal/bd/bd.go index 34c0ddb..d006dc0 100644 --- a/internal/bd/bd.go +++ b/internal/bd/bd.go @@ -11,6 +11,7 @@ type Bd struct { config *ConfigBD db *pgx.Conn filmsrepo *Filmsrepo + userrepo *Userrepo } func New(config *ConfigBD) *Bd { @@ -46,3 +47,13 @@ func (b *Bd) Films() *Filmsrepo { return b.filmsrepo } + +func (b *Bd) User() *Userrepo { + if b.userrepo != nil { + return b.userrepo + } + b.userrepo = &Userrepo{ + db: *b, + } + return b.userrepo +} diff --git a/internal/bd/filmsrepo.go b/internal/bd/filmsrepo.go index 38cdac6..787eeee 100644 --- a/internal/bd/filmsrepo.go +++ b/internal/bd/filmsrepo.go @@ -2,6 +2,7 @@ package bd import ( "context" + "fmt" "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) { 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 { 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.PosterUrl, &buff.PosterUrlPreview, &buff.Countries, &buff.Genres, &buff.Year, &buff.Description, &buff.RatingKinopoisk, &buff.RatingImdb, &buff.Iframe_src, &buff.RatingImdbVoteCount, - &buff.RatingKinopoiskVoteCount, &buff.Media) + &buff.RatingKinopoiskVoteCount) if err != nil { return nil, err } @@ -49,3 +50,26 @@ func (f *Filmsrepo) FindByAll() (*[]model.Films, error) { } 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 +} diff --git a/internal/bd/model/films.go b/internal/bd/model/films.go index b492e7c..a2ccd99 100644 --- a/internal/bd/model/films.go +++ b/internal/bd/model/films.go @@ -8,8 +8,8 @@ type Films struct { Kinopoisk_id string PosterUrl string PosterUrlPreview string - Countries string - Genres string + Countries interface{} + Genres interface{} Year int Description string RatingKinopoisk int @@ -17,5 +17,5 @@ type Films struct { Iframe_src string RatingImdbVoteCount int RatingKinopoiskVoteCount int - Media string + Media interface{} } diff --git a/internal/bd/model/user.go b/internal/bd/model/user.go new file mode 100644 index 0000000..03f90c4 --- /dev/null +++ b/internal/bd/model/user.go @@ -0,0 +1,11 @@ +package model + +type User struct { + Id int + Login string + Email string + Password string + Avatar_Url string + Token string + PermisionLVL int +} diff --git a/internal/bd/userrepo.go b/internal/bd/userrepo.go new file mode 100644 index 0000000..0083aa8 --- /dev/null +++ b/internal/bd/userrepo.go @@ -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 +} diff --git a/internal/restserver/filmhandle.go b/internal/restserver/filmhandle.go new file mode 100644 index 0000000..33f48f8 --- /dev/null +++ b/internal/restserver/filmhandle.go @@ -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)) + } +} diff --git a/internal/restserver/restserver.go b/internal/restserver/restserver.go index 66ac155..c48ccb1 100644 --- a/internal/restserver/restserver.go +++ b/internal/restserver/restserver.go @@ -1,8 +1,6 @@ package restserver import ( - "encoding/json" - "io" "net/http" "git.ukamnya.ru/stulyaganov/RestApi/internal/bd" @@ -33,7 +31,8 @@ func (r *RestServer) Start() error { return err } - r.configureRouter() + r.configureRouterFilms() + r.configureRouterUser() r.logger.Info("Starting Server") r.logger.Info("Listen server on ", r.config.BindPort, " Port") @@ -49,11 +48,6 @@ func (r *RestServer) configureLogger() error { 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 { dataBase := bd.New(r.config.DB) if err := dataBase.Open(); err != nil { @@ -64,26 +58,3 @@ func (r *RestServer) configurebd() error { 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)) - } -} diff --git a/internal/restserver/userHendle.go b/internal/restserver/userHendle.go new file mode 100644 index 0000000..b07fe15 --- /dev/null +++ b/internal/restserver/userHendle.go @@ -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) + } + +} diff --git a/migrates/20221213130458_films.up.sql b/migrates/20221213130458_films.up.sql index 4e1058a..fc950a4 100644 --- a/migrates/20221213130458_films.up.sql +++ b/migrates/20221213130458_films.up.sql @@ -18,4 +18,4 @@ CREATE TABLE films ( ratingKinopoiskVoteCount INTEGER, media JSON, PRIMARY KEY(id) -); \ No newline at end of file +); diff --git a/migrates/users/20230106180150_users.down.sql b/migrates/users/20230106180150_users.down.sql new file mode 100644 index 0000000..441087a --- /dev/null +++ b/migrates/users/20230106180150_users.down.sql @@ -0,0 +1 @@ +DROP TABLE users; \ No newline at end of file diff --git a/migrates/users/20230106180150_users.up.sql b/migrates/users/20230106180150_users.up.sql new file mode 100644 index 0000000..ecc4fd0 --- /dev/null +++ b/migrates/users/20230106180150_users.up.sql @@ -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) +); \ No newline at end of file