model

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ScopeActivation     = "activation"
	ScopeAuthentication = "authentication"
)

Variables

This section is empty.

Functions

func ValidateTokenPlaintext

func ValidateTokenPlaintext(tokenPlaintext string) error

ValidateTokenPlaintext validates a token plaintext. token must not be empty and be 26 bytes long.

Types

type Models

type Models struct {
	Urls   UrlModel
	Users  UserModel
	Roles  RoleModel
	Tokens TokenModel
}

func NewModels

func NewModels(db *gorm.DB) Models

type Role

type Role struct {
	gorm.Model
	Name string `gorm:"type:varchar(255)"`
}

type RoleModel

type RoleModel struct {
	DB *gorm.DB
}

type Session

type Session struct {
	gorm.Model
	Token  string    `gorm:"size:43;uniqueIndex"`
	Data   []byte    `gorm:"not null"`
	Expiry time.Time `gorm:"not null;index"`
}

type Token

type Token struct {
	gorm.Model
	Plaintext string    `json:"token"`
	Hash      []byte    `json:"-"`
	UserID    uuid.UUID `json:"-"`
	User      User      `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
	Expiry    time.Time `json:"expiry"`
	Scope     string    `json:"-"`
}

type TokenModel

type TokenModel struct {
	DB *gorm.DB
}

func (TokenModel) DeleteAllForUser

func (m TokenModel) DeleteAllForUser(scope string, userID uuid.UUID) error

func (TokenModel) GetUserID

func (m TokenModel) GetUserID(scope, token string) (uuid.UUID, error)

GetUserID returns a user for a given token.

func (TokenModel) Insert

func (m TokenModel) Insert(token *Token) error

func (TokenModel) New

func (m TokenModel) New(userID uuid.UUID, ttl time.Duration, scope string) (*Token, error)

type Url

type Url struct {
	gorm.Model
	ID       uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();primary_key" json:"id,omitempty"`
	Original string    `gorm:"type:varchar(2048);not null;uniqueIndex" json:"original"`
	ShortUrl string    `gorm:"type:varchar(11);not null;uniqueIndex" json:"short_url"`
	UserID   uuid.UUID `gorm:"type:uuid" json:"user_id"`
	User     User      `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
	Visits   int       `gorm:"default:0" json:"visits"`
}

type UrlByUserRequest

type UrlByUserRequest struct {
	ID uuid.UUID `json:"user_id"`
}

type UrlByUserResponse

type UrlByUserResponse struct {
	ID        uuid.UUID `json:"id"`
	Original  string    `json:"original"`
	ShortUrl  string    `json:"short_url"`
	Visits    int       `json:"visits"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

type UrlCreateRequest

type UrlCreateRequest struct {
	Original  string    `json:"original" validate:"required,url"`
	ShortCode string    `json:"short_code" validate:"omitempty,alphanum,min=3,max=11"`
	UserID    uuid.UUID `json:"user_id"`
}

type UrlDeleteRequest

type UrlDeleteRequest struct {
	ID uuid.UUID `json:"id"`
}

type UrlDeleteResponse

type UrlDeleteResponse struct {
	Message string `json:"message"`
}

type UrlModel

type UrlModel struct {
	DB *gorm.DB
}

Create a UrlModel struct which wraps the connection pool.

func (*UrlModel) Create

func (u *UrlModel) Create(urlReq *UrlCreateRequest) (Url, error)

func (*UrlModel) Delete

func (u *UrlModel) Delete(urlUUID uuid.UUID) error

func (*UrlModel) Find

func (u *UrlModel) Find(urlUUID uuid.UUID) *Url

func (*UrlModel) GetRedirect

func (u *UrlModel) GetRedirect(shortUrl string) (Url, error)

func (*UrlModel) GetUrlByUser

func (u *UrlModel) GetUrlByUser(userId uuid.UUID) (*[]UrlByUserResponse, error)

GetUrlByUser returns all URLs for a given user

type UrlResponse

type UrlResponse struct {
	ID      uuid.UUID `json:"id"`
	FullUrl string    `json:"full_url"`
}

type User

type User struct {
	gorm.Model
	ID        uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();primary_key"`
	Name      string    `gorm:"type:varchar(255)"`
	Email     string    `gorm:"not null;uniqueIndex"`
	Password  string    `gorm:"not null"`
	Role      string    `gorm:"default:'user'"`
	Activated bool      `gorm:"default:false"`
}

type UserLoginRequest

type UserLoginRequest struct {
	Email    string `json:"email" validate:"required,email"`
	Password string `json:"password" validate:"required,min=8,max=72"`
}

type UserLoginResponse

type UserLoginResponse struct {
	ID    uuid.UUID `json:"id"`
	Email string    `json:"email"`
	Token string    `json:"token"`
}

type UserModel

type UserModel struct {
	DB *gorm.DB
}

func (*UserModel) Activate

func (u *UserModel) Activate(id uuid.UUID) error

Activate sets the activated flag to true for a user.

func (*UserModel) GetByEmail

func (u *UserModel) GetByEmail(email string) (*User, error)

GetByEmail returns a user based on the email address.

func (*UserModel) GetByID

func (u *UserModel) GetByID(id uuid.UUID) (*User, error)

func (*UserModel) GetRole

func (u *UserModel) GetRole(c echo.Context) (string, error)

func (*UserModel) List

func (u *UserModel) List() ([]User, error)

func (*UserModel) Login

func (u *UserModel) Login(email, password string) (*User, error)

func (*UserModel) Register

func (u *UserModel) Register(body *UserRegisterReq) (UserResponse, error)

type UserRegisterReq

type UserRegisterReq struct {
	Email           string `json:"email" validate:"required,email"`
	Name            string `json:"name" validate:"required"`
	Password        string `json:"password" validate:"required,min=8,max=72"`
	PasswordConfirm string `json:"password_confirm" validate:"required,min=8,max=72"`
}

type UserResponse

type UserResponse struct {
	ID        uuid.UUID `json:"id"`
	Email     string    `json:"email"`
	Name      string    `json:"name"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL