database

package
v0.0.0-...-9da8cb4 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDialectorNotFound  = fmt.Errorf("url-shortener/internal/database: dialector not found")
	ErrKeyNotFound        = errors.New("url-shortener/internal/database: key not found")
	ErrApiKeyNotExists    = errors.New("url-shortener/internal/database: api key doesn't exist")
	ErrShortLinkExists    = errors.New("url-shortener/internal/database: short link already exists")
	ErrShortLinkNotExists = errors.New("url-shortener/internal/database: short link doesn't exist")
)

Functions

This section is empty.

Types

type ApiKey

type ApiKey struct {
	Model
	ApiKey           uuid.UUID   `gorm:"primaryKey;type:uuid;default:gen_random_uuid()" json:"api_key"`
	AvailableCredits int         `gorm:"default:5" json:"available_credits"`
	UtilizedCredits  int         `gorm:"default:0" json:"utilized_credits"`
	ShortLinks       []ShortLink `gorm:"foreignKey:ApiKey" json:"short_links"`
}

ApiKey represents an api_keys table in the database.

type Database

type Database struct {
	DB *gorm.DB
}

Database represents a database instance.

func New

func New(d Dialector) Database

New initializes a new Database instance based on the provided Dialector.

It takes a Dialector as a parameter and returns a Database.

func (Database) CreateApiKey

func (db Database) CreateApiKey(key ...uuid.UUID) (*ApiKey, error)

CreateApiKey creates a new API key in the database.

func (db Database) CreateShortLink(apiKey uuid.UUID, shortname string, url string) (*ShortLink, error)

CreateShortLink creates a short link in the database.

func (Database) GetApiKeyBy

func (db Database) GetApiKeyBy(key string, value interface{}) (*ApiKey, error)

GetApiKeyBy retrieves an API key by a specified key and value.

func (Database) GetApiKeyByApiKey

func (db Database) GetApiKeyByApiKey(apiKey uuid.UUID) *ApiKey

GetApiKeyByApiKey returns an API key based on the provided API key.

func (Database) GetShortLinkBy

func (db Database) GetShortLinkBy(key string, value interface{}) (*ShortLink, error)

GetShortLinkBy retrieves a ShortLink from the Database based on the specified key and value.

func (Database) GetShortLinkByApiKey

func (db Database) GetShortLinkByApiKey(apiKey uuid.UUID) *ShortLink

GetShortLinkByApiKey retrieves a ShortLink by the provided API key.

func (Database) GetShortLinkByShortname

func (db Database) GetShortLinkByShortname(shortname string) *ShortLink

GetShortLinkByShortname retrieves a ShortLink from the Database based on the given shortname.

func (Database) GetShortLinkByURL

func (db Database) GetShortLinkByURL(url string) *ShortLink

GetShortLinkByURL retrieves a short link by the given URL.

type Dialector

type Dialector string

Dialector represents a database dialect.

const (
	// DialectorPostgres represents the Postgres dialect.
	DialectorPostgres Dialector = "postgres"
)

type Model

type Model struct {
	CreatedAt time.Time      `gorm:"autoCreateTime:nano" json:"created_at"`
	UpdatedAt time.Time      `gorm:"autoUpdateTime:nano" json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"`
}

Model represents a base model.

type ShortLink struct {
	Model
	ApiKey    uuid.UUID `gorm:"type:uuid,not null" json:"api_key"`
	Shortname string    `gorm:"unique,not null" json:"shortname"`
	URL       string    `gorm:"not null" json:"url"`
}

ShortLink represents a short_links table in the database.

Jump to

Keyboard shortcuts

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