2024-04-26 16:03:32 -07:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2024-05-05 10:02:17 -07:00
|
|
|
"context"
|
2024-04-26 16:03:32 -07:00
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2024-05-09 19:08:57 -07:00
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/entity"
|
2024-04-26 16:03:32 -07:00
|
|
|
"github.com/huandu/go-sqlbuilder"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
refreshTokenTableName = "RefreshTokens"
|
|
|
|
)
|
|
|
|
|
2024-04-28 11:39:25 -07:00
|
|
|
type RefreshToken interface {
|
2024-05-05 10:02:17 -07:00
|
|
|
Create(ctx context.Context, username string, token string) (int64, error)
|
2024-05-09 19:08:57 -07:00
|
|
|
GetByUsername(ctx context.Context, name string) (entity.RefreshTokenEntity, error)
|
2024-05-05 10:02:17 -07:00
|
|
|
DeleteById(ctx context.Context, id int64) (int64, error)
|
2024-04-26 16:03:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type RefreshTokenRepository struct {
|
|
|
|
connection *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRefreshTokenRepository(conn *sql.DB) RefreshTokenRepository {
|
|
|
|
return RefreshTokenRepository{
|
|
|
|
connection: conn,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-05 10:02:17 -07:00
|
|
|
func (rt RefreshTokenRepository) Create(ctx context.Context, username string, token string) (int64, error) {
|
2024-04-26 16:03:32 -07:00
|
|
|
dt := time.Now()
|
|
|
|
builder := sqlbuilder.NewInsertBuilder()
|
|
|
|
builder.InsertInto(refreshTokenTableName)
|
2024-05-05 10:02:17 -07:00
|
|
|
builder.Cols("Username", "Token", "CreatedAt", "UpdatedAt", "DeletedAt")
|
|
|
|
builder.Values(username, token, dt, dt, time.Time{})
|
2024-04-26 16:03:32 -07:00
|
|
|
query, args := builder.Build()
|
|
|
|
|
2024-05-05 10:02:17 -07:00
|
|
|
_, err := rt.connection.ExecContext(ctx, query, args...)
|
2024-04-26 16:03:32 -07:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1, nil
|
|
|
|
}
|
|
|
|
|
2024-05-09 19:08:57 -07:00
|
|
|
func (rt RefreshTokenRepository) GetByUsername(ctx context.Context, name string) (entity.RefreshTokenEntity, error) {
|
2024-04-26 16:03:32 -07:00
|
|
|
builder := sqlbuilder.NewSelectBuilder()
|
|
|
|
builder.Select("*").From(refreshTokenTableName).Where(
|
|
|
|
builder.E("Username", name),
|
|
|
|
)
|
|
|
|
|
|
|
|
query, args := builder.Build()
|
2024-05-05 10:02:17 -07:00
|
|
|
rows, err := rt.connection.QueryContext(ctx, query, args...)
|
2024-04-26 16:03:32 -07:00
|
|
|
if err != nil {
|
2024-05-09 19:08:57 -07:00
|
|
|
return entity.RefreshTokenEntity{}, err
|
2024-04-26 16:03:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
data := rt.processRows(rows)
|
|
|
|
if len(data) == 0 {
|
2024-05-09 19:08:57 -07:00
|
|
|
return entity.RefreshTokenEntity{}, errors.New("no token found for user")
|
2024-04-26 16:03:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return data[0], nil
|
|
|
|
}
|
|
|
|
|
2024-05-05 10:02:17 -07:00
|
|
|
func (rt RefreshTokenRepository) DeleteById(ctx context.Context, id int64) (int64, error) {
|
2024-04-26 16:03:32 -07:00
|
|
|
builder := sqlbuilder.NewDeleteBuilder()
|
|
|
|
builder.DeleteFrom(refreshTokenTableName)
|
|
|
|
builder.Where(
|
|
|
|
builder.EQ("Id", id),
|
|
|
|
)
|
|
|
|
|
|
|
|
query, args := builder.Build()
|
2024-05-05 10:02:17 -07:00
|
|
|
rows, err := rt.connection.ExecContext(ctx, query, args...)
|
2024-04-26 16:03:32 -07:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rows.RowsAffected()
|
|
|
|
}
|
|
|
|
|
2024-05-09 19:08:57 -07:00
|
|
|
func (rd RefreshTokenRepository) processRows(rows *sql.Rows) []entity.RefreshTokenEntity {
|
|
|
|
items := []entity.RefreshTokenEntity{}
|
2024-04-26 16:03:32 -07:00
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var id int64
|
|
|
|
var username string
|
|
|
|
var token string
|
|
|
|
var createdAt time.Time
|
|
|
|
var updatedAt time.Time
|
|
|
|
var deletedAt sql.NullTime
|
|
|
|
|
|
|
|
err := rows.Scan(&id, &createdAt, &updatedAt, &deletedAt, &username, &token)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
2024-05-09 19:08:57 -07:00
|
|
|
item := entity.RefreshTokenEntity{
|
2024-04-26 16:03:32 -07:00
|
|
|
ID: id,
|
|
|
|
Username: username,
|
|
|
|
Token: token,
|
|
|
|
CreatedAt: createdAt,
|
|
|
|
UpdatedAt: updatedAt,
|
|
|
|
}
|
2024-05-09 19:08:57 -07:00
|
|
|
|
|
|
|
if deletedAt.Valid {
|
2024-04-26 16:03:32 -07:00
|
|
|
item.DeletedAt = deletedAt.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
items = append(items, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return items
|
|
|
|
}
|
|
|
|
|
|
|
|
//func (rt RefreshTokenRepository) Delete()
|