diff --git a/README.md b/README.md index ae07cb4..a7d8b99 100644 --- a/README.md +++ b/README.md @@ -1 +1,20 @@ -# go-jsonapi-example \ No newline at end of file +# go-jsonapi-example + +`make && make run` + +## Create a new car: +`curl -X POST http://localhost:8080/v1/cars -d '{"data" : {"type" : "cars" , "attributes": {"brand" : "bmw", "model": "x5", "price": 999, "status": "OnTheWay"}}}'` + +## List cars: +`curl -X GET http://localhost:8080/v1/cars` + +## List paginated cars: +`curl -X GET 'http://localhost:8080/v1/cars?page\[offset\]=0&page\[limit\]=2'` +## OR +`curl -X GET 'http://localhost:8080/v1/cars?page\[number\]=1&page\[size\]=2'` + +## Update: +`curl -X PATCH http://localhost:8080/v1/cars/1 -d '{ "data" : {"type" : "cars", "id": "1", "attributes": {"model" : "x3"}}}'` + +## Delete: +`curl -X DELETE http://localhost:8080/v1/cars/2` diff --git a/internal/model/model_car.go b/internal/model/model_car.go index ed65cf8..3f90c87 100644 --- a/internal/model/model_car.go +++ b/internal/model/model_car.go @@ -42,7 +42,7 @@ func (c Car) Verify() (bool, api2go.HTTPError) { newErr := newVerifyError( "Invalid Attribute", "attribute must be one of: OnTheWay, InStock, Sold, Discontinued", - "/data/attributes/brand") + "/data/attributes/status") verifyErrors = append(verifyErrors, newErr) } diff --git a/internal/resource/resource_car.go b/internal/resource/resource_car.go index 193a1e7..b264c29 100644 --- a/internal/resource/resource_car.go +++ b/internal/resource/resource_car.go @@ -19,8 +19,8 @@ type CarResource struct { // FindAll to satisfy api2go data source interface func (s CarResource) FindAll(r api2go.Request) (api2go.Responder, error) { - var result []model.Car cars := s.CarStorage.GetAll() + result := make([]model.Car, 0, len(cars)) for _, car := range cars { result = append(result, *car) @@ -106,7 +106,6 @@ func (s CarResource) PaginatedFindAll(r api2go.Request) (uint, api2go.Responder, } // FindOne to satisfy `api2go.DataSource` interface -// this method should return the car with the given ID, otherwise an error func (s CarResource) FindOne(ID string, r api2go.Request) (api2go.Responder, error) { car, err := s.CarStorage.GetOne(ID) if err != nil {