Skip to content

Commit

Permalink
move db logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ddusichka committed Sep 9, 2024
1 parent 8976324 commit c97d1ef
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 42 deletions.
4 changes: 2 additions & 2 deletions backend/cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
Expand Down
42 changes: 7 additions & 35 deletions backend/internal/service/handler/user.go
Original file line number Diff line number Diff line change
@@ -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
}

Expand All @@ -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)
}
5 changes: 3 additions & 2 deletions backend/internal/service/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions backend/internal/storage/storage.go
Original file line number Diff line number Diff line change
@@ -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
}
65 changes: 65 additions & 0 deletions backend/internal/storage/supabase/schema/user.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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),
}
}

0 comments on commit c97d1ef

Please sign in to comment.