| const axios = require("axios"); | |
| const getChats = async (req, res) => { | |
| try { | |
| const response = await axios.get(`http://localhost:8082/get-chats`); | |
| console.log(response.data); | |
| if (!response.data) { | |
| return res.status(200).json({ | |
| success: false, | |
| message: "failed to get chats", | |
| }); | |
| } | |
| const chats = response.data; | |
| const all_chats = chats.sort( | |
| (a, b) => new Date(b.lastUpdatedAt) - new Date(a.lastUpdatedAt) | |
| ); | |
| return res.status(200).json({ | |
| success: true, | |
| message: "All chats", | |
| chats: all_chats, | |
| }); | |
| } catch (error) { | |
| console.log(error); | |
| return res | |
| .status(500) | |
| .json({ success: false, message: "Failed to get chat" }); | |
| } | |
| }; | |
| const renameChats = async (req, res) => { | |
| try { | |
| const { sessionId, title } = req.body; | |
| const response = await axios.post(`http://localhost:8082/rename-chat`, { | |
| sessionId, | |
| title, | |
| }); | |
| console.log(response.data); | |
| if (!response.data) { | |
| return res.status(200).json({ | |
| success: false, | |
| message: "failed to rename chats", | |
| }); | |
| } | |
| return res.status(200).json({ | |
| success: true, | |
| message: "chat renamed!", | |
| rename: { id: sessionId, title: title }, | |
| }); | |
| } catch (error) { | |
| console.log(error); | |
| return res | |
| .status(500) | |
| .json({ success: false, message: "Failed to get chat" }); | |
| } | |
| }; | |
| const createChat = async (req, res) => { | |
| try { | |
| const { sessionId } = req.body; | |
| console.log(sessionId); | |
| const response = await axios.post( | |
| `http://localhost:8082/create-chat/${sessionId}` | |
| ); | |
| if (!response.data) { | |
| return res | |
| .status(500) | |
| .json({ success: false, message: "Failed to create chat" }); | |
| } | |
| return res.status(200).json({ | |
| success: true, | |
| message: "Chat created successfully", | |
| sessionId: sessionId, | |
| }); | |
| } catch (error) { | |
| console.log(error); | |
| return res | |
| .status(500) | |
| .json({ success: false, message: "Failed to create chat" }); | |
| } | |
| }; | |
| const deleteChat = async (req, res) => { | |
| try { | |
| const { sessionId } = req.body; | |
| const response = await axios.post(`http://localhost:8082/delete-chat`, { | |
| sessionId, | |
| }); | |
| if (!response.data) { | |
| return res | |
| .status(500) | |
| .json({ success: false, message: "Failed to delete chat" }); | |
| } | |
| return res.status(200).json({ | |
| success: true, | |
| message: "Chat deleted successfully", | |
| sessionId: sessionId, | |
| }); | |
| } catch (error) { | |
| console.log(error); | |
| return res | |
| .status(500) | |
| .json({ success: false, message: "Failed to delete chat" }); | |
| } | |
| }; | |
| module.exports = { createChat, getChats, renameChats, deleteChat }; | |