|
@@ -25,7 +25,7 @@ func InitUserRepository(db *sql.DB) interfaces.UserRepository {
|
|
|
return userRepository{db: db}
|
|
|
}
|
|
|
|
|
|
-func (r userRepository) Get(ctx context.Context, username string) (*models.User, error) {
|
|
|
+func (u userRepository) Get(ctx context.Context, username string) (*models.User, error) {
|
|
|
query, args, err := sq.Select("id", "username", "password", "created_at", "updated_at").
|
|
|
From(userTableName).
|
|
|
PlaceholderFormat(sq.Dollar).
|
|
@@ -37,7 +37,7 @@ func (r userRepository) Get(ctx context.Context, username string) (*models.User,
|
|
|
}
|
|
|
|
|
|
var res models.User
|
|
|
- err = r.db.QueryRowContext(ctx, query, args...).
|
|
|
+ err = u.db.QueryRowContext(ctx, query, args...).
|
|
|
Scan(&res.ID, &res.Username, &res.Password, &res.CreatedAt, &res.UpdatedAt)
|
|
|
|
|
|
if err != nil {
|
|
@@ -47,7 +47,7 @@ func (r userRepository) Get(ctx context.Context, username string) (*models.User,
|
|
|
return &res, nil
|
|
|
}
|
|
|
|
|
|
-func (r userRepository) Add(ctx context.Context, username string, password string) (int, error) {
|
|
|
+func (u userRepository) Add(ctx context.Context, username string, password string) (int, error) {
|
|
|
query, args, err := sq.Insert(userTableName).
|
|
|
PlaceholderFormat(sq.Dollar).
|
|
|
Columns("username", "password").
|
|
@@ -60,9 +60,27 @@ func (r userRepository) Add(ctx context.Context, username string, password strin
|
|
|
}
|
|
|
|
|
|
var id int
|
|
|
- if err = r.db.QueryRowContext(ctx, query, args...).Scan(&id); err != nil {
|
|
|
+ if err = u.db.QueryRowContext(ctx, query, args...).Scan(&id); err != nil {
|
|
|
return 0, err
|
|
|
}
|
|
|
|
|
|
return id, nil
|
|
|
}
|
|
|
+
|
|
|
+func (u userRepository) UpdatePassword(ctx context.Context, id int, newPassword string) error {
|
|
|
+ query, args, err := sq.Update(userTableName).
|
|
|
+ PlaceholderFormat(sq.Dollar).
|
|
|
+ Set("password", newPassword).
|
|
|
+ Set("updated_at", "NOW()").
|
|
|
+ Where(sq.Eq{"id": id}).
|
|
|
+ ToSql()
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ _, err = u.db.ExecContext(ctx, query, args...)
|
|
|
+
|
|
|
+ return err
|
|
|
+
|
|
|
+}
|