const express = require("express"); const mongoose = require("mongoose"); const app = express(); const port = 3001; // Connecting to MongoDB Atlas mongoose.connect("mongodb+srv://ronreciproco123:admin123@cluster0.9ao1xec.mongodb.net/b322_to-do?retryWrites=true&w=majority", { useNewUrlParser: true, useUnifiedTopology: true }); let db = mongoose.connection; db.on("error", console.error.bind(console, "connection error")); db.once("open", () => console.log("We're connected to the cloud database")); // [Section] Mongoose Schemas // Schema is the blueprint of our "task" documents const taskSchema = new mongoose.Schema({ name: String, status: { type: String, default: "pending" } }); // [Section] Models const Task = mongoose.model("Task", taskSchema); // Middlewares app.use(express.json()); app.use(express.urlencoded({extended:true})); app.post("/tasks", (req, res) => { Task.findOne({name: req.body.name}).then((result, error) => { if(result != null && result.name == req.body.name){ return res.send("Duplicate task found!"); } else { let newTask = new Task({ name: req.body.name }); newTask.save().then((saveTask, saveErr) => { if(saveErr){ return console.error(saveErr); } else { return res.status(201).send("New task created!"); } }) } }) }); // Activity Output app.post('/signup', async (req, res) => { try { const { username, password } = req.body; console.log('Received data from Postman:'); console.log('Username:', username); console.log('Password:', password); // Create a new user const newUser = new User({ username, password }); // Save the user to the database await newUser.save(); res.status(201).json({ message: 'User created successfully' }); } catch (err) { console.error('Error:', err); res.status(500).json({ error: err.message }); } }); // app.get("/tasks", (req, res) => { // Task.find({}).then((result, error) => { // if(error){ // return console.log(error); // } else { // return res.status(200).json({ // data: result // }) // } // }); // }); if(require.main === module){ app.listen(port, () => console.log(`Server running at port ${port}`)); } module.export = app;