Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bg(chat): add pagination when retrieving the messages #94

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 34 additions & 19 deletions src/controllers/chat.controller.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { config } from 'dotenv';
import { clients } from '../helpers/socket.helper';
import UserService from '../services/user.service';
import chatService from '../services/chat.service';
import response from '../helpers/response.helper';

import { config } from "dotenv";
import { clients } from "../helpers/socket.helper";
import UserService from "../services/user.service";
import chatService from "../services/chat.service";
import response from "../helpers/response.helper";
import Paginate from "../helpers/paginate.helper";

config();
/** Function to list all users
* @returns {*} data returned
*/
* @returns {*} data returned
*/
class ChatController {
/** Function to list all users
* @param {object} req the request sent
* @param {object} res the response returned
* @returns {*} data returned
*/
* @param {object} req the request sent
* @param {object} res the response returned
* @returns {*} data returned
*/
static async getAllUsers(req, res) {
try {
const newArray = [];
Expand All @@ -40,26 +40,41 @@ class ChatController {
});
}
});
return response.successMessage(res, 'users available', 200, newArray.sort((a, b) => a.isOnline.toString().localeCompare(b.isOnline.toString())).reverse());
return response.successMessage(
res,
"users available",
200,
newArray
.sort((a, b) =>
a.isOnline.toString().localeCompare(b.isOnline.toString())
)
.reverse()
);
} catch (error) {
return error.message;
}
}

/** Get Private and public message between two users
* @param {object} req the request sent
* @param {object} res the response returned
* @returns {*} data returned
*/
* @param {object} req the request sent
* @param {object} res the response returned
* @returns {*} data returned
*/
static async getMessages(req, res) {
try {
const { userId } = req.params;
const { id } = req.user;
const { limit, page } = req.query;
const offset = Paginate(page, limit);
if (!userId) {
const publicMessages = await chatService.getPublicMessage();
const publicMessages = await chatService.getPublicMessage(
null,
limit,
offset
);
return response.successMessage(res, 'Messages', 200, publicMessages);
}
const privateMessages = await chatService.getPrivateMessage(userId, id);
const privateMessages = await chatService.getPrivateMessage(userId, id, limit, offset);
return response.successMessage(res, 'Messages', 200, privateMessages);
} catch (error) {
return response.errorMessage(res, error.message, 500);
Expand Down
Loading