-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscratchUseApplication(React).jsx
More file actions
252 lines (226 loc) · 7.65 KB
/
scratchUseApplication(React).jsx
File metadata and controls
252 lines (226 loc) · 7.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// // Declarations
// import dotenv from "dotenv";
// import express, { response } from "express";
// import morgan from "morgan";
// import bodyParser from "body-parser";
// import { getImage, getChatResponse } from "./lib/openAIHelpers.js";
// import { MongoClient, ServerApiVersion, ObjectId } from "mongodb";
// import { OpenAIApi, Configuration } from "openai";
// // import { termForAiDrawer, termForAiDrawer1 } from './helper/filterUserWords.js';
// dotenv.config();
// const app = express();
// const { ENVIROMENT, PORT, GPT_API_KEY, DB_MONGO_PASSWORD } = process.env;
// // Configure OpenAI client
// const openai = new OpenAIApi(
// new Configuration({
// apiKey: GPT_API_KEY,
// })
// );
// ///
// //MONGO
// ///
// const uri = DB_MONGO_PASSWORD;
// // Create a MongoClient with a MongoClientOptions object to set the Stable API version
// const client = new MongoClient(uri, {
// serverApi: {
// version: ServerApiVersion.v1,
// strict: true,
// deprecationErrors: true,
// },
// });
// let db;
// async function run() {
// try {
// await client.connect();
// db = client.db("Memory"); //name of the db
// console.log("Successfully connected to MongoDB!");
// } catch (error) {
// console.error("Error connecting to MongoDB:", error);
// process.exit(1);
// }
// }
// run().catch(console.dir);
// ///
// //END MONGO
// //
// //routes import
// import exampleRoutes from "./routes/exampleRoutes.js";
// // middleware setup
// app.use(bodyParser.json());
// app.use(morgan(ENVIROMENT));
// // const express = require('express');
// // const app = express();
// // const { getChatResponse } = require('./lib/openAIHelpers'); // Use 'require' for Node.js modules
// // const PORT = process.env.PORT || 8080; // Define the port
// app.use(express.json());
// app.post("/api/getChatResponse", async (req, res) => {
// const content = req.body.content;
// try {
// const chatResponse = await getChatResponse(content); // Call the helper function
// res.json({ response: chatResponse });
// } catch (error) {
// res.status(500).json({ error: "An error occurred." });
// }
// });
// // app.listen(PORT, () => console.log(`Server is listening on port ${PORT}`));
// //
// //ROUTES
// //
// // app.use('/cats', exampleRoutes);
// // // For Later ChatGPT integration
// // app.get('/phrases', (req, res) => {
// // const ChatGptWord = WordtermForAiDrawer(req);
// // const result = getChatResponse(`${ChatGptWord}`)
// // .then(response => {
// // res.json(response);
// // const img_url = getImage(result);
// // console.log(img_url);
// // db.Palace.insertOne({
// // "name": `${item}`,
// // "Palace Description": `${result}`,
// // "front_img_url": `${img_url}`,
// // "room": `${roomID}`
// // });
// // });
// // });
// // app.post('/initMemoryPalace', (req, res) => {
// // const memoryPalaceCollection = db.collection("Palaces"); //name of collection
// // const palaceToInsert = req.body;
// // memoryPalaceCollection.insertMany(palaceToInsert)
// // .then(result => {
// // //example result
// // // = {
// // // acknowledged: true,
// // // insertedCount: 2,
// // // insertedIds: {
// // // '0': new ObjectId("64d3bb72171be03ea57537d7"),
// // // '1': new ObjectId("64d3bb72171be03ea57537d8")
// // // ......
// // // }
// // // }
// // res.json({ success: true, insertedCount: result.insertedCount, insertedIds: result.insertedIds });
// // })
// // .catch(error => {
// // res.status(500).json({ success: false, message: "Failed to insert memory palaces.", error: error });
// // });
// // });
// // CREATE: New Memory Palace
// app.post("/initMemoryPalace", async (req, res) => {
// const memoryPalaceCollection = db.collection("Palaces");
// try {
// const result = await memoryPalaceCollection.insertOne(req.body);
// const insertedDocument = await memoryPalaceCollection.findOne({
// _id: result.insertedId,
// });
// if (insertedDocument) {
// res.json({
// success: true,
// insertedCount: 1,
// insertedId: insertedDocument._id,
// palaceData: insertedDocument,
// });
// } else {
// res.status(500).json({
// success: false,
// message: "Inserted data is not available.",
// });
// }
// } catch (error) {
// res
// .status(500)
// .json({
// success: false,
// message: "Failed to insert memory palaces.",
// error: error,
// });
// }
// });
// // READ: All Memory Palaces
// app.get("/getMemoryPalaces", (req, res) => {
// const memoryPalaceCollection = db.collection("Palaces"); //name of collection
// memoryPalaceCollection
// .find({})
// .toArray()
// .then((palaces) => {
// res.json(palaces);
// })
// .catch((error) => {
// res
// .status(500)
// .json({ success: false, message: "Failed to fetch memory palaces." });
// });
// });
// // UPDATE: Existing Memory Palace
// app.put("/update", (req, res) => {
// console.log("reqbodydata", req.body.data);
// const palaceId = new ObjectId(req.body.id);
// const updatedData = req.body.data;
// updatedData._id = palaceId;
// const memoryPalaceCollection = db.collection("Palaces");
// memoryPalaceCollection
// .find({ _id: new ObjectId(palaceId) })
// .toArray()
// .then((palaces) => {
// console.log(palaces);
// });
// memoryPalaceCollection
// .replaceOne(
// { _id: palaceId }, // Query for the specific palace using _id
// updatedData // Update specific fields using $set
// )
// .then((result) => {
// console.log("result count", result.matchedCount);
// if (result.matchedCount > 0) {
// console.log("*** object update success ***");
// res.json({
// success: true,
// message: "Palace updated successfully.",
// });
// } else {
// console.log("CALLED!!");
// res.status(404).json({
// success: false,
// message: "Palace not found.",
// });
// }
// })
// .catch((error) => {
// res.status(500).json({
// success: false,
// message: "Failed to update palace.",
// error: error,
// });
// });
// });
// // DELETE: Memory Palace by ID
// // app.delete('/deleteMemoryPalace/:id', async (req, res) => {
// // const palaceId = new ObjectId(req.params.id);
// // try {
// // const memoryPalaceCollection = db.collection('Palaces');
// // // Find the palace before deleting it (for logging or other purposes if needed)
// // const palaceToDelete = await memoryPalaceCollection.findOne({ _id: palaceId });
// // // Delete the palace
// // const deleteResult = await memoryPalaceCollection.deleteOne({ _id: palaceId });
// // if (deleteResult.deletedCount > 0) {
// // console.log(`Deleted palace with ID: ${palaceId}`);
// // res.json({
// // success: true,
// // message: 'Palace deleted successfully.',
// // deletedPalace: palaceToDelete
// // });
// // } else {
// // res.status(404).json({
// // success: false,
// // message: 'Palace not found for deletion.'
// // });
// // }
// // } catch (error) {
// // console.error("Error deleting memory palace:", error);
// // res.status(500).json({
// // success: false,
// // message: 'Failed to delete palace.',
// // error: error
// // });
// // }
// // });
// app.listen(PORT, () => console.log(`Server is listening on port ${PORT}`));