db

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

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

Go to latest
Published: Jul 20, 2024 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ForeignKeyViolation = "23503"
	UniqueViolation     = "23505"
)

Constants for PostgreSQL error codes ForeignKeyViolation represents a foreign key violation error UniqueViolation represents a unique constraint violation error

Variables

View Source
var ErrRecordNotFound = pgx.ErrNoRows

ErrRecordNotFound is returned when a query returns no rows

View Source
var ErrUniqueViolation = &pgconn.PgError{
	Code: UniqueViolation,
}

ErrUniqueViolation is returned when a unique constraint is violated

Functions

func ErrorCode

func ErrorCode(err error) string

ErrorCode extracts the error code from a pgconn.PgError Function that returns the error code based on the error or nothing to use default error in code

Types

type Blog

type Blog struct {
	ID          uuid.UUID `json:"id"`
	Title       string    `json:"title"`
	Slug        string    `json:"slug"`
	Description string    `json:"description"`
	Body        string    `json:"body"`
	BannerImage string    `json:"banner_image"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
	AuthorID    uuid.UUID `json:"author_id"`
}

type Comment

type Comment struct {
	ID        uuid.UUID `json:"id"`
	BlogID    uuid.UUID `json:"blog_id"`
	Body      string    `json:"body"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
	UserID    uuid.UUID `json:"user_id"`
}

type CreateBlogParams

type CreateBlogParams struct {
	AuthorID    uuid.UUID `json:"author_id"`
	Title       string    `json:"title"`
	Slug        string    `json:"slug"`
	Description string    `json:"description"`
	Body        string    `json:"body"`
	BannerImage string    `json:"banner_image"`
}

type CreateCommentParams

type CreateCommentParams struct {
	BlogID uuid.UUID `json:"blog_id"`
	UserID uuid.UUID `json:"user_id"`
	Body   string    `json:"body"`
}

type CreateFavoriteParams

type CreateFavoriteParams struct {
	BlogID uuid.UUID `json:"blog_id"`
	UserID uuid.UUID `json:"user_id"`
}

type CreateSessionParams

type CreateSessionParams struct {
	ID           uuid.UUID `json:"id"`
	UserID       uuid.UUID `json:"user_id"`
	RefreshToken string    `json:"refresh_token"`
	UserAgent    string    `json:"user_agent"`
	ClientIp     string    `json:"client_ip"`
	IsBlocked    bool      `json:"is_blocked"`
	ExpiresAt    time.Time `json:"expires_at"`
}

type CreateUserParams

type CreateUserParams struct {
	Username       string `json:"username"`
	HashedPassword string `json:"hashed_password"`
	FullName       string `json:"full_name"`
	Email          string `json:"email"`
}

type CreateUserTxParams

type CreateUserTxParams struct {
	CreateUserParams
	AfterCreate func(user User) error
}

type CreateUserTxResult

type CreateUserTxResult struct {
	User User
}

type CreateVerifyEmailParams

type CreateVerifyEmailParams struct {
	UserID     uuid.UUID `json:"user_id"`
	Email      string    `json:"email"`
	SecretCode string    `json:"secret_code"`
}

type DBTX

type DBTX interface {
	Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
	Query(context.Context, string, ...interface{}) (pgx.Rows, error)
	QueryRow(context.Context, string, ...interface{}) pgx.Row
}

type DeleteBlogTxParams

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

DeleteBlogTxParams contains the input parameters of the delete blog transaction

type DeleteBlogTxResult

type DeleteBlogTxResult struct {
	DeletedBlogID         uuid.UUID `json:"deleted_blog_id"`
	DeletedCommentsCount  int64     `json:"deleted_comments_count"`
	DeletedFavoritesCount int64     `json:"deleted_favorites_count"`
}

DeleteBlogTxResult is the result of the delete blog transaction

type DeleteCommentByBlogIDParams

type DeleteCommentByBlogIDParams struct {
	ID     uuid.UUID `json:"id"`
	BlogID uuid.UUID `json:"blog_id"`
}

