Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mercybassey committed Sep 10, 2023
0 parents commit decd254
Show file tree
Hide file tree
Showing 21 changed files with 2,312 additions and 0 deletions.
1 change: 1 addition & 0 deletions Backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.gitignore
1 change: 1 addition & 0 deletions Backend/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MONGO_URI=mongodb+srv://mercy:[email protected]/tododb
13 changes: 13 additions & 0 deletions Backend/Dockerfile
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"]
32 changes: 32 additions & 0 deletions Backend/Models/Todo.js
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);
57 changes: 57 additions & 0 deletions Backend/index.js
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}/`);
});
Loading

0 comments on commit decd254

Please sign in to comment.