115 lines
3.4 KiB
Go
115 lines
3.4 KiB
Go
package bunstorage
|
|
|
|
import (
|
|
"time"
|
|
|
|
"git.maronato.dev/maronato/goshort/internal/storage/models"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
type UserModel struct {
|
|
bun.BaseModel `bun:"table:users,alias:u"`
|
|
|
|
// ID is the primary key
|
|
ID string `bun:",pk,unique,notnull" json:"id"`
|
|
// Username is the user's username
|
|
Username string `bun:",unique,notnull" json:"username"`
|
|
// Password is the user's password
|
|
Password string `bun:",notnull" json:"-"`
|
|
// CreatedAt is when the user was created (initialized by the storage)
|
|
CreatedAt time.Time `bun:",notnull,default:current_timestamp" json:"createdAt"`
|
|
}
|
|
|
|
func (u *UserModel) toUser() *models.User {
|
|
return models.NewAuthenticatableUser(&models.User{
|
|
ID: u.ID,
|
|
Username: u.Username,
|
|
CreatedAt: u.CreatedAt,
|
|
}, u.Password)
|
|
}
|
|
|
|
type ShortModel struct {
|
|
bun.BaseModel `bun:"table:shorts,alias:s" json:"-"`
|
|
|
|
ID string `bun:",pk,unique,notnull" json:"id"`
|
|
// Name is the short's name, which is also the path to access it
|
|
Name string `bun:",unique,notnull" json:"name"`
|
|
// URL is the URL that the short will redirect to
|
|
URL string `bun:",notnull" json:"url"`
|
|
// Deleted is whether the short is soft-deleted or not
|
|
Deleted bool `bun:",notnull,default:false" json:"-"`
|
|
// CreatedAt is when the short was created (initialized by the storage)
|
|
CreatedAt time.Time `bun:",notnull,default:current_timestamp" json:"createdAt"`
|
|
// DeletedAt is when the short was deleted
|
|
DeletedAt time.Time `bun:"" json:"-"`
|
|
|
|
// UserID is the ID of the user that created the short
|
|
// This can be null if the short was deleted
|
|
UserID *string `json:"-"`
|
|
}
|
|
|
|
func (s *ShortModel) toShort() *models.Short {
|
|
return &models.Short{
|
|
ID: s.ID,
|
|
Name: s.Name,
|
|
URL: s.URL,
|
|
CreatedAt: s.CreatedAt,
|
|
UserID: s.UserID,
|
|
}
|
|
}
|
|
|
|
type TokenModel struct {
|
|
bun.BaseModel `bun:"table:tokens,alias:t"`
|
|
|
|
// ID is the primary key
|
|
ID string `bun:",pk,unique,notnull" json:"id"`
|
|
// Name is the user-friendly name of the token
|
|
Name string `bun:",notnull" json:"name"`
|
|
// Value is the actual token
|
|
Value string `bun:",unique,notnull" json:"value"`
|
|
// CreatedAt is when the token was created (initialized by the storage)
|
|
CreatedAt time.Time `bun:",notnull,default:current_timestamp" json:"createdAt"`
|
|
|
|
// UserID is the ID of the user that created the token
|
|
UserID *string `bun:",notnull" json:"-"`
|
|
}
|
|
|
|
func (t *TokenModel) toToken() *models.Token {
|
|
return &models.Token{
|
|
ID: t.ID,
|
|
Name: t.Name,
|
|
Value: t.Value,
|
|
CreatedAt: t.CreatedAt,
|
|
UserID: t.UserID,
|
|
}
|
|
}
|
|
|
|
type ShortLogModel struct {
|
|
bun.BaseModel `bun:"table:short_logs,alias:sl"`
|
|
|
|
// ID is the primary key
|
|
ID string `bun:",pk,unique,notnull" json:"id"`
|
|
// IPAddress is the IP address of the client that accessed the short
|
|
IPAddress string `bun:"" json:"ipAddress"`
|
|
// UserAgent is the User-Agent of the client that accessed the short
|
|
UserAgent string `bun:"" json:"userAgent"`
|
|
// Referer is the referer of the client that accessed the short
|
|
Referer string `bun:"" json:"referer"`
|
|
// CreatedAt is when the short was accessed
|
|
CreatedAt time.Time `bun:",notnull,default:current_timestamp" json:"createdAt"`
|
|
|
|
// ShortID is the ID of the short that was accessed
|
|
ShortID string `bun:",notnull" json:"-"`
|
|
}
|
|
|
|
func (sl *ShortLogModel) toShortLog() *models.ShortLog {
|
|
return &models.ShortLog{
|
|
ID: sl.ID,
|
|
IPAddress: sl.IPAddress,
|
|
UserAgent: sl.UserAgent,
|
|
Referer: sl.Referer,
|
|
CreatedAt: sl.CreatedAt,
|
|
ShortID: sl.ShortID,
|
|
}
|
|
}
|