Skip to content

Commit

Permalink
Change: add total for pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
Voktor Stolenets committed Jan 22, 2025
1 parent b5c1e48 commit f3b4770
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 89 deletions.
194 changes: 112 additions & 82 deletions src/documentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
44 changes: 37 additions & 7 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -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 });
Expand All @@ -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 });
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -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 });
Expand All @@ -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 });
Expand Down

0 comments on commit f3b4770

Please sign in to comment.