go-jsonapi-example/internal/model/model_car.go

110 lines
2.9 KiB
Go
Raw Normal View History

2021-03-18 18:52:41 +00:00
package model
import (
"errors"
2021-03-19 17:24:02 +00:00
"net/http"
"github.com/manyminds/api2go"
2021-03-18 18:52:41 +00:00
"github.com/manyminds/api2go/jsonapi"
)
type Car struct {
ID string `json:"-"`
Brand string `json:"brand"`
Model string `json:"model"`
2021-03-19 17:24:02 +00:00
Price uint64 `json:"price"`
Status string `json:"status"` // OnTheWay, InStock, Sold, Discontinued
}
func (c Car) Verify() (bool, api2go.HTTPError) {
var verifyErrors []api2go.Error
var httpError api2go.HTTPError
if c.Brand == "" {
newErr := newVerifyError(
"Invalid Attribute",
"attribute cannot be empty",
"/data/attributes/brand")
verifyErrors = append(verifyErrors, newErr)
}
if c.Model == "" {
newErr := newVerifyError(
"Invalid Attribute",
"attribute cannot be empty",
"/data/attributes/model")
verifyErrors = append(verifyErrors, newErr)
}
if c.Status != "OnTheWay" &&
c.Status != "InStock" &&
c.Status != "Sold" &&
c.Status != "Discontinued" {
newErr := newVerifyError(
"Invalid Attribute",
"attribute must be one of: OnTheWay, InStock, Sold, Discontinued",
"/data/attributes/status")
2021-03-19 17:24:02 +00:00
verifyErrors = append(verifyErrors, newErr)
}
ok := len(verifyErrors) == 0
if !ok {
2021-03-20 17:51:04 +00:00
httpError = api2go.NewHTTPError(
2021-03-19 17:24:02 +00:00
errors.New("Invalid content"),
"Invalid content",
http.StatusBadRequest)
httpError.Errors = verifyErrors
}
return ok, httpError
}
func newVerifyError(title string, detail string, pointer string) api2go.Error {
var newError api2go.Error
newError.Title = title
newError.Detail = detail
newError.Source = &api2go.ErrorSource{Pointer: pointer}
return newError
2021-03-18 18:52:41 +00:00
}
// GetID to satisfy jsonapi.MarshalIdentifier interface
2021-03-19 17:24:02 +00:00
func (c Car) GetID() string {
return c.ID
2021-03-18 18:52:41 +00:00
}
// SetID to satisfy jsonapi.UnmarshalIdentifier interface
2021-03-19 17:24:02 +00:00
func (c *Car) SetID(id string) error {
c.ID = id
2021-03-18 18:52:41 +00:00
return nil
}
// GetReferences to satisfy the jsonapi.MarshalReferences interface
2021-03-19 17:24:02 +00:00
func (c Car) GetReferences() []jsonapi.Reference {
2021-03-18 18:52:41 +00:00
return []jsonapi.Reference{}
}
// GetReferencedIDs to satisfy the jsonapi.MarshalLinkedRelations interface
2021-03-19 17:24:02 +00:00
func (c Car) GetReferencedIDs() []jsonapi.ReferenceID {
2021-03-18 18:52:41 +00:00
return []jsonapi.ReferenceID{}
}
// GetReferencedStructs to satisfy the jsonapi.MarhsalIncludedRelations interface
2021-03-19 17:24:02 +00:00
func (c Car) GetReferencedStructs() []jsonapi.MarshalIdentifier {
2021-03-18 18:52:41 +00:00
return []jsonapi.MarshalIdentifier{}
}
// SetToManyReferenceIDs sets the sweets reference IDs and satisfies the jsonapi.UnmarshalToManyRelations interface
2021-03-19 17:24:02 +00:00
func (c *Car) SetToManyReferenceIDs(name string, IDs []string) error {
2021-03-18 18:52:41 +00:00
return errors.New("There is no to-many relationship with the name " + name)
}
// AddToManyIDs adds some new sweets that a users loves so much
2021-03-19 17:24:02 +00:00
func (c *Car) AddToManyIDs(name string, IDs []string) error {
2021-03-18 18:52:41 +00:00
return errors.New("There is no to-many relationship with the name " + name)
}
// DeleteToManyIDs removes some sweets from a users because they made him very sick
2021-03-19 17:24:02 +00:00
func (c *Car) DeleteToManyIDs(name string, IDs []string) error {
2021-03-18 18:52:41 +00:00
return errors.New("There is no to-many relationship with the name " + name)
}