|
| 1 | +const express = require("express"); |
| 2 | + |
| 3 | +const {open} = require("sqlite"); |
| 4 | + |
| 5 | +const sqlite3 = require("sqlite3"); |
| 6 | + |
| 7 | +const path = require("path"); |
| 8 | + |
| 9 | +const app = express(); |
| 10 | + |
| 11 | +const dbPath = path.join(__dirname,"todoapp.db"); |
| 12 | + |
| 13 | +app.use(express.json()); |
| 14 | + |
| 15 | +let db = null; |
| 16 | + |
| 17 | +const initilaizeDbAndServer = async()=> { |
| 18 | + |
| 19 | + try { |
| 20 | + db = await open({ |
| 21 | + filename: dbPath, |
| 22 | + driver: sqlite3.Database, |
| 23 | + }); |
| 24 | + const port = 5200; |
| 25 | + app.listen(port, () => { |
| 26 | + console.log(`DB Connected\nServer Running at ${port}`); |
| 27 | + }); |
| 28 | + } catch (e) { |
| 29 | + console.log(`DB Error: ${e.message}`); |
| 30 | + process.exit(1); |
| 31 | + } |
| 32 | + |
| 33 | +}; |
| 34 | + |
| 35 | +initilaizeDbAndServer(); |
| 36 | + |
| 37 | +//table names is todo |
| 38 | +//Add todo tasks to Database Table... |
| 39 | +app.post("/todos/", async(req,res) => { |
| 40 | +try { |
| 41 | + const { |
| 42 | + id, |
| 43 | +todo, |
| 44 | +priority, |
| 45 | +status, |
| 46 | +category, |
| 47 | +dueDate |
| 48 | + } = req.body; |
| 49 | + |
| 50 | + const addTodoQuery = ` |
| 51 | +INSERT INTO todo( |
| 52 | + id , |
| 53 | +todo , |
| 54 | +priority, |
| 55 | +status , |
| 56 | +category, |
| 57 | +dueDate |
| 58 | +) |
| 59 | +VALUES( |
| 60 | + ${id}, |
| 61 | + "${todo}", |
| 62 | + "${priority}", |
| 63 | + "${status}", |
| 64 | + "${category}", |
| 65 | + "${dueDate}"); |
| 66 | + `; |
| 67 | + |
| 68 | + const todoResp = await db.run(addTodoQuery); |
| 69 | + |
| 70 | + res.status(201).json({ |
| 71 | + message :`todos added to the todo Table ` |
| 72 | + }); |
| 73 | + |
| 74 | + |
| 75 | +} catch (error) { |
| 76 | + console.log("todos" , error.message); |
| 77 | + res.status(500).send("Internal Server Error"); |
| 78 | +} |
| 79 | +}); |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +//Get a Single todo based on todoId from Table... |
| 84 | +app.get("/todos/:todoId", async(req,res) => { |
| 85 | + try { |
| 86 | + |
| 87 | + const {todoId} = req.params; |
| 88 | + |
| 89 | + const fetchQuery = `SELECT * from todo WHERE id=${todoId};`; |
| 90 | + |
| 91 | + const singletodo = await db.get(fetchQuery); |
| 92 | + |
| 93 | + |
| 94 | + res.status(200).json({ |
| 95 | + message :` Fetched todoId : ${todoId} from state Table`, singletodo : singletodo |
| 96 | + }); |
| 97 | + |
| 98 | + |
| 99 | + } catch (error) { |
| 100 | + console.log("todos" , error.message); |
| 101 | + res.status(500).send("Internal Server Error"); |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +//Update Todo status and Add to Database Table... |
| 108 | +app.put("/todos/:todoId/", async(req,res) => { |
| 109 | + try { |
| 110 | + const {todoId} = req.params; |
| 111 | + const {status} = req.body; |
| 112 | + |
| 113 | + const updateStatusQuery = `UPDATE todo SET status = '${status}' WHERE id = ${todoId}`; |
| 114 | + |
| 115 | + |
| 116 | + const todoStatusResp = await db.run(updateStatusQuery); |
| 117 | + |
| 118 | + res.status(200).json({ |
| 119 | + message :`Todo status updated Successfully with todoId : ${todoId}` |
| 120 | + }); |
| 121 | + |
| 122 | + } catch (error) { |
| 123 | + console.log("todos" , error.message); |
| 124 | + res.status(500).send("Internal Server Error"); |
| 125 | + } |
| 126 | + }); |
| 127 | + |
| 128 | + |
| 129 | +//Update Todo priority and Add to Database Table... |
| 130 | +app.put("/todos/:todoId/", async(req,res) => { |
| 131 | + try { |
| 132 | + const {todoId} = req.params; |
| 133 | + const {priority} = req.body; |
| 134 | + |
| 135 | + const updatePriorityQuery = `UPDATE todo SET priority = '${priority}' WHERE id = ${todoId}`; |
| 136 | + |
| 137 | + |
| 138 | + const todoPriorityResp = await db.run(updatePriorityQuery); |
| 139 | + |
| 140 | + res.status(200).json({ |
| 141 | + message :`Todo Priority is updated Successfully on todoId : ${todoId}` |
| 142 | + }); |
| 143 | + |
| 144 | + |
| 145 | + } catch (error) { |
| 146 | + console.log("todos" , error.message); |
| 147 | + res.status(500).send("Internal Server Error"); |
| 148 | + } |
| 149 | + }); |
| 150 | + |
| 151 | + |
| 152 | +//Update Todo category and Add to Database Table... |
| 153 | +app.put("/todos/:todoId/", async(req,res) => { |
| 154 | + try { |
| 155 | + const {todoId} = req.params; |
| 156 | + const {category} = req.body; |
| 157 | + |
| 158 | + const updateCategoryQuery = `UPDATE todo SET category = '${category}' WHERE id = ${todoId}`; |
| 159 | + |
| 160 | + |
| 161 | + const todoCategoryResp = await db.run(updateCategoryQuery); |
| 162 | + |
| 163 | + res.status(200).json({ |
| 164 | + message :`Todo Category is updated Successfully on todoId : ${todoId}` |
| 165 | + }); |
| 166 | + |
| 167 | + |
| 168 | + } catch (error) { |
| 169 | + console.log("todos" , error.message); |
| 170 | + res.status(500).send("Internal Server Error"); |
| 171 | + } |
| 172 | + }); |
| 173 | + |
| 174 | + |
| 175 | +//Update Todo table todo and Add to Database Table... |
| 176 | +app.put("/todos/:todoId/", async(req,res) => { |
| 177 | + try { |
| 178 | + const {todoId} = req.params; |
| 179 | + const {todo} = req.body; |
| 180 | + |
| 181 | + const updateTodoQuery = `UPDATE todo SET todo = '${todo}' WHERE id = ${todoId}`; |
| 182 | + |
| 183 | + |
| 184 | + const todoResp = await db.run(updateTodoQuery); |
| 185 | + |
| 186 | + res.status(200).json({ |
| 187 | + message :`Todos table TODO is updated Successfully on todoId : ${todoId}` |
| 188 | + }); |
| 189 | + |
| 190 | + |
| 191 | + } catch (error) { |
| 192 | + console.log("todos" , error.message); |
| 193 | + res.status(500).send("Internal Server Error"); |
| 194 | + } |
| 195 | + }); |
| 196 | + |
| 197 | + |
| 198 | +//Update Todo dueDate and Add to Database Table... |
| 199 | +app.put("/todos/:todoId/", async(req,res) => { |
| 200 | + try { |
| 201 | + const {todoId} = req.params; |
| 202 | + const {dueDate} = req.body; |
| 203 | + |
| 204 | + const updateDueDateQuery = `UPDATE todo SET dueDate = '${dueDate}' WHERE id = ${todoId}`; |
| 205 | + |
| 206 | + |
| 207 | + const todoDueDateResp = await db.run(updateDueDateQuery); |
| 208 | + |
| 209 | + res.status(200).json({ |
| 210 | + message :`Todo DueDate is updated Successfully on todoId : ${todoId}` |
| 211 | + }); |
| 212 | + |
| 213 | + |
| 214 | + } catch (error) { |
| 215 | + console.log("todos" , error.message); |
| 216 | + res.status(500).send("Internal Server Error"); |
| 217 | + } |
| 218 | + }); |
| 219 | + |
| 220 | + |
| 221 | + |
| 222 | +//Delete todo from Database Table... |
| 223 | +app.delete("/todos/:todoId", async(req,res) => { |
| 224 | + try { |
| 225 | + const {todoId} = req.params; |
| 226 | + |
| 227 | + |
| 228 | + const deleteTodoQuery = `DELETE FROM todo WHERE id = ${todoId}`; |
| 229 | + |
| 230 | + |
| 231 | + const todosresp = await db.run(deleteTodoQuery); |
| 232 | + |
| 233 | + res.status(201).json({ |
| 234 | + message :` Todo deleted Successfully with TodoId : ${todoId}` |
| 235 | + }); |
| 236 | + |
| 237 | + |
| 238 | + } catch (error) { |
| 239 | + console.log("todos" , error.message); |
| 240 | + res.status(500).send("Internal Server Error"); |
| 241 | + } |
| 242 | + }); |
| 243 | + |
| 244 | + //fliters Api's.. |
| 245 | + //path--> http://localhost:5200/todos/?status=TO%20DO |
| 246 | + // api...({status="",priority="",category="",search_q=""})...({[OR operator]}) |
| 247 | + app.get("/todos/", async(req,res)=>{ |
| 248 | + |
| 249 | + try { |
| 250 | + const {status="",priority="",category=""} = req.query; |
| 251 | + |
| 252 | + const fetchQuery = `SELECT * FROM todo WHERE status = '${status}' OR category = '${category}' OR priority = '${priority}' `; |
| 253 | + |
| 254 | + const todoResp2 = await db.all(fetchQuery); |
| 255 | + |
| 256 | + res.status(201).json({ |
| 257 | + message : `Values are Fetched `, todoResp:todoResp2, |
| 258 | + }) |
| 259 | + |
| 260 | + } catch (error) { |
| 261 | + console.log("todos" , error.message); |
| 262 | + res.status(500).send("Internal Server Error"); |
| 263 | + } |
| 264 | + }); |
| 265 | + |
| 266 | +//API for search_q... |
| 267 | +//path :---> http://localhost:5200/todoserach_q/?search_q=buy |
| 268 | + app.get("/todoserach_q/", async(req,res)=>{ |
| 269 | + |
| 270 | + try { |
| 271 | + const {search_q=""} = req.query; |
| 272 | + |
| 273 | + const fetchQuery = `SELECT * FROM todo WHERE todo LIKE '%${search_q}%'`; |
| 274 | + |
| 275 | + const todoResp = await db.all(fetchQuery); |
| 276 | + |
| 277 | + res.status(201).json({ |
| 278 | + message : `Search_q Values are Fetched `, todoResp:todoResp, |
| 279 | + }) |
| 280 | + |
| 281 | + } catch (error) { |
| 282 | + console.log("todoserach_q" , error.message); |
| 283 | + res.status(500).send("Internal Server Error"); |
| 284 | + } |
| 285 | + }); |
| 286 | + |
| 287 | + |
| 288 | + // http://localhost:5200/todo/?status=DONE&priority=HIGH -->api...({status="",priority="",category=""})...({[AND operator]}) |
| 289 | + app.get("/todo/", async(req,res)=>{ |
| 290 | + |
| 291 | + try { |
| 292 | + const {status="",priority="",category=""} = req.query; |
| 293 | + |
| 294 | + const sqlQuery = `SELECT * FROM todo WHERE ( priority = '${priority}' AND status = '${status}') OR (category = '${category}' AND status = '${status}') OR (category = '${category}' AND priority = '${priority}'); `; |
| 295 | + |
| 296 | + const todoResp = await db.all(sqlQuery); |
| 297 | + |
| 298 | + res.status(201).json({ |
| 299 | + message : `Values are Fetched by using AND Operator`, todoResp:todoResp, |
| 300 | + }) |
| 301 | + |
| 302 | + } catch (error) { |
| 303 | + console.log("todo" , error.message); |
| 304 | + res.status(500).send("Internal Server Error"); |
| 305 | + } |
| 306 | + }); |
| 307 | + |
| 308 | + |
| 309 | + |
| 310 | +//path :---> http://localhost:5200/agenda/?date=2021-12-12 |
| 311 | +//dueDate Api... |
| 312 | + app.get("/agenda/", async(req,res)=>{ |
| 313 | + |
| 314 | + try { |
| 315 | + const {date=""} = req.query; |
| 316 | + |
| 317 | + const fetchQuery = `SELECT * FROM todo WHERE dueDate = '${date}'`; |
| 318 | + |
| 319 | + const todoResp = await db.all(fetchQuery); |
| 320 | + |
| 321 | + res.status(201).json({ |
| 322 | + message : `Date Values are Fetched `, todoResp:todoResp, |
| 323 | + }) |
| 324 | + |
| 325 | + } catch (error) { |
| 326 | + console.log("agenda" , error.message); |
| 327 | + res.status(500).send("Internal Server Error"); |
| 328 | + } |
| 329 | + }); |
0 commit comments