not tested
This commit is contained in:
@ -3,6 +3,9 @@ package model
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/manyminds/api2go"
|
||||
"github.com/manyminds/api2go/jsonapi"
|
||||
)
|
||||
|
||||
@ -10,47 +13,97 @@ type Car struct {
|
||||
ID string `json:"-"`
|
||||
Brand string `json:"brand"`
|
||||
Model string `json:"model"`
|
||||
Price uint `json:"price"`
|
||||
Status string `json:"status"`
|
||||
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/brand")
|
||||
verifyErrors = append(verifyErrors, newErr)
|
||||
}
|
||||
|
||||
ok := len(verifyErrors) == 0
|
||||
|
||||
if !ok {
|
||||
httpError := api2go.NewHTTPError(
|
||||
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
|
||||
}
|
||||
|
||||
// GetID to satisfy jsonapi.MarshalIdentifier interface
|
||||
func (u Car) GetID() string {
|
||||
return u.ID
|
||||
func (c Car) GetID() string {
|
||||
return c.ID
|
||||
}
|
||||
|
||||
// SetID to satisfy jsonapi.UnmarshalIdentifier interface
|
||||
func (u *Car) SetID(id string) error {
|
||||
u.ID = id
|
||||
func (c *Car) SetID(id string) error {
|
||||
c.ID = id
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetReferences to satisfy the jsonapi.MarshalReferences interface
|
||||
func (u Car) GetReferences() []jsonapi.Reference {
|
||||
func (c Car) GetReferences() []jsonapi.Reference {
|
||||
return []jsonapi.Reference{}
|
||||
}
|
||||
|
||||
// GetReferencedIDs to satisfy the jsonapi.MarshalLinkedRelations interface
|
||||
func (u Car) GetReferencedIDs() []jsonapi.ReferenceID {
|
||||
func (c Car) GetReferencedIDs() []jsonapi.ReferenceID {
|
||||
return []jsonapi.ReferenceID{}
|
||||
}
|
||||
|
||||
// GetReferencedStructs to satisfy the jsonapi.MarhsalIncludedRelations interface
|
||||
func (u Car) GetReferencedStructs() []jsonapi.MarshalIdentifier {
|
||||
func (c Car) GetReferencedStructs() []jsonapi.MarshalIdentifier {
|
||||
return []jsonapi.MarshalIdentifier{}
|
||||
}
|
||||
|
||||
// SetToManyReferenceIDs sets the sweets reference IDs and satisfies the jsonapi.UnmarshalToManyRelations interface
|
||||
func (u *Car) SetToManyReferenceIDs(name string, IDs []string) error {
|
||||
func (c *Car) SetToManyReferenceIDs(name string, IDs []string) error {
|
||||
return errors.New("There is no to-many relationship with the name " + name)
|
||||
}
|
||||
|
||||
// AddToManyIDs adds some new sweets that a users loves so much
|
||||
func (u *Car) AddToManyIDs(name string, IDs []string) error {
|
||||
func (c *Car) AddToManyIDs(name string, IDs []string) error {
|
||||
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
|
||||
func (u *Car) DeleteToManyIDs(name string, IDs []string) error {
|
||||
func (c *Car) DeleteToManyIDs(name string, IDs []string) error {
|
||||
return errors.New("There is no to-many relationship with the name " + name)
|
||||
}
|
||||
|
@ -122,6 +122,10 @@ func (s CarResource) Create(obj interface{}, r api2go.Request) (api2go.Responder
|
||||
return &Response{}, api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if ok, httpErr := car.Verify(); !ok {
|
||||
return &Response{}, httpErr
|
||||
}
|
||||
|
||||
id := s.CarStorage.Insert(car)
|
||||
car.ID = id
|
||||
|
||||
@ -141,6 +145,10 @@ func (s CarResource) Update(obj interface{}, r api2go.Request) (api2go.Responder
|
||||
return &Response{}, api2go.NewHTTPError(errors.New("Invalid instance given"), "Invalid instance given", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if ok, httpErr := car.Verify(); !ok {
|
||||
return &Response{}, httpErr
|
||||
}
|
||||
|
||||
err := s.CarStorage.Update(car)
|
||||
return &Response{Res: car, Code: http.StatusNoContent}, err
|
||||
return &Response{Code: http.StatusOK}, err
|
||||
}
|
||||
|
@ -8,9 +8,7 @@ type Response struct {
|
||||
|
||||
func (r Response) Metadata() map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"author": "GenZmeY",
|
||||
"license": "wtfpl",
|
||||
"license-url": "http://www.wtfpl.net",
|
||||
"author": "GenZmeY",
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user