From f3b47706e6aa344b8d3043f96cc782555ddd5f53 Mon Sep 17 00:00:00 2001 From: Voktor Stolenets Date: Wed, 22 Jan 2025 22:51:54 +0200 Subject: [PATCH] Change: add total for pagination --- src/documentation.ts | 194 +++++++++++++++++++++++++------------------ src/server.ts | 44 ++++++++-- 2 files changed, 149 insertions(+), 89 deletions(-) diff --git a/src/documentation.ts b/src/documentation.ts index e9203c8..5d89217 100644 --- a/src/documentation.ts +++ b/src/documentation.ts @@ -294,23 +294,28 @@ paths: content: application/json: schema: - type: array - items: - type: object - properties: - _id: - type: string - address: - type: string - tokenAddress: - type: string - message: - type: string - id: - type: string - timestamp: - type: string - format: date-time + type: object + properties: + total: + type: integer + data: + type: array + items: + type: object + properties: + _id: + type: string + address: + type: string + tokenAddress: + type: string + message: + type: string + id: + type: string + timestamp: + type: string + format: date-time "500": description: Server error content: @@ -345,23 +350,28 @@ paths: content: application/json: schema: - type: array - items: - type: object - properties: - _id: - type: string - address: - type: string - tokenAddress: - type: string - message: - type: string - id: - type: string - timestamp: - type: string - format: date-time + type: object + properties: + total: + type: integer + data: + type: array + items: + type: object + properties: + _id: + type: string + address: + type: string + tokenAddress: + type: string + message: + type: string + id: + type: string + timestamp: + type: string + format: date-time "500": description: Server error content: @@ -396,23 +406,28 @@ paths: content: application/json: schema: - type: array - items: - type: object - properties: - _id: - type: string - address: - type: string - tokenAddress: - type: string - message: - type: string - id: - type: string - timestamp: - type: string - format: date-time + type: object + properties: + total: + type: integer + data: + type: array + items: + type: object + properties: + _id: + type: string + address: + type: string + tokenAddress: + type: string + message: + type: string + id: + type: string + timestamp: + type: string + format: date-time "500": description: Server error content: @@ -449,16 +464,21 @@ paths: content: application/json: schema: - type: array - items: - type: object - properties: - address: - type: string - description: The address of the follower. - userAddress: - type: string - description: The address of the user being followed. + type: object + properties: + total: + type: integer + data: + type: array + items: + type: object + properties: + address: + type: string + description: The address of the follower. + userAddress: + type: string + description: The address of the user being followed. "400": description: Invalid query parameter or missing required field. content: @@ -504,16 +524,21 @@ paths: content: application/json: schema: - type: array - items: - type: object - properties: - address: - type: string - description: The address of the follower. - userAddress: - type: string - description: The address of the user being followed. + type: object + properties: + total: + type: integer + data: + type: array + items: + type: object + properties: + address: + type: string + description: The address of the follower. + userAddress: + type: string + description: The address of the user being followed. "400": description: Invalid query parameter or missing required field. content: @@ -587,17 +612,22 @@ paths: content: application/json: schema: - type: array - items: - type: object - properties: - address: - type: string - tokenAddress: - type: string - timestamp: - type: string - format: date-time + type: object + properties: + total: + type: integer + data: + type: array + items: + type: object + properties: + address: + type: string + tokenAddress: + type: string + timestamp: + type: string + format: date-time "500": description: Server error content: diff --git a/src/server.ts b/src/server.ts index 7c88b0c..df53b86 100644 --- a/src/server.ts +++ b/src/server.ts @@ -311,7 +311,12 @@ async function getMessages(request: FastifyRequest, reply: FastifyReply) { .skip(skip) .limit(limit) .toArray(); - return reply.send(messages); + + const total = await request.server.mongo.db + ?.collection("message") + .countDocuments({ tokenAddress }); + + return reply.send({ total: total, data: messages }); } catch (error) { request.log.error(error); return reply.status(500).send({ error: error.message }); @@ -333,7 +338,12 @@ async function getMessagesByUser(request: FastifyRequest, reply: FastifyReply) { .skip(skip) .limit(limit) .toArray(); - return reply.send(messages); + + const total = await request.server.mongo.db + ?.collection("message") + .countDocuments({ address }); + + return reply.send({ total: total, data: messages }); } catch (error) { request.log.error(error); return reply.status(500).send({ error: error.message }); @@ -354,7 +364,12 @@ async function getMessageReplies(request: FastifyRequest, reply: FastifyReply) { .skip(skip) .limit(limit) .toArray(); - return reply.send(messages); + + const total = await request.server.mongo.db + ?.collection("message") + .countDocuments({ id }); + + return reply.send({ total: total, data: messages }); } catch (error) { request.log.error(error); return reply.status(500).send({ error: error.message }); @@ -391,7 +406,12 @@ async function getUserLikes(request: FastifyRequest, reply: FastifyReply) { .skip(skip) .limit(limit) .toArray(); - return reply.send(likes); + + const total = await request.server.mongo.db + ?.collection("like") + .countDocuments({ address }); + + return reply.send({ total: total, data: likes }); } catch (error) { request.log.error(error); return reply.status(500).send({ error: error.message }); @@ -501,7 +521,12 @@ async function getFollowers(request: FastifyRequest, reply: FastifyReply) { .skip(skip) .limit(limit) .toArray(); - return reply.send(followers); + + const total = await request.server.mongo.db + ?.collection("followers") + .countDocuments({ userAddress }); + + return reply.send({ total: total, data: followers }); } catch (error) { request.log.error(error); return reply.status(500).send({ error: error.message }); @@ -515,14 +540,19 @@ async function getFollowed(request: FastifyRequest, reply: FastifyReply) { skip?: number; limit?: number; }; - const followers = await request.server.mongo.db + const followerd = await request.server.mongo.db ?.collection("followers") .find({ address }, { projection: { _id: 0 } }) .sort({ timestamp: -1 }) .skip(skip) .limit(limit) .toArray(); - return reply.send(followers); + + const total = await request.server.mongo.db + ?.collection("followers") + .countDocuments({ address }); + + return reply.send({ total: total, data: followerd }); } catch (error) { request.log.error(error); return reply.status(500).send({ error: error.message });