devrelcollective/xela

View on GitHub
models/link.go

Summary

Maintainability
A
40 mins
Test Coverage
package models

import (
    "encoding/json"
    "time"

    "github.com/gobuffalo/pop"
    "github.com/gobuffalo/pop/nulls"
    "github.com/gobuffalo/uuid"
    "github.com/gobuffalo/validate"
    "github.com/gobuffalo/validate/validators"
)

type Link struct {
    ID          uuid.UUID    `json:"id" db:"id"`
    CreatedAt   time.Time    `json:"created_at" db:"created_at"`
    UpdatedAt   time.Time    `json:"updated_at" db:"updated_at"`
    Name        string       `json:"name" db:"name"`
    Url         string       `json:"url" db:"url"`
    Description nulls.String `json:"description" db:"description"`
    UserID      uuid.UUID    `json:"user_id" db:"user_id"`
    UpdatedBy   uuid.UUID    `json:"updated_by" db:"updated_by"`
}

// String is not required by pop and may be deleted
func (l Link) String() string {
    jl, _ := json.Marshal(l)
    return string(jl)
}

// Links is not required by pop and may be deleted
type Links []Link

// String is not required by pop and may be deleted
func (l Links) String() string {
    jl, _ := json.Marshal(l)
    return string(jl)
}

// 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 (l *Link) Validate(tx *pop.Connection) (*validate.Errors, error) {
    return validate.Validate(
        &validators.StringIsPresent{Field: l.Name, Name: "Name"},
        &validators.StringIsPresent{Field: l.Url, Name: "Url"},
    ), nil
}

// ValidateCreate gets run every time you call "pop.ValidateAndCreate" method.
// This method is not required and may be deleted.
func (l *Link) 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 (l *Link) ValidateUpdate(tx *pop.Connection) (*validate.Errors, error) {
    return validate.NewErrors(), nil
}