user.NewRepository   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
package user
2
3
import (
4
	"context"
5
6
	"github.com/jackc/pgx/v5/pgtype"
7
	"github.com/jackc/pgx/v5/pgxpool"
8
	db "github.com/memnix/memnix-rest/db/sqlc"
9
)
10
11
// SQLRepository is the repository for the user.
12
type SQLRepository struct {
13
	q *db.Queries // q is the sqlc queries.
14
}
15
16
// NewRepository returns a new repository.
17
func NewRepository(dbConn *pgxpool.Pool) IRepository {
18
	q := db.New(dbConn)
19
	return &SQLRepository{q: q}
20
}
21
22
// GetName returns the name of the user.
23
func (r *SQLRepository) GetName(ctx context.Context, id int32) string {
24
	userName, err := r.q.GetUserName(ctx, id)
25
	if err != nil {
26
		return ""
27
	}
28
	return userName.String
29
}
30
31
// GetByID returns the user with the given id.
32
func (r *SQLRepository) GetByID(ctx context.Context, id int32) (db.User, error) {
33
	return r.q.GetUser(ctx, id)
34
}
35
36
// GetByEmail returns the user with the given email.
37
func (r *SQLRepository) GetByEmail(ctx context.Context, email string) (db.User, error) {
38
	user, err := r.q.GetUserByEmail(ctx, email)
39
	return user, err
40
}
41
42
// Create creates a new user.
43
func (r *SQLRepository) Create(ctx context.Context, email, password, username string) error {
44
	_, err := r.q.CreateUser(ctx, db.CreateUserParams{
45
		Email:    email,
46
		Password: password,
47
		Username: pgtype.Text{String: username, Valid: true},
48
	})
49
	return err
50
}
51
52
// Update updates the user with the given id.
53
func (r *SQLRepository) Update(ctx context.Context, id int32, email, password, username string) error {
54
	_, err := r.q.UpdateUser(ctx, db.UpdateUserParams{
55
		ID:       id,
56
		Email:    email,
57
		Password: password,
58
		Username: pgtype.Text{String: username, Valid: true},
59
	})
60
61
	return err
62
}
63
64
// Delete deletes the user with the given id.
65
func (r *SQLRepository) Delete(ctx context.Context, id int32) error {
66
	return r.q.DeleteUser(ctx, id)
67
}
68
69
// GetByOauthID returns the user with the given oauth id.
70
func (r *SQLRepository) GetByOauthID(_ context.Context, _ string) (db.User, error) {
71
	// err := r.DBConn.WithContext(ctx).Where("oauth_id = ?", id).First(&user).Error
72
	return db.User{}, nil
73
}
74