Add repousers add migrations, add finbyid film repo
parent
5b1b385531
commit
4165956d45
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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{}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
package model
|
||||
|
||||
type User struct {
|
||||
Id int
|
||||
Login string
|
||||
Email string
|
||||
Password string
|
||||
Avatar_Url string
|
||||
Token string
|
||||
PermisionLVL int
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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))
|
||||
}
|
||||
}
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
DROP TABLE users;
|
|
@ -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)
|
||||
);
|
Loading…
Reference in New Issue