github.com/tsmith1024/pop@v4.12.2+incompatible/genny/model/_fixtures/models/widget_jsonapi.go (about)

     1  package models
     2  
     3  import (
     4  	"strings"
     5  	"time"
     6  
     7  	"github.com/gobuffalo/nulls"
     8  	"github.com/gobuffalo/pop"
     9  	"github.com/gobuffalo/validate"
    10  	"github.com/gobuffalo/validate/validators"
    11  	"github.com/gofrs/uuid"
    12  	"github.com/google/jsonapi"
    13  )
    14  
    15  type Widget struct {
    16  	ID          uuid.UUID    `jsonapi:"primary,id" db:"id"`
    17  	CreatedAt   time.Time    `jsonapi:"attr,created_at" db:"created_at"`
    18  	UpdatedAt   time.Time    `jsonapi:"attr,updated_at" db:"updated_at"`
    19  	Name        string       `jsonapi:"attr,name" db:"name"`
    20  	Description string       `jsonapi:"attr,description" db:"description"`
    21  	Age         int          `jsonapi:"attr,age" db:"age"`
    22  	Bar         nulls.String `jsonapi:"attr,bar" db:"bar"`
    23  }
    24  
    25  // String is not required by pop and may be deleted
    26  func (w Widget) String() string {
    27  	var jb strings.Builder
    28  	_ = jsonapi.MarshalPayload(&jb, &w)
    29  	return jb.String()
    30  }
    31  
    32  // Widgets is not required by pop and may be deleted
    33  type Widgets []Widget
    34  
    35  // String is not required by pop and may be deleted
    36  func (w Widgets) String() string {
    37  	var jb strings.Builder
    38  	_ = jsonapi.MarshalPayload(&jb, &w)
    39  	return jb.String()
    40  }
    41  
    42  // Validate gets run every time you call a "pop.Validate*" (pop.ValidateAndSave, pop.ValidateAndCreate, pop.ValidateAndUpdate) method.
    43  // This method is not required and may be deleted.
    44  func (w *Widget) Validate(tx *pop.Connection) (*validate.Errors, error) {
    45  	return validate.Validate(
    46  		&validators.StringIsPresent{Field: w.Name, Name: "Name"},
    47  		&validators.StringIsPresent{Field: w.Description, Name: "Description"},
    48  		&validators.IntIsPresent{Field: w.Age, Name: "Age"},
    49  	), nil
    50  }
    51  
    52  // ValidateCreate gets run every time you call "pop.ValidateAndCreate" method.
    53  // This method is not required and may be deleted.
    54  func (w *Widget) ValidateCreate(tx *pop.Connection) (*validate.Errors, error) {
    55  	return validate.NewErrors(), nil
    56  }
    57  
    58  // ValidateUpdate gets run every time you call "pop.ValidateAndUpdate" method.
    59  // This method is not required and may be deleted.
    60  func (w *Widget) ValidateUpdate(tx *pop.Connection) (*validate.Errors, error) {
    61  	return validate.NewErrors(), nil
    62  }