-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit decd254
Showing
21 changed files
with
2,312 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.gitignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
MONGO_URI=mongodb+srv://mercy:[email protected]/tododb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
FROM node:18 | ||
|
||
WORKDIR /usr/src/app | ||
|
||
COPY package*.json ./ | ||
|
||
RUN npm install | ||
|
||
COPY . . | ||
|
||
EXPOSE 3000 | ||
|
||
CMD ["npm", "start"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const TodoSchema = new mongoose.Schema({ | ||
title: { | ||
type: String, | ||
required: true | ||
}, | ||
description: { | ||
type: String, | ||
default: '' | ||
}, | ||
completed: { | ||
type: Boolean, | ||
default: false | ||
}, | ||
actions: { | ||
update: { | ||
type: Boolean, | ||
default: true | ||
}, | ||
delete: { | ||
type: Boolean, | ||
default: true | ||
} | ||
}, | ||
dueDate: { | ||
type: Date, | ||
default: null | ||
} | ||
}); | ||
|
||
module.exports = mongoose.model('Todo', TodoSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const express = require('express'); | ||
const mongoose = require('mongoose'); | ||
const Todo = require('./Models/Todo'); | ||
const cors = require('cors'); | ||
require('dotenv').config(); | ||
|
||
const app = express(); | ||
const port = 3000; | ||
|
||
// Enable CORS | ||
app.use(cors()); | ||
|
||
// Enable JSON parsing | ||
app.use(express.json()); | ||
|
||
// MongoDB connection | ||
mongoose.connect(process.env.MONGO_URI, { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true | ||
}) | ||
.then(() => console.log('MongoDB connected')) | ||
.catch(err => console.log(err)); | ||
|
||
// Create a new todo | ||
app.post('/todos_create', async (req, res) => { | ||
const newTodo = new Todo(req.body); | ||
await newTodo.save(); | ||
res.status(201).json(newTodo); | ||
}); | ||
|
||
// Get all todos | ||
app.get('/todos', async (req, res) => { | ||
const todos = await Todo.find(); | ||
res.json(todos); | ||
}); | ||
|
||
// Get a single todo by ID | ||
app.get('/todos/:id', async (req, res) => { | ||
const todo = await Todo.findById(req.params.id); | ||
res.json(todo); | ||
}); | ||
|
||
// Update a todo by ID | ||
app.put('/todos/:id', async (req, res) => { | ||
const updatedTodo = await Todo.findByIdAndUpdate(req.params.id, req.body, { new: true }); | ||
res.json(updatedTodo); | ||
}); | ||
|
||
// Delete a todo by ID | ||
app.delete('/todos/:id', async (req, res) => { | ||
await Todo.findByIdAndDelete(req.params.id); | ||
res.json({ message: 'Todo deleted' }); | ||
}); | ||
|
||
app.listen(port, () => { | ||
console.log(`Server running at http://localhost:${port}/`); | ||
}); |
Oops, something went wrong.