Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 51 additions & 51 deletions src/app/components/createTodo.jsx
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
import { useState } from "react";

import { addTodo, getTodos } from "../apis";
import { addTodo } from "../apis";

export function CreateTodo() {
const [data, setData] = useState({ title: "", description: "" });
const [data, setData] = useState({ title: "", description: "" });

function handleChange(e) {
setData((data) => ({ ...data, [e.target.name]: e.target.value }));
}
function handleChange(e) {
setData((data) => ({ ...data, [e.target.name]: e.target.value }));
}

async function handleSubmit(e) {
e.preventDefault();
await addTodo(data);
setData({ title: "", description: "" });
}
async function handleSubmit(e) {
e.preventDefault();
await addTodo(data);
setData({ title: "", description: "" });
}

return (
<section className="container">
<section className="contents">
<h1>ADD TODOS</h1>
<section className="contents" data-testid="container">
<form onSubmit={handleSubmit} className="form-container" noValidate>
<label className="label" htmlFor="title">
Title
</label>
<input
type="text"
name="title"
value={data.title}
onChange={handleChange}
className="input"
data-testid="title-input"
/>
<label className="label" htmlFor="description">
Description
</label>
<input
type="text"
name="description"
value={data.description}
onChange={handleChange}
data-testid="description-input"
className="input"
/>
<button
type="submit"
className="button"
data-testid="submit-button"
>
create todo
</button>
</form>
</section>
</section>
</section>
);
}
return (
<section className="container">
<section className="contents">
<h1>ADD TODOS</h1>
<section className="contents" data-testid="container">
<form onSubmit={handleSubmit} className="form-container" noValidate>
<label className="label" htmlFor="title">
Title
</label>
<input
type="text"
name="title"
value={data.title}
onChange={handleChange}
className="input"
data-testid="title-input"
/>
<label className="label" htmlFor="description">
Description
</label>
<input
type="text"
name="description"
value={data.description}
onChange={handleChange}
data-testid="description-input"
className="input"
/>
<button
type="submit"
className="button"
data-testid="submit-button"
>
create todo
</button>
</form>
</section>
</section>
</section>
);
}
2 changes: 1 addition & 1 deletion src/server/config/db.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const mongoose = require("mongoose");

const db = "mongodb://120.0.0.1:27017/todo_app"
const db = "mongodb://127.0.0.1:27017/todo_app"

const connectDB = async () => {
try {
Expand Down
22 changes: 21 additions & 1 deletion src/server/controllers/todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ exports.deleteTodo = (req, res) => {
res.json({ message: "todo deleted successfully", data });
})
.catch((err) => {
res.status(404).json({ error: "book not found", message: err.message });
res.status(404).json({ error: "todo not found", message: err.message });
});
};

exports.deleteTodo = (req, res) => {
Todo.findByIdAndRemove(req.params.id, req.body)
.then((data) => {
res.json({ message: "todo deleted successfully", data });
})
.catch((err) => {
res.status(404).json({ error: "todo not found", message: err.message });
});
};

exports.completeTodo = (req, res, data) => {
Todo.findByIdAndUpdate(req._id, { updated: req.body.updated })
.then(() => {
res.json({ message: "todo status updated successfully", data });
})
.catch((err) => {
res.status(404).json({ error: "todo not found", message: err.message });
});
};
18 changes: 11 additions & 7 deletions src/server/models/todo.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
const mongoose = require("mongoose");

const TodoSchema = new mongoose.Schema({
title: {
type: String,
required: true,
},
description: {
type: String,
},
title: {
type: String,
required: true,
},
description: {
type: String,
},
completedStatus: {
type: Number,
default: false,
},
});

const Todo = mongoose.model("todo", TodoSchema);
Expand Down
8 changes: 4 additions & 4 deletions src/server/routes/todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ const express = require("express");
const router = express.Router();

const {
getAllTodo,
postCreateTodo,
putUpdateTodo,
deleteTodo,
getAllTodo,
postCreateTodo,
putUpdateTodo,
deleteTodo,
} = require("../controllers/todo");

/**
Expand Down