xiaohutai / jsonapi

[Bolt Extension] JSON API for Bolt CMS
https://market.bolt.cm/view/bolt/jsonapi
MIT License
55 stars 18 forks source link

Incorrect `prev` returned when querying the latest record #96

Open zomars opened 5 years ago

zomars commented 5 years ago

I have 4 entries with their respective ids [1, 2, 3, 4]. When the latest one is being requested (entries/4), it should return id 3, but instead it returns the first one:

{
    "links": {
        "self": "http://localhost:3000/json/entries/4",
        "prev": "http://localhost:3000/json/entries/1"
    },
}
Raistlfiren commented 5 years ago

More details...

Dates of each of the entries have the following values for date published (Courtesy of zomars)- Id 4has "datepublish": "2019-03-06T18:06:04+00:00" Id 3 has "datepublish": "2019-03-06T17:57:00+00:00" id 1 has "datepublish": "2019-03-06T17:53:05+00:00"

The parameter datepublish_1 would be - '2019-03-06 17:57:00'

Here are the select queries for PREVIOUS and NEXT: PREVIOUS Query SELECT _entries.*, GROUP_CONCAT(pages.id) as _pages_id, GROUP_CONCAT(pages.to_id) as _pages_toid, GROUP_CONCAT(incomingrelation.id) as _incomingrelation_id, GROUP_CONCAT(incomingrelation.from_id) as _incomingrelation_fromid, GROUP_CONCAT(incomingrelation.from_contenttype) as _incomingrelation_fromcontenttype, GROUP_CONCAT(categories.id, '|') as _categories_id, GROUP_CONCAT(categories.slug, '|') as _categories_slug, GROUP_CONCAT(categories.name, '|') as _categories_name, GROUP_CONCAT(categories.taxonomytype, '|') as _categories_taxonomytype, GROUP_CONCAT(tags.id, '|') as _tags_id, GROUP_CONCAT(tags.slug, '|') as _tags_slug, GROUP_CONCAT(tags.name, '|') as _tags_name, GROUP_CONCAT(tags.taxonomytype, '|') as _tags_taxonomytype FROM bolt_entries _entries LEFT JOIN bolt_relations pages ON _entries.id = pages.from_id AND pages.from_contenttype='entries' AND pages.to_contenttype='pages' LEFT JOIN bolt_relations incomingrelation ON _entries.id = incomingrelation.to_id AND incomingrelation.to_contenttype='entries' LEFT JOIN bolt_taxonomy "categories" ON _entries.id = "categories".content_id AND "categories".contenttype='entries' AND "categories".taxonomytype='categories' LEFT JOIN bolt_taxonomy "tags" ON _entries.id = "tags".content_id AND "tags".contenttype='entries' AND "tags".taxonomytype='tags' WHERE _entries.datepublish < :datepublish_1 GROUP BY _entries.id, _entries.id, _entries.id, _entries.id ORDER BY datepublish ASC LIMIT 1

NEXT Query: SELECT _entries.*, GROUP_CONCAT(pages.id) as _pages_id, GROUP_CONCAT(pages.to_id) as _pages_toid, GROUP_CONCAT(incomingrelation.id) as _incomingrelation_id, GROUP_CONCAT(incomingrelation.from_id) as _incomingrelation_fromid, GROUP_CONCAT(incomingrelation.from_contenttype) as _incomingrelation_fromcontenttype, GROUP_CONCAT(categories.id, '|') as _categories_id, GROUP_CONCAT(categories.slug, '|') as _categories_slug, GROUP_CONCAT(categories.name, '|') as _categories_name, GROUP_CONCAT(categories.taxonomytype, '|') as _categories_taxonomytype, GROUP_CONCAT(tags.id, '|') as _tags_id, GROUP_CONCAT(tags.slug, '|') as _tags_slug, GROUP_CONCAT(tags.name, '|') as _tags_name, GROUP_CONCAT(tags.taxonomytype, '|') as _tags_taxonomytype FROM bolt_entries _entries LEFT JOIN bolt_relations pages ON _entries.id = pages.from_id AND pages.from_contenttype='entries' AND pages.to_contenttype='pages' LEFT JOIN bolt_relations incomingrelation ON _entries.id = incomingrelation.to_id AND incomingrelation.to_contenttype='entries' LEFT JOIN bolt_taxonomy "categories" ON _entries.id = "categories".content_id AND "categories".contenttype='entries' AND "categories".taxonomytype='categories' LEFT JOIN bolt_taxonomy "tags" ON _entries.id = "tags".content_id AND "tags".contenttype='entries' AND "tags".taxonomytype='tags' WHERE _entries.datepublish > :datepublish_1 GROUP BY _entries.id, _entries.id, _entries.id, _entries.id ORDER BY datepublish ASC LIMIT 1

@xiaohutai - The question is should we just base it upon ID or continue using this approach?

Raistlfiren commented 5 years ago

The referenced code from Bolt can be seen here - https://github.com/bolt/bolt/blob/c3ead6d48cc2431d72ae7f81947df7fa5fefa92b/src/Twig/Runtime/RecordRuntime.php#L314 (previous function)