+ paginated find

This commit is contained in:
GenZmeY 2021-03-21 14:25:23 +03:00
parent e90f4a1291
commit d03abe0837
3 changed files with 14 additions and 20 deletions

View File

@ -77,6 +77,11 @@ func (c Car) GetID() string {
// SetID to satisfy jsonapi.UnmarshalIdentifier interface // SetID to satisfy jsonapi.UnmarshalIdentifier interface
func (c *Car) SetID(id string) error { func (c *Car) SetID(id string) error {
if id == "" {
c.ID = 0
return nil
}
intID, err := strconv.ParseUint(id, 10, 64) intID, err := strconv.ParseUint(id, 10, 64)
if err != nil { if err != nil {
return err return err

View File

@ -3,7 +3,7 @@ package resource
import ( import (
"errors" "errors"
"net/http" "net/http"
_ "sort" "sort"
"strconv" "strconv"
"go-jsonapi-example/internal/model" "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 // PaginatedFindAll can be used to load cars in chunks
/*
func (s CarResource) PaginatedFindAll(r api2go.Request) (uint, api2go.Responder, error) { func (s CarResource) PaginatedFindAll(r api2go.Request) (uint, api2go.Responder, error) {
var ( var (
result []model.Car result storage.Cars
number, size, offset, limit string number, size, offset, limit string
keys []int
) )
cars := s.CarStorage.GetAll() cars := s.CarStorage.GetAll()
sort.Sort(cars)
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)
numberQuery, ok := r.QueryParams["page[number]"] numberQuery, ok := r.QueryParams["page[number]"]
if ok { if ok {
@ -75,7 +64,7 @@ func (s CarResource) PaginatedFindAll(r api2go.Request) (uint, api2go.Responder,
if i >= uint64(len(cars)) { if i >= uint64(len(cars)) {
break break
} }
result = append(result, *cars[strconv.FormatInt(int64(keys[i]), 10)]) result = append(result, cars[i])
} }
} else { } else {
limitI, err := strconv.ParseUint(limit, 10, 64) 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)) { if i >= uint64(len(cars)) {
break break
} }
result = append(result, *cars[strconv.FormatInt(int64(keys[i]), 10)]) result = append(result, cars[i])
} }
} }
return uint(len(cars)), &Response{Res: result}, nil return uint(len(cars)), &Response{Res: result}, nil
} }
*/
// FindOne to satisfy `api2go.DataSource` interface // FindOne to satisfy `api2go.DataSource` interface
func (s CarResource) FindOne(ID string, r api2go.Request) (api2go.Responder, error) { func (s CarResource) FindOne(ID string, r api2go.Request) (api2go.Responder, error) {
intID, err := strconv.ParseUint(ID, 10, 64) intID, err := strconv.ParseUint(ID, 10, 64)

View File

@ -45,7 +45,7 @@ func (s CarStorage) GetOne(id uint64) (model.Car, error) {
return *car, nil return *car, nil
} }
s.mutex.RUnlock() 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) 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() defer s.mutex.Unlock()
_, exists := s.cars[id] _, exists := s.cars[id]
if !exists { 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) delete(s.cars, id)
@ -75,7 +75,7 @@ func (s *CarStorage) Update(c model.Car) error {
defer s.mutex.Unlock() defer s.mutex.Unlock()
_, exists := s.cars[c.ID] _, exists := s.cars[c.ID]
if !exists { 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 s.cars[c.ID] = &c