Changed controllers to classes, added image functionality
This commit is contained in:
@@ -1,43 +1,85 @@
|
||||
import { getMessages as getMessagesService, findMatch, registerUser as registerUserService, addMessage, endChat } from '../services/chatService.js';
|
||||
import {
|
||||
getMessages as getMessagesService,
|
||||
findMatch,
|
||||
registerUser as registerUserService,
|
||||
addMessage,
|
||||
endChat,
|
||||
removeUser as removeUserService
|
||||
} from '../services/chatService.js';
|
||||
|
||||
export const getMessages = (req, res) => {
|
||||
const { to, from } = req.body;
|
||||
const messages = getMessagesService(to, from);
|
||||
res.status(200).json(messages);
|
||||
};
|
||||
|
||||
export const findRandomChatMatch = (req, res) => {
|
||||
const { genders, age, id } = req.body;
|
||||
const match = findMatch(genders, age, id);
|
||||
if (match) {
|
||||
res.status(200).json({ status: 'matched', user: match });
|
||||
} else {
|
||||
res.status(200).json({ status: 'waiting' });
|
||||
class ChatController {
|
||||
constructor() {
|
||||
this.getMessages = this.getMessages.bind(this);
|
||||
this.findRandomChatMatch = this.findRandomChatMatch.bind(this);
|
||||
this.registerUser = this.registerUser.bind(this);
|
||||
this.sendMessage = this.sendMessage.bind(this);
|
||||
this.stopChat = this.stopChat.bind(this);
|
||||
this.removeUser = this.removeUser.bind(this);
|
||||
}
|
||||
};
|
||||
|
||||
export const registerUser = (req, res) => {
|
||||
const { gender, age } = req.body;
|
||||
const userId = registerUserService(gender, age);
|
||||
res.status(200).json({ id: userId });
|
||||
};
|
||||
async getMessages(req, res) {
|
||||
const { to, from } = req.body;
|
||||
try {
|
||||
const messages = await getMessagesService(to, from);
|
||||
res.status(200).json(messages);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
export const sendMessage = (req, res) => {
|
||||
const from = req.body.from;
|
||||
const to = req.body.to;
|
||||
const text = req.body.text;
|
||||
const message = addMessage(from, to, text);
|
||||
res.status(200).json(message);
|
||||
};
|
||||
async findRandomChatMatch(req, res) {
|
||||
const { genders, age, id } = req.body;
|
||||
try {
|
||||
const match = await findMatch(genders, age, id);
|
||||
if (match) {
|
||||
res.status(200).json({ status: 'matched', user: match });
|
||||
} else {
|
||||
res.status(200).json({ status: 'waiting' });
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
export const removeUser = (req, res) => {
|
||||
const { id } = req.body;
|
||||
removeUserService(id);
|
||||
res.sendStatus(200);
|
||||
};
|
||||
async registerUser(req, res) {
|
||||
const { gender, age } = req.body;
|
||||
try {
|
||||
const userId = await registerUserService(gender, age);
|
||||
res.status(200).json({ id: userId });
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
export const stopChat = (req, res) => {
|
||||
const { id } = req.body;
|
||||
endChat(id);
|
||||
res.sendStatus(200);
|
||||
}
|
||||
async sendMessage(req, res) {
|
||||
const { from, to, text } = req.body;
|
||||
try {
|
||||
const message = await addMessage(from, to, text);
|
||||
res.status(200).json(message);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
async removeUser(req, res) {
|
||||
const { id } = req.body;
|
||||
try {
|
||||
await removeUserService(id);
|
||||
res.sendStatus(200);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
async stopChat(req, res) {
|
||||
const { id } = req.body;
|
||||
try {
|
||||
await endChat(id);
|
||||
res.sendStatus(200);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default ChatController;
|
||||
|
||||
Reference in New Issue
Block a user