models

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const AvgWordsPerMinute = 250

AvgWordsPerMinute is the average number of words per minute a person can read.

Variables

View Source
var (
	ErrValidationFailed = errors.New("ERR_VALIDATION_FAILED")
	ErrNotFound         = errors.New("ERR_NOT_FOUND") // ErrNotFound is returned if item is not found
	ErrDuplicate        = errors.New("ERR_DUPLICATE") // ErrDuplicate is returned if item already exists

	ErrUserLoginRequired      = errors.New("ERR_USER_LOGIN_REQUIRED")
	ErrUserExternalIDRequired = errors.New("ERR_USER_EXTERNAL_ID_REQUIRED")
	ErrUserAuthMethodRequired = errors.New("ERR_USER_AUTH_METHOD_REQUIRED")
	ErrFailedToCreateUser     = errors.New("ERR_FAILED_TO_CREATE_USER")
	ErrFailedToGetUser        = errors.New("ERR_FAILED_TO_GET_USER")

	ErrPostURLRequired         = errors.New("ERR_POST_URL_REQUIRED")
	ErrPostTitleRequired       = errors.New("ERR_POST_TITLE_REQUIRED")
	ErrPostDescriptionRequired = errors.New("ERR_POST_DESCRIPTION_REQUIRED")
	ErrPostContentRequired     = errors.New("ERR_POST_CONTENT_REQUIRED")
	ErrPostWrongKeywordsString = errors.New("ERR_POST_WRONG_KEYWORDS_STRING")
	ErrPostInvalidSlug         = errors.New("ERR_POST_INVALID_SLUG")
	ErrPostUserIDRequired      = errors.New("ERR_POST_USER_ID_REQUIRED")

	ErrCreateSubscription        = errors.New("ERR_CREATE_SUBSCRIPTION")
	ErrSubscriptionEmailRequired = errors.New("ERR_SUBSCRIPTION_EMAIL_REQUIRED")
	ErrGetSubscription           = errors.New("ERR_GET_SUBSCRIPTION")
	ErrGetSubscriptionEmails     = errors.New("ERR_GET_SUBSCRIPTION_EMAILS")
	ErrDeleteSubscription        = errors.New("ERR_DELETE_SUBSCRIPTION")
	ErrUpdateSubscription        = errors.New("ERR_UPDATE_SUBSCRIPTION")
)

Functions

This section is empty.

Types

type AuthMethod

type AuthMethod string

AuthMethod is the method of authentication used by the user.

const (
	GitHubAuthMethod AuthMethod = "github"
)

type Post

type Post struct {
	ID                  int       `json:"id" gorm:"primaryKey;autoIncrement"`
	Slug                string    `json:"slug" gorm:"uniqueIndex"` // Slug is the URL friendly version of the title
	Title               string    `json:"title"`
	Description         string    `json:"description"`
	Keywords            string    `json:"keywords"` // Keywords are comma separated
	Content             string    `json:"content"`
	ReadingTime         int       `json:"reading_time"` // ReadingTime is the estimated time to read the post in seconds
	UserID              int       `json:"user_id" gorm:"not null;constraint:OnUpdate:CASCADE;foreignKey:ID;references:ID"`
	SentToSubscribersAt time.Time `json:"sent_to_subscribers_at" gorm:"default:null"` // If not null, the post was sent
	CreatedAt           time.Time `json:"created_at"`
	UpdatedAt           time.Time `json:"updated_at"`
}

Post is the model for the post-data.

func (*Post) BeforeCreate

func (p *Post) BeforeCreate(_ *gorm.DB) error

func (*Post) BeforeUpdate

func (p *Post) BeforeUpdate(_ *gorm.DB) error

func (*Post) CountReadingTime

func (p *Post) CountReadingTime() time.Duration

CountReadingTime counts the reading time of the post.

func (*Post) Validate

func (p *Post) Validate() error

type PostRepository

type PostRepository struct {
	// contains filtered or unexported fields
}

PostRepository is the database for the post data.

func NewPostRepository

func NewPostRepository(conn *gorm.DB) *PostRepository

NewPostRepository creates a new PostRepository.

func (*PostRepository) Count

func (db *PostRepository) Count(ctx context.Context) (int64, error)

Count returns the total number of posts.

func (*PostRepository) Create

func (db *PostRepository) Create(ctx context.Context, p *Post) error

Create creates a new Post.

func (*PostRepository) FindAll

func (db *PostRepository) FindAll(ctx context.Context, page, perPage int) ([]*Post, error)

