2021-03-18 18:52:41 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2021-03-21 11:03:35 +00:00
|
|
|
"fmt"
|
2021-03-19 17:24:02 +00:00
|
|
|
"net/http"
|
2021-03-21 11:03:35 +00:00
|
|
|
"strconv"
|
2021-03-19 17:24:02 +00:00
|
|
|
|
|
|
|
"github.com/manyminds/api2go"
|
2021-03-18 18:52:41 +00:00
|
|
|
"github.com/manyminds/api2go/jsonapi"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Car struct {
|
2021-03-21 11:03:35 +00:00
|
|
|
ID uint64 `json:"-"`
|
2021-03-20 20:40:18 +00:00
|
|
|
Brand string `json:"brand"`
|
|
|
|
Model string `json:"model"`
|
|
|
|
Price uint64 `json:"price"`
|
|
|
|
Status string `json:"status"` // OnTheWay, InStock, Sold, Discontinued
|
|
|
|
Mileage int64 `json:"mileage"` // I suppose it should be made unsigned, but that's what the task says ¯\_(ツ)_/¯
|
2021-03-19 17:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
2021-03-20 20:01:36 +00:00
|
|
|
"/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 {
|
2021-03-21 11:03:35 +00:00
|
|
|
return fmt.Sprintf("%d", 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 {
|
2021-03-21 11:25:23 +00:00
|
|
|
if id == "" {
|
|
|
|
c.ID = 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-21 11:03:35 +00:00
|
|
|
intID, err := strconv.ParseUint(id, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.ID = intID
|
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)
|
|
|
|
}
|