type Favorite

type Favorite struct {
	ID        uuid.UUID `json:"id"`
	BlogID    uuid.UUID `json:"blog_id"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
	UserID    uuid.UUID `json:"user_id"`
}

type ListBlogsParams

type ListBlogsParams struct {
	Limit  int32 `json:"limit"`
	Offset int32 `json:"offset"`
}

type ListCommentsByBlogParams

type ListCommentsByBlogParams struct {
	BlogID uuid.UUID `json:"blog_id"`
	Limit  int32     `json:"limit"`
	Offset int32     `json:"offset"`
}

type ListFavoritesByBlogParams

type ListFavoritesByBlogParams struct {
	BlogID uuid.UUID `json:"blog_id"`
	Limit  int32     `json:"limit"`
	Offset int32     `json:"offset"`
}

type Querier

type Querier interface {
	CreateBlog(ctx context.Context, arg CreateBlogParams) (Blog, error)
	CreateComment(ctx context.Context, arg CreateCommentParams) (Comment, error)
	CreateFavorite(ctx context.Context, arg CreateFavoriteParams) (Favorite, error)
	CreateSession(ctx context.Context, arg CreateSessionParams) (Session, error)
	CreateUser(ctx context.Context, arg CreateUserParams) (User, error)
	CreateVerifyEmail(ctx context.Context, arg CreateVerifyEmailParams) (VerifyEmail, error)
	DeleteBlog(ctx context.Context, id uuid.UUID) error
	DeleteComment(ctx context.Context, id uuid.UUID) error
	DeleteCommentByBlogID(ctx context.Context, arg DeleteCommentByBlogIDParams) error
	DeleteCommentsByBlog(ctx context.Context, blogID uuid.UUID) (pgconn.CommandTag, error)
	DeleteFavorite(ctx context.Context, id uuid.UUID) error
	DeleteFavoritesByBlog(ctx context.Context, blogID uuid.UUID) (pgconn.CommandTag, error)
	GetBlog(ctx context.Context, id uuid.UUID) (Blog, error)
	GetBlogBySlug(ctx context.Context, slug string) (Blog, error)
	GetComment(ctx context.Context, id uuid.UUID) (Comment, error)
	GetFavorite(ctx context.Context, id uuid.UUID) (Favorite, error)
	GetSession(ctx context.Context, id uuid.UUID) (Session, error)
	GetUser(ctx context.Context, id uuid.UUID) (User, error)
	GetUserByUsername(ctx context.Context, username string) (User, error)
	ListBlogs(ctx context.Context, arg ListBlogsParams) ([]Blog, error)
	ListCommentsByBlog(ctx context.Context, arg ListCommentsByBlogParams) ([]Comment, error)
	ListFavoritesByBlog(ctx context.Context, arg ListFavoritesByBlogParams) ([]Favorite, error)
	UpdateBlog(ctx context.Context, arg UpdateBlogParams) (Blog, error)
	UpdateComment(ctx context.Context, arg UpdateCommentParams) (Comment, error)
	UpdateCommentByBlogID(ctx context.Context, arg UpdateCommentByBlogIDParams) (Comment, error)
	UpdateUser(ctx context.Context, arg UpdateUserParams) (User, error)
	UpdateVerifyEmail(ctx context.Context, arg UpdateVerifyEmailParams) (VerifyEmail, error)
}

type Queries

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

func New

func New(db DBTX) *Queries

func (*Queries) CreateBlog

func (q *Queries) CreateBlog(ctx context.Context, arg CreateBlogParams) (Blog, error)

func (*Queries) CreateComment

func (q *Queries) CreateComment(ctx context.Context, arg CreateCommentParams) (Comment, error)

func (*Queries) CreateFavorite

func (q *Queries) CreateFavorite(ctx context.Context, arg CreateFavoriteParams) (Favorite, error)

func (*Queries) CreateSession

func (q *Queries) CreateSession(ctx context.Context, arg CreateSessionParams) (Session, error)

func (*Queries) CreateUser

func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error)

func (*Queries) CreateVerifyEmail

func (q *Queries) CreateVerifyEmail(ctx context.Context, arg CreateVerifyEmailParams) (VerifyEmail, error)

func (*Queries) DeleteBlog

func (q *Queries) DeleteBlog(ctx context.Context, id uuid.UUID) error

func (*Queries) DeleteComment

func (q *Queries) DeleteComment(ctx context.Context, id uuid.UUID) error

func (*Queries) DeleteCommentByBlogID

func (q *Queries) DeleteCommentByBlogID(ctx context.Context, arg DeleteCommentByBlogIDParams) error

func (*Queries) DeleteCommentsByBlog

func (q *Queries) DeleteCommentsByBlog(ctx context.Context, blogID uuid.UUID) (pgconn.CommandTag, error)

func (*Queries) DeleteFavorite

func (q *Queries) DeleteFavorite(ctx context.Context, id uuid.UUID) error

func (*Queries) DeleteFavoritesByBlog

func (q *Queries) DeleteFavoritesByBlog(ctx context.Context, blogID uuid.UUID) (pgconn.CommandTag, error)

func (*Queries) GetBlog

func (q *Queries) GetBlog(ctx context.Context, id uuid.UUID) (Blog, error)

func (*Queries) GetBlogBySlug

func (q *Queries) GetBlogBySlug(ctx context.Context, slug string) (Blog, error)

func (*Queries) GetComment

func (q *Queries) GetComment(ctx context.Context, id uuid.UUID) (Comment, error)

func (*Queries) GetFavorite

func (q *Queries) GetFavorite(ctx context.Context, id uuid.UUID) (Favorite, error)

func (*Queries) GetSession

func (q *Queries) GetSession(ctx context.Context, id uuid.UUID) (Session, error)

func (*Queries) GetUser

func (q *Queries) GetUser(ctx context.Context, id uuid.UUID) (User, error)

func (*Queries) GetUserByUsername

func (q *Queries) GetUserByUsername(ctx context.Context, username string) (User, error)

func (*Queries) ListBlogs

func (q *Queries) ListBlogs(ctx context.Context, arg ListBlogsParams) ([]Blog, error)

func (*Queries) ListCommentsByBlog

func (q *Queries) ListCommentsByBlog(ctx context.Context, arg ListCommentsByBlogParams) ([]Comment, error)

func (*Queries) ListFavoritesByBlog

func (q *Queries) ListFavoritesByBlog(ctx context.Context, arg ListFavoritesByBlogParams) ([]Favorite, error)

func (*Queries) UpdateBlog

func (q *Queries) UpdateBlog(ctx context.Context, arg UpdateBlogParams) (Blog, error)

func (*Queries) UpdateComment

func (q *Queries) UpdateComment(ctx context.Context, arg UpdateCommentParams) (Comment, error)

func (*Queries) UpdateCommentByBlogID

func (q *Queries) UpdateCommentByBlogID(ctx context.Context, arg UpdateCommentByBlogIDParams) (Comment, error)

func (*Queries) UpdateUser

func (q *Queries) UpdateUser(ctx context.Context, arg UpdateUserParams) (User, error)

func (*Queries) UpdateVerifyEmail

func (q *Queries) UpdateVerifyEmail(ctx context.Context, arg UpdateVerifyEmailParams) (VerifyEmail, error)

func (*Queries) WithTx

func (q *Queries) WithTx(tx pgx.Tx) *Queries

type SQLStore

type SQLStore struct {
	*Queries
	// contains filtered or unexported fields
}

provides functionality for executing all SQL queries and transactions

func (*SQLStore) CreateUserTx

func (store *SQLStore) CreateUserTx(ctx context.Context, arg CreateUserTxParams) (CreateUserTxResult, error)

func (*SQLStore) DeleteBlogTx

func (store *SQLStore) DeleteBlogTx(ctx context.Context, arg DeleteBlogTxParams) (DeleteBlogTxResult, error)

DeleteBlogTx performs a deletion of a blog and all its associated data. It deletes the blog, its comments, and its favorites within a database transaction

func (*SQLStore) VerifyEmailTx

func (store *SQLStore) VerifyEmailTx(ctx context.Context, arg VerifyEmailTxParams) (VerifyEmailTxResult, error)

type Session

type Session struct {
	ID           uuid.UUID `json:"id"`
	UserID       uuid.UUID `json:"user_id"`
	RefreshToken string    `json:"refresh_token"`
	UserAgent    string    `json:"user_agent"`
	ClientIp     string    `json:"client_ip"`
	IsBlocked    bool      `json:"is_blocked"`
	ExpiresAt    time.Time `json:"expires_at"`
	CreatedAt    time.Time `json:"created_at"`
}

type Store

type Store interface {
	Querier
	DeleteBlogTx(ctx context.Context, arg DeleteBlogTxParams) (DeleteBlogTxResult, error)
	CreateUserTx(ctx context.Context, arg CreateUserTxParams) (CreateUserTxResult, error)
	VerifyEmailTx(ctx context.Context, arg VerifyEmailTxParams) (VerifyEmailTxResult, error)
}

store defines all the functions related to execute db queries and also contains code for transactions in DB

func NewStore

func NewStore(connPool *pgxpool.Pool) Store

creating a NewStore

type UpdateBlogParams

type UpdateBlogParams struct {
	Title       pgtype.Text `json:"title"`
	Slug        pgtype.Text `json:"slug"`
	Description pgtype.Text `json:"description"`
	Body        pgtype.Text `json:"body"`
	BannerImage pgtype.Text `json:"banner_image"`
	ID          uuid.UUID   `json:"id"`
}

type UpdateCommentByBlogIDParams

type UpdateCommentByBlogIDParams struct {
	ID     uuid.UUID `json:"id"`
	BlogID uuid.UUID `json:"blog_id"`
	Body   string    `json:"body"`
}

type UpdateCommentParams

type UpdateCommentParams struct {
	ID   uuid.UUID `json:"id"`
	Body string    `json:"body"`
}

type UpdateUserParams

type UpdateUserParams struct {
	Username        pgtype.Text `json:"username"`
	FullName        pgtype.Text `json:"full_name"`
	Email           pgtype.Text `json:"email"`
	IsEmailVerified pgtype.Bool `json:"is_email_verified"`
	HashedPassword  pgtype.Text `json:"hashed_password"`
	ID              uuid.UUID   `json:"id"`
}

type UpdateVerifyEmailParams

type UpdateVerifyEmailParams struct {
	ID         uuid.UUID `json:"id"`
	SecretCode string    `json:"secret_code"`
}

type User

type User struct {
	ID                uuid.UUID `json:"id"`
	Username          string    `json:"username"`
	HashedPassword    string    `json:"hashed_password"`
	FullName          string    `json:"full_name"`
	Email             string    `json:"email"`
	IsEmailVerified   bool      `json:"is_email_verified"`
	PasswordChangedAt time.Time `json:"password_changed_at"`
	CreatedAt         time.Time `json:"created_at"`
	UpdatedAt         time.Time `json:"updated_at"`
}

type VerifyEmail

type VerifyEmail struct {
	ID         uuid.UUID `json:"id"`
	UserID     uuid.UUID `json:"user_id"`
	Email      string    `json:"email"`
	SecretCode string    `json:"secret_code"`
	IsUsed     bool      `json:"is_used"`
	CreatedAt  time.Time `json:"created_at"`
	ExpiresAt  time.Time `json:"expires_at"`
}

type VerifyEmailTxParams

type VerifyEmailTxParams struct {
	EmailId    uuid.UUID
	SecretCode string
}

type VerifyEmailTxResult

type VerifyEmailTxResult struct {
	User        User
	VerifyEmail VerifyEmail
}

Jump to

Keyboard shortcuts

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