121 lines
2.8 KiB
Go
121 lines
2.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/domain"
|
|
"github.com/huandu/go-sqlbuilder"
|
|
)
|
|
|
|
const (
|
|
refreshTokenTableName = "RefreshTokens"
|
|
)
|
|
|
|
type RefreshToken interface {
|
|
Create(ctx context.Context, username string, token string) (int64, error)
|
|
GetByUsername(ctx context.Context, name string) (domain.RefreshTokenEntity, error)
|
|
DeleteById(ctx context.Context, id int64) (int64, error)
|
|
}
|
|
|
|
type RefreshTokenRepository struct {
|
|
connection *sql.DB
|
|
}
|
|
|
|
func NewRefreshTokenRepository(conn *sql.DB) RefreshTokenRepository {
|
|
return RefreshTokenRepository{
|
|
connection: conn,
|
|
}
|
|
}
|
|
|
|
func (rt RefreshTokenRepository) Create(ctx context.Context, username string, token string) (int64, error) {
|
|
dt := time.Now()
|
|
builder := sqlbuilder.NewInsertBuilder()
|
|
builder.InsertInto(refreshTokenTableName)
|
|
builder.Cols("Username", "Token", "CreatedAt", "UpdatedAt", "DeletedAt")
|
|
builder.Values(username, token, dt, dt, time.Time{})
|
|
query, args := builder.Build()
|
|
|
|
_, err := rt.connection.ExecContext(ctx, query, args...)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return 1, nil
|
|
}
|
|
|
|
func (rt RefreshTokenRepository) GetByUsername(ctx context.Context, name string) (domain.RefreshTokenEntity, error) {
|
|
builder := sqlbuilder.NewSelectBuilder()
|
|
builder.Select("*").From(refreshTokenTableName).Where(
|
|
builder.E("Username", name),
|
|
)
|
|
|
|
query, args := builder.Build()
|
|
rows, err := rt.connection.QueryContext(ctx, query, args...)
|
|
if err != nil {
|
|
return domain.RefreshTokenEntity{}, err
|
|
}
|
|
|
|
data := rt.processRows(rows)
|
|
if len(data) == 0 {
|
|
return domain.RefreshTokenEntity{}, errors.New("no token found for user")
|
|
}
|
|
|
|
return data[0], nil
|
|
}
|
|
|
|
func (rt RefreshTokenRepository) DeleteById(ctx context.Context, id int64) (int64, error) {
|
|
builder := sqlbuilder.NewDeleteBuilder()
|
|
builder.DeleteFrom(refreshTokenTableName)
|
|
builder.Where(
|
|
builder.EQ("Id", id),
|
|
)
|
|
|
|
query, args := builder.Build()
|
|
rows, err := rt.connection.ExecContext(ctx, query, args...)
|
|
if err != nil {
|
|
return -1, err
|
|
}
|
|
|
|
return rows.RowsAffected()
|
|
}
|
|
|
|
func (rd RefreshTokenRepository) processRows(rows *sql.Rows) []domain.RefreshTokenEntity {
|
|
items := []domain.RefreshTokenEntity{}
|
|
|
|
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)
|
|
}
|
|
|
|
item := domain.RefreshTokenEntity{
|
|
ID: id,
|
|
Username: username,
|
|
Token: token,
|
|
CreatedAt: createdAt,
|
|
UpdatedAt: updatedAt,
|
|
}
|
|
|
|
if (deletedAt.Valid) {
|
|
item.DeletedAt = deletedAt.Time
|
|
}
|
|
|
|
items = append(items, item)
|
|
}
|
|
|
|
return items
|
|
}
|
|
|
|
//func (rt RefreshTokenRepository) Delete()
|