newsbox/models/message.go
Adriano Caloiaro 8ae801ae5f
Initial commit
2023-02-15 08:57:48 -08:00

67 lines
2.5 KiB
Go

package models
import (
"encoding/json"
"time"
"github.com/gobuffalo/pop/v6"
"github.com/gobuffalo/validate/v3"
"github.com/gobuffalo/validate/v3/validators"
"github.com/gofrs/uuid"
)
// Message is used by pop to map your messages database table to your go code.
type Message struct {
ID uuid.UUID `json:"id" db:"id"`
OwnerID uuid.UUID `json:"owner_id" db:"owner_id"`
From string `json:"from" db:"from" form:"sender"`
FromEmail string `json:"from_email" db:"from_email" form:"sender"`
To string `json:"to" db:"to" form:"recipient"`
Subject string `json:"subject" db:"subject" form:"subject"`
Body string `json:"body" db:"body" form:"body-plain"`
BodyHTML string `json:"body_html" db:"body_html" form:"body-html"`
Timestamp int64 `json:"timestamp" db:"timestamp" form:"timestamp"`
Token string `json:"-" db:"-" form:"token"`
Signature string `json:"-" db:"-" form:"signature"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}
// String is not required by pop and may be deleted
func (m Message) String() string {
jm, _ := json.Marshal(m)
return string(jm)
}
// Messages is not required by pop and may be deleted
type Messages []Message
// String is not required by pop and may be deleted
func (m Messages) String() string {
jm, _ := json.Marshal(m)
return string(jm)
}
// Validate gets run every time you call a "pop.Validate*" (pop.ValidateAndSave, pop.ValidateAndCreate, pop.ValidateAndUpdate) method.
// This method is not required and may be deleted.
func (m *Message) Validate(tx *pop.Connection) (*validate.Errors, error) {
return validate.Validate(
&validators.StringIsPresent{Field: m.From, Name: "From"},
&validators.StringIsPresent{Field: m.FromEmail, Name: "FromEmail"},
&validators.StringIsPresent{Field: m.Subject, Name: "Subject"},
&validators.StringIsPresent{Field: m.Body, Name: "Body"},
&validators.StringIsPresent{Field: m.BodyHTML, Name: "BodyHTML"},
), nil
}
// ValidateCreate gets run every time you call "pop.ValidateAndCreate" method.
// This method is not required and may be deleted.
func (m *Message) ValidateCreate(tx *pop.Connection) (*validate.Errors, error) {
return validate.NewErrors(), nil
}
// ValidateUpdate gets run every time you call "pop.ValidateAndUpdate" method.
// This method is not required and may be deleted.
func (m *Message) ValidateUpdate(tx *pop.Connection) (*validate.Errors, error) {
return validate.NewErrors(), nil
}