From d03abe0837ea43f2e6fc44601eb9a89a8b7ddf4a Mon Sep 17 00:00:00 2001 From: GenZmeY Date: Sun, 21 Mar 2021 14:25:23 +0300 Subject: [PATCH] + paginated find --- internal/model/model_car.go | 5 +++++ internal/resource/resource_car.go | 23 ++++++----------------- internal/storage/storage_car.go | 6 +++--- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/internal/model/model_car.go b/internal/model/model_car.go index 9449792..e4a2f20 100644 --- a/internal/model/model_car.go +++ b/internal/model/model_car.go @@ -77,6 +77,11 @@ func (c Car) GetID() string { // SetID to satisfy jsonapi.UnmarshalIdentifier interface func (c *Car) SetID(id string) error { + if id == "" { + c.ID = 0 + return nil + } + intID, err := strconv.ParseUint(id, 10, 64) if err != nil { return err diff --git a/internal/resource/resource_car.go b/internal/resource/resource_car.go index 3f6ab08..6dfc860 100644 --- a/internal/resource/resource_car.go +++ b/internal/resource/resource_car.go @@ -3,7 +3,7 @@ package resource import ( "errors" "net/http" - _ "sort" + "sort" "strconv" "go-jsonapi-example/internal/model" @@ -23,24 +23,13 @@ func (s CarResource) FindAll(r api2go.Request) (api2go.Responder, error) { } // PaginatedFindAll can be used to load cars in chunks -/* func (s CarResource) PaginatedFindAll(r api2go.Request) (uint, api2go.Responder, error) { var ( - result []model.Car + result storage.Cars number, size, offset, limit string - keys []int ) cars := s.CarStorage.GetAll() - - for k := range cars { - i, err := strconv.ParseInt(k, 10, 64) - if err != nil { - return 0, &Response{}, err - } - - keys = append(keys, int(i)) - } - sort.Ints(keys) + sort.Sort(cars) numberQuery, ok := r.QueryParams["page[number]"] if ok { @@ -75,7 +64,7 @@ func (s CarResource) PaginatedFindAll(r api2go.Request) (uint, api2go.Responder, if i >= uint64(len(cars)) { break } - result = append(result, *cars[strconv.FormatInt(int64(keys[i]), 10)]) + result = append(result, cars[i]) } } else { limitI, err := strconv.ParseUint(limit, 10, 64) @@ -92,13 +81,13 @@ func (s CarResource) PaginatedFindAll(r api2go.Request) (uint, api2go.Responder, if i >= uint64(len(cars)) { break } - result = append(result, *cars[strconv.FormatInt(int64(keys[i]), 10)]) + result = append(result, cars[i]) } } return uint(len(cars)), &Response{Res: result}, nil } -*/ + // FindOne to satisfy `api2go.DataSource` interface func (s CarResource) FindOne(ID string, r api2go.Request) (api2go.Responder, error) { intID, err := strconv.ParseUint(ID, 10, 64) diff --git a/internal/storage/storage_car.go b/internal/storage/storage_car.go index f6264d4..b1f0c4d 100644 --- a/internal/storage/storage_car.go +++ b/internal/storage/storage_car.go @@ -45,7 +45,7 @@ func (s CarStorage) GetOne(id uint64) (model.Car, error) { return *car, nil } s.mutex.RUnlock() - errMessage := fmt.Sprintf("Car for id %s not found", id) + errMessage := fmt.Sprintf("Car for id %d not found", id) return model.Car{}, api2go.NewHTTPError(errors.New(errMessage), errMessage, http.StatusNotFound) } @@ -63,7 +63,7 @@ func (s *CarStorage) Delete(id uint64) error { defer s.mutex.Unlock() _, exists := s.cars[id] if !exists { - return fmt.Errorf("Car with id %s does not exist", id) + return fmt.Errorf("Car with id %d does not exist", id) } delete(s.cars, id) @@ -75,7 +75,7 @@ func (s *CarStorage) Update(c model.Car) error { defer s.mutex.Unlock() _, exists := s.cars[c.ID] if !exists { - return fmt.Errorf("Car with id %s does not exist", c.ID) + return fmt.Errorf("Car with id %d does not exist", c.ID) } s.cars[c.ID] = &c