Add links to adjacent entries with data from a single API call

Woah! I figured it out on my own! Did not expect that to happen.

const UID = "api::case-study.case-study";

module.exports = createCoreController(UID, ({ strapi }) => ({
  async findOne(ctx) {
    await this.validateQuery(ctx);

    const { slug } = ctx.params;

    const [entity, total] = await Promise.all([
      strapi.db.query(UID).findOne({
        where: { slug },
        populate: true,
      }),
      strapi.db
        .query(UID)
        .count({ where: { publishedAt: { $notNull: true } } }),
    ]);

    const prevOrder = entity.order === 1 ? total : entity.order - 1;
    const nextOrder = entity.order === total ? 1 : entity.order + 1;

    const adjacent = await strapi.entityService.findMany(UID, {
      select: ["name", "order", "slug"],
      publicationState: "live",
      filters: {
        order: { $in: [prevOrder, nextOrder] },
      },
    });

    const prev = adjacent.find((a) => a.order === prevOrder) || null;
    const next = adjacent.find((a) => a.order === nextOrder) || null;

    entity.adjacent = {
      prev,
      next,
    };

    const sanitizedEntity = await this.sanitizeOutput(entity, ctx);

    return this.transformResponse(sanitizedEntity);
  },
}));