data

package
v0.0.0-...-96d23e7 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMongoNotFound is returned when a given ID cannot be
	// found in the MongoDB collection.
	ErrMongoNotFound = errors.New("document not found")

	// ErrMongoNilColl is returned when a Nil collection is
	// given to NewMongo().
	ErrMongoNilColl = errors.New("collection cannot be nil")

	// ErrMongoFailCreate is returned when MongoDB fails to
	// create a new mailbox entry.
	ErrMongoFailCreate = errors.New("could not submit form, please try again later")

	// ErrMongoFailDelete is returned when MongoDB fails to
	// delete a mailbox entry.
	ErrMongoFailDelete = errors.New("could not delete form, please try again later")

	// ErrMongoInvalidID is returned when referencing an invalid
	// Mongo document ID.
	ErrMongoInvalidID = errors.New("invalid resource id")

	// ErrMongoInternal is returned when an internal database
	// error occurs.
	ErrMongoInternal = errors.New("internal server error")
)

Functions

This section is empty.

Types

type Data

type Data interface {

	// Count returns the number of entries in the mailbo.
	Count(ctx.Context) int64

	// ReadAll fetches batches of entries of arbitrary size.
	// See the implementation in mongo.go.
	ReadAll(ctx.Context, int64, int64) ([]Form, error)

	// Read fetches a single entry by referencing it's ID.
	Read(ctx.Context, string) (Form, error)

	// Create a new mailbox entry.
	Create(ctx.Context, Form) (string, error)

	// Delete a mailbox entry by referencing its ID.
	Delete(ctx.Context, string) error
}

Data defines the Mailbox database interface.

func NewMongo

func NewMongo(coll *mongodb.Collection) (Data, error)

NewMongo initializes a new Mongo Data instance.

type Form

type Form struct {
	ID      string `json:"id" bson:"_id,omitempty"`
	From    string `json:"from" bson:"from" binding:"required"`
	Subject string `json:"subject" bson:"subject" binding:"required"`
	Message string `json:"message" bson:"message" binding:"required"`
}

Form defines a single mailbox entry.

func (Form) Validate

func (f Form) Validate() error

Validate a form's 'From', 'Subject', and 'Message' fields. returns a human-friendly error message.

type FormWithCaptcha

type FormWithCaptcha struct {
	Form
	Captcha string `json:"captcha" binding:"required"`
}

FormWithCaptcha encapsulates a Form with a captcha token and RemoteIP attribute. It is used to validate Turnstile captchas.

type Mongo

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

Mongo implements the Data interface with a MongoDB backend.

func (*Mongo) Count

func (m *Mongo) Count(ctx context.Context) int64

Count the number of elements in the Mongo mailbox collection.

func (*Mongo) Create

func (m *Mongo) Create(ctx context.Context, f Form) (string, error)

Create a new mailbox entry with the given context and form.

func (*Mongo) Delete

func (m *Mongo) Delete(ctx context.Context, id string) error

Delete the mailbox entry with the given id.

func (*Mongo) Read

func (m *Mongo) Read(ctx context.Context, id string) (Form, error)

Read the mailbox entry with the given id.

func (*Mongo) ReadAll

func (m *Mongo) ReadAll(ctx context.Context, batch, page int64) ([]Form, error)

ReadAll returns paginated mailbox entries. It fetches up to 'batch' number of elements, after skipping the first (batch * page) elements.

Jump to

Keyboard shortcuts

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