-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
103 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters