From c97d1ef61c3bd6268905978949b6770362009432 Mon Sep 17 00:00:00 2001 From: ddusichka <78527291+ddusichka@users.noreply.github.com> Date: Mon, 9 Sep 2024 00:05:26 -0400 Subject: [PATCH] move db logic --- backend/cmd/server/main.go | 4 +- backend/internal/service/handler/user.go | 42 ++---------- backend/internal/service/server.go | 5 +- backend/internal/storage/storage.go | 16 +++++ .../internal/storage/supabase/schema/user.go | 65 +++++++++++++++++++ .../supabase/storage.go} | 13 +++- 6 files changed, 103 insertions(+), 42 deletions(-) create mode 100644 backend/internal/storage/storage.go create mode 100644 backend/internal/storage/supabase/schema/user.go rename backend/internal/{database/database.go => storage/supabase/storage.go} (79%) diff --git a/backend/cmd/server/main.go b/backend/cmd/server/main.go index 27bb8900..baacd2da 100644 --- a/backend/cmd/server/main.go +++ b/backend/cmd/server/main.go @@ -5,8 +5,8 @@ import ( "log" "platnm/internal/config" - "platnm/internal/database" "platnm/internal/service" + "platnm/internal/storage/supabase" _ "github.com/lib/pq" "github.com/sethvargo/go-envconfig" @@ -20,7 +20,7 @@ func main() { } // Connect to database - conn := database.ConnectDatabase(config.DbHost, config.DbUser, config.DbPassword, config.DbName, config.DbPort) + conn := supabase.ConnectDatabase(config.DbHost, config.DbUser, config.DbPassword, config.DbName, config.DbPort) app := service.InitApp(service.Params{Conn: conn}) defer conn.Close() diff --git a/backend/internal/service/handler/user.go b/backend/internal/service/handler/user.go index 0f7d4b22..e3e06639 100644 --- a/backend/internal/service/handler/user.go +++ b/backend/internal/service/handler/user.go @@ -1,52 +1,24 @@ package handler import ( - "context" - "platnm/internal/models" + "platnm/internal/storage" "github.com/gofiber/fiber/v2" - "github.com/jackc/pgx/v5/pgxpool" ) type UserHandler struct { - conn *pgxpool.Pool + userRepository storage.UserRepository } -func NewUserHandler(conn *pgxpool.Pool) *UserHandler { +func NewUserHandler(userRepository storage.UserRepository) *UserHandler { return &UserHandler{ - conn, + userRepository, } } func (h *UserHandler) GetUsers(c *fiber.Ctx) error { - rows, err := h.conn.Query(context.Background(), "SELECT user_id, first_name, last_name, phone, email, address, profile_picture FROM users") + users, err := h.userRepository.GetUsers(c.Context()) if err != nil { - print(err.Error(), "from transactions err ") - return err - } - defer rows.Close() - - var users []models.User - for rows.Next() { - var user models.User - var firstName, lastName, phone, email, address, profilePicture *string - - if err := rows.Scan(&user.UserID, &firstName, &lastName, &phone, &email, &address, &profilePicture); err != nil { - print(err.Error(), "from transactions err ") - return err - } - - user.FirstName = *firstName - user.LastName = *lastName - user.Email = *email - user.Phone = phone - user.ProfilePicture = profilePicture - - users = append(users, user) - } - - if err := rows.Err(); err != nil { - print(err.Error(), "from transactions err ") return err } @@ -55,12 +27,12 @@ func (h *UserHandler) GetUsers(c *fiber.Ctx) error { func (h *UserHandler) GetUserById(c *fiber.Ctx) error { id := c.Params("id") - var user models.User - err := h.conn.QueryRow(context.Background(), "SELECT user_id, first_name, last_name, phone, email, profile_picture FROM users WHERE user_id = $1", id).Scan(&user.UserID, &user.FirstName, &user.LastName, &user.Phone, &user.Email, &user.ProfilePicture) + user, err := h.userRepository.GetUserByID(id, c.Context()) if err != nil { print(err.Error(), "from transactions err ") return err } + return c.Status(fiber.StatusOK).JSON(user) } diff --git a/backend/internal/service/server.go b/backend/internal/service/server.go index 6deeb505..008e506b 100644 --- a/backend/internal/service/server.go +++ b/backend/internal/service/server.go @@ -2,6 +2,7 @@ package service import ( "platnm/internal/service/handler" + "platnm/internal/storage/supabase" go_json "github.com/goccy/go-json" "github.com/gofiber/fiber/v2" @@ -28,9 +29,9 @@ func setupRoutes(app *fiber.App, conn *pgxpool.Pool) { app.Get("/health", func(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusOK) }) - // app.Get("/hello/:name", service.Hello) - userHandler := handler.NewUserHandler(conn) + repository := supabase.NewRepository(conn) + userHandler := handler.NewUserHandler(repository.User) app.Route("/users", func(r fiber.Router) { r.Get("/", userHandler.GetUsers) r.Get("/:id", userHandler.GetUserById) diff --git a/backend/internal/storage/storage.go b/backend/internal/storage/storage.go new file mode 100644 index 00000000..62457249 --- /dev/null +++ b/backend/internal/storage/storage.go @@ -0,0 +1,16 @@ +package storage + +import ( + "context" + "platnm/internal/models" +) + +type UserRepository interface { + GetUsers(ctx context.Context) ([]*models.User, error) + GetUserByID(id string, ctx context.Context) (*models.User, error) +} + +// Repository storage of all repositories. +type Repository struct { + User UserRepository +} diff --git a/backend/internal/storage/supabase/schema/user.go b/backend/internal/storage/supabase/schema/user.go new file mode 100644 index 00000000..03ecbeb1 --- /dev/null +++ b/backend/internal/storage/supabase/schema/user.go @@ -0,0 +1,65 @@ +package user + +import ( + "context" + "platnm/internal/models" + + "github.com/jackc/pgx/v5/pgxpool" +) + +type UserRepository struct { + db *pgxpool.Pool +} + +func (r *UserRepository) GetUsers(ctx context.Context) ([]*models.User, error) { + rows, err := r.db.Query(context.Background(), "SELECT user_id, first_name, last_name, phone, email, address, profile_picture FROM users") + if err != nil { + print(err.Error(), "from transactions err ") + return []*models.User{}, err + } + defer rows.Close() + + var users []*models.User + for rows.Next() { + var user models.User + var firstName, lastName, phone, email, address, profilePicture *string + + if err := rows.Scan(&user.UserID, &firstName, &lastName, &phone, &email, &address, &profilePicture); err != nil { + print(err.Error(), "from transactions err ") + return users, err + } + + user.FirstName = *firstName + user.LastName = *lastName + user.Email = *email + user.Phone = phone + user.ProfilePicture = profilePicture + + users = append(users, &user) + } + + if err := rows.Err(); err != nil { + print(err.Error(), "from transactions err ") + return []*models.User{}, err + } + + return users, nil +} + +func (r *UserRepository) GetUserByID(id string, ctx context.Context) (*models.User, error) { + var user models.User + err := r.db.QueryRow(context.Background(), "SELECT user_id, first_name, last_name, phone, email, profile_picture FROM users WHERE user_id = $1", id).Scan(&user.UserID, &user.FirstName, &user.LastName, &user.Phone, &user.Email, &user.ProfilePicture) + + if err != nil { + print(err.Error(), "from transactions err ") + return nil, err + } + + return &user, nil +} + +func NewUserRepository(db *pgxpool.Pool) *UserRepository { + return &UserRepository{ + db: db, + } +} diff --git a/backend/internal/database/database.go b/backend/internal/storage/supabase/storage.go similarity index 79% rename from backend/internal/database/database.go rename to backend/internal/storage/supabase/storage.go index b3e11c95..0f15bc1f 100644 --- a/backend/internal/database/database.go +++ b/backend/internal/storage/supabase/storage.go @@ -1,10 +1,11 @@ -package database +package supabase import ( "context" - "log" - "fmt" + "log" + "platnm/internal/storage" + user "platnm/internal/storage/supabase/schema" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" @@ -36,3 +37,9 @@ func ConnectDatabase(host, user, password, dbname, port string) *pgxpool.Pool { return conn } + +func NewRepository(db *pgxpool.Pool) *storage.Repository { + return &storage.Repository{ + User: user.NewUserRepository(db), + } +}