FindAll returns all the posts with pagination, sorted by the created time. Selects only the necessary fields to reduce the payload - slug, title, description, keywords, created_at, sent_to_subscribers_at.

func (*PostRepository) GetBySlug

func (db *PostRepository) GetBySlug(ctx context.Context, slug string) (*Post, error)

GetBySlug finds a Post by its URL Slug.

func (*PostRepository) Update

func (db *PostRepository) Update(ctx context.Context, p *Post) error

Update updates the Post.

type PostRepositoryInterface

type PostRepositoryInterface interface {
	Create(ctx context.Context, p *Post) error
	GetBySlug(ctx context.Context, slug string) (*Post, error)
	FindAll(ctx context.Context, page, perPage int) ([]*Post, error)
	Update(ctx context.Context, p *Post) error
	Count(ctx context.Context) (int64, error)
}

PostRepositoryInterface is the interface for the PostRepository.

type Subscriber

type Subscriber struct {
	ID          uuid.UUID `json:"id" gorm:"primaryKey;type:uuid"`
	Email       string    `json:"email" gorm:"uniqueIndex"`
	IsConfirmed bool      `json:"is_confirmed"`
	CreatedAt   time.Time `json:"created_at"`
}

func (*Subscriber) BeforeCreate

func (s *Subscriber) BeforeCreate(_ *gorm.DB) error

func (*Subscriber) Validate

func (s *Subscriber) Validate() error

type SubscriberRepositoryInterface

type SubscriberRepositoryInterface interface {
	Create(ctx context.Context, s *Subscriber) error
	Update(ctx context.Context, s *Subscriber) error
	GetByID(ctx context.Context, id string) (*Subscriber, error)
	GetConfirmed(ctx context.Context) ([]*Subscriber, error)
	Delete(ctx context.Context, id string) error
}

SubscriberRepositoryInterface is the interface for the subscriber repository.

type SubscribersRepository

type SubscribersRepository struct {
	// contains filtered or unexported fields
}

func NewSubscribersRepository

func NewSubscribersRepository(conn *gorm.DB) *SubscribersRepository

func (*SubscribersRepository) Create

func (*SubscribersRepository) Delete

func (db *SubscribersRepository) Delete(ctx context.Context, id string) error

func (*SubscribersRepository) GetByID

func (db *SubscribersRepository) GetByID(ctx context.Context, id string) (*Subscriber, error)

func (*SubscribersRepository) GetConfirmed

func (db *SubscribersRepository) GetConfirmed(ctx context.Context) ([]*Subscriber, error)

func (*SubscribersRepository) Update

type User

type User struct {
	ID         int        `json:"id" gorm:"primaryKey;autoIncrement"`
	ExternalID string     `json:"external_id" gorm:"uniqueIndex"` // ExternalID is the ID of the user in the AuthMethod
	Login      string     `json:"login"`
	AuthMethod AuthMethod `json:"auth_method"` // AuthMethod is the method of authentication used by the user
	Posts      []Post     `json:"posts" gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
	CreatedAt  time.Time  `json:"created_at"`
	UpdatedAt  time.Time  `json:"updated_at"`
}

User is the model for the user data.

func (*User) BeforeCreate

func (u *User) BeforeCreate(_ *gorm.DB) error

func (*User) Validate

func (u *User) Validate() error

type UserRepository

type UserRepository struct {
	// contains filtered or unexported fields
}

UserRepository is the database for the user data.

func NewUserRepository

func NewUserRepository(conn *gorm.DB) *UserRepository

NewUserRepository creates a new UserRepository.

func (*UserRepository) GetByExternalID

func (db *UserRepository) GetByExternalID(ctx context.Context, externalID string) (*User, error)

GetByExternalID returns the User data by the User.ExternalID.

func (*UserRepository) GetByID

func (db *UserRepository) GetByID(ctx context.Context, id int) (*User, error)

GetByID returns the User data by the User.ID.

func (*UserRepository) Upsert

func (db *UserRepository) Upsert(ctx context.Context, user *User) error

Upsert inserts or updates the User data.

type UserRepositoryInterface

type UserRepositoryInterface interface {
	Upsert(ctx context.Context, user *User) error
	GetByExternalID(ctx context.Context, externalID string) (*User, error)
	GetByID(ctx context.Context, id int) (*User, error)
}

UserRepositoryInterface is the interface for the UserRepository.

Jump to

Keyboard shortcuts

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