models

package
v0.0.0-...-8d09c37 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MIGRATIONS_DIR_URI = "file://internal/models/migrations"
)
View Source
const ServiceTableName = "services"
View Source
const UserTableName = "users"
View Source
const VersionTableName = "versions"

Variables

View Source
var (
	ErrRecordNotFound            = errors.New("record not found")
	ErrUniqueConstraintViolation = errors.New("unique key constraint violated")
)
View Source
var DB *gorm.DB

Functions

func GetPasswordHash

func GetPasswordHash(password string) (string, error)

func InitDB

func InitDB() error

InitDB initializes the database handler.

func Migrate

func Migrate(migrationsDirUri string, destroy bool) error

Migrate runs the migrations present in the specified URI. If destroy is true, the migrations are run downwards than upwards.

func SetDBConfiguration

func SetDBConfiguration() error

SetDBConfiguration reads the database connection configuration from env vars. This NEEDS to be called before InitDB().

Types

type CreateServiceInput

type CreateServiceInput struct {
	Name        string `json:"name" binding:"required,max=50"`
	Description string `json:"description"`
	UserID      uint
}

CreateServiceInput represents the input required to create a Service.

type CreateVersionInput

type CreateVersionInput struct {
	Version   string `json:"version" binding:"required,max=50"`
	ServiceID int
	Changelog string `json:"changelog"`
	UserID    uint
}

CreateVersionInput represents the input required to create Version object.

type GetServiceWithVersionsTxOutput

type GetServiceWithVersionsTxOutput struct {
	ServiceID        uint
	ServiceCreatedAt time.Time
	ServiceUpdatedAt time.Time
	Name             string
	Description      string
	VersionID        uint
	VersionCreatedAt time.Time
	VersionUpdatedAt time.Time
	Version          string
	Changelog        string
}

GetServiceWithVersionsTxOutput represents the various columns returned by the database query made in GetServiceWithVersions().

func GetServiceWithVersions

func GetServiceWithVersions(svcID uint, userID uint) ([]GetServiceWithVersionsTxOutput, error)

GetServiceWithVersions returns the requested Service for the provided ID along of the Version objects belonging to this Service.

type ListServicesInput

type ListServicesInput struct {
	Limit      int `form:"limit"`
	Offset     int `form:"offset"`
	UserID     uint
	SortKey    string `form:"sortKey"`
	Descending bool   `form:"descending"`
	Name       string `form:"name"`
}

ListServicesInput represnts the different input parameters that can be included in the database query. The form struct tags allows for convinient query parameter validation.

type Model

type Model struct {
	ID        uint      `json:"id"`
	CreatedAt time.Time `json:"createdAt"`
	UpdatedAt time.Time `json:"updatedAt"`
}

Model contains common fields across all tables. It is exactly like gorm.Model with the exception of JSON field tags.

type Service

type Service struct {
	Model
	Name        string `json:"name"`
	Description string `json:"description"`
	// Versions contains the different versions of this service.
	// It helps us fetch the versions without a JOIN query.
	Versions pq.StringArray `json:"versions" gorm:"type:varchar(50)[]"`
	UserID   int            `json:"userID"`
}

Service represents a single service in the catalog.

func CreateService

func CreateService(input CreateServiceInput) (*Service, error)

CreateService creates a new Service.

func GetService

func GetService(svcID uint, userID uint) (*Service, error)

GetService returns the Service for the provided ID.

func ListServices

func ListServices(input ListServicesInput) ([]Service, error)

ListServices returns a list of Service objects based on the different input parameters.

func UpdateService

func UpdateService(input UpdateServiceInput, id uint, userID uint) (*Service, error)

type UpdateServiceInput

type UpdateServiceInput struct {
	Name        string `json:"name" binding:"max=50"`
	Description string `json:"description"`
}

CreateServiceInput represents the input required to create a Service.

type User

type User struct {
	Model
	Username string `json:"username"`
	Password string `json:"-"`
}

User represents a registered user.

func CreateUser

func CreateUser(username, password string) (*User, error)

CreateUser creates a user with the provided username and password.

func GetUserByID

func GetUserByID(id uint) (*User, error)

GetUserByID returns the User for the provided ID.

func GetUserByUsername

func GetUserByUsername(username string) (*User, error)

GetUserByUsername returns the User for the provided username.

type Version

type Version struct {
	Model
	Version   string `json:"version"`
	ServiceID int    `json:"serviceID"`
	Changelog string `json:"changelog"`
}

Version represents a version of a Service.

func CreateVersion

func CreateVersion(input CreateVersionInput) (*Version, error)

CreateVersion fetches the Service with the provided id, and if it exists, it creates a new Version according to the input and then update the related Service with the new version string.

Jump to

Keyboard shortcuts

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