├── .gitignore ├── .vscode └── settings.json ├── README.md ├── jsconfig.json ├── next.config.js ├── package-lock.json ├── package.json ├── postcss.config.js ├── public ├── favicon.ico ├── react.svg └── vercel.svg ├── requests ├── ping.http └── tasks.http ├── src ├── app │ ├── api │ │ ├── ping │ │ │ └── route.js │ │ └── tasks │ │ │ ├── [id] │ │ │ └── route.js │ │ │ └── route.js │ ├── layout.jsx │ ├── not-found.jsx │ ├── page.jsx │ └── tasks │ │ ├── [id] │ │ └── page.jsx │ │ └── new │ │ └── page.jsx ├── components │ ├── Navbar.jsx │ └── TaskCard.jsx ├── models │ ├── Task.js │ └── User.js ├── styles │ └── globals.css └── utils │ └── mongoose.js └── tailwind.config.js /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # local env files 28 | .env 29 | .env.local 30 | .env.development.local 31 | .env.test.local 32 | .env.production.local 33 | 34 | # vercel 35 | .vercel 36 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.exclude": { 3 | "**/.git": true, 4 | "**/.svn": true, 5 | "**/.hg": true, 6 | "**/CVS": true, 7 | "**/.DS_Store": true, 8 | "**/Thumbs.db": true, 9 | "**/node_modules": true, 10 | } 11 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Nextjs & Mongodb CRUD 2 | 3 | A web aplication CRUD using Nodejs y Mongodb (with mongoose) 4 | 5 | ### Installation 6 | 7 | ``` 8 | cd next-mongodb-app 9 | npm i 10 | npm run dev 11 | ``` 12 | -------------------------------------------------------------------------------- /jsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "baseUrl": "./src", 4 | "paths": { 5 | "@/*": [ 6 | "*" 7 | ] 8 | } 9 | } 10 | } -------------------------------------------------------------------------------- /next.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('next').NextConfig} */ 2 | const nextConfig = { 3 | }; 4 | 5 | module.exports = nextConfig; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "next-mongodb-tasks", 3 | "version": "0.1.0", 4 | "private": true, 5 | "scripts": { 6 | "dev": "next dev", 7 | "build": "next build", 8 | "start": "next start" 9 | }, 10 | "dependencies": { 11 | "axios": "^1.4.0", 12 | "bcrypt": "^5.1.0", 13 | "cookie": "^0.5.0", 14 | "formik": "^2.4.2", 15 | "jose": "^4.14.4", 16 | "jsonwebtoken": "^9.0.1", 17 | "mongoose": "^7.4.1", 18 | "morgan": "^1.10.0", 19 | "next": "13.4.12", 20 | "react": "18.2.0", 21 | "react-dom": "18.2.0", 22 | "semantic-ui-css": "^2.5.0", 23 | "semantic-ui-react": "^2.1.4" 24 | }, 25 | "devDependencies": { 26 | "@types/react": "18.2.17", 27 | "autoprefixer": "^10.4.14", 28 | "eslint-config-next": "^13.4.12", 29 | "postcss": "^8.4.27", 30 | "tailwindcss": "^3.3.3" 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /postcss.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | } 7 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FaztWeb/nextjs-mongodb-crud/911c60adb1d5903e222dff588c9180137493ba30/public/favicon.ico -------------------------------------------------------------------------------- /public/react.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /public/vercel.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /requests/ping.http: -------------------------------------------------------------------------------- 1 | GET http://localhost:3000/api/ping -------------------------------------------------------------------------------- /requests/tasks.http: -------------------------------------------------------------------------------- 1 | @api = http://localhost:3000/api/tasks 2 | 3 | ### get tasks 4 | {{api}} 5 | 6 | ### create task 7 | POST {{api}} 8 | Content-Type: application/json 9 | 10 | { 11 | "title": "second task 2", 12 | "description": "second desc" 13 | } 14 | 15 | ### some validation 16 | POST {{api}} 17 | 18 | ### get single task 19 | GET {{api}}/64c347322fb76f285655e882 20 | 21 | ### Update a single task 22 | PUT {{api}}/64c347322fb76f285655e882 23 | Content-Type: application/json 24 | 25 | { 26 | "title": "I have to create a next app" 27 | } 28 | 29 | ### delete a single task 30 | DELETE {{api}}/64c347322fb76f285655e882 -------------------------------------------------------------------------------- /src/app/api/ping/route.js: -------------------------------------------------------------------------------- 1 | import { NextResponse } from "next/server"; 2 | 3 | export function GET() { 4 | return NextResponse.json({ hello: "world" }); 5 | } -------------------------------------------------------------------------------- /src/app/api/tasks/[id]/route.js: -------------------------------------------------------------------------------- 1 | import Task from "@/models/Task"; 2 | import { dbConnect } from "@/utils/mongoose"; 3 | import { NextResponse } from "next/server"; 4 | 5 | export async function GET(request, { params }) { 6 | dbConnect(); 7 | try { 8 | const taskFound = await Task.findById(params.id); 9 | 10 | if (!taskFound) 11 | return NextResponse.json( 12 | { 13 | message: "Task not found", 14 | }, 15 | { 16 | status: 404, 17 | } 18 | ); 19 | 20 | return NextResponse.json(taskFound); 21 | } catch (error) { 22 | return NextResponse.json(error.message, { 23 | status: 400, 24 | }); 25 | } 26 | } 27 | 28 | export async function PUT(request, { params }) { 29 | const body = await request.json(); 30 | dbConnect(); 31 | 32 | try { 33 | const taskUpdated = await Task.findByIdAndUpdate(params.id, body, { 34 | new: true, 35 | }); 36 | 37 | if (!taskUpdated) 38 | return NextResponse.json( 39 | { 40 | message: "Task not found", 41 | }, 42 | { 43 | status: 404, 44 | } 45 | ); 46 | 47 | return NextResponse.json(taskUpdated); 48 | } catch (error) { 49 | return NextResponse.json(error.message, { 50 | status: 400, 51 | }); 52 | } 53 | } 54 | 55 | export async function DELETE(request, { params }) { 56 | dbConnect(); 57 | 58 | try { 59 | const taskDeleted = await Task.findByIdAndDelete(params.id); 60 | 61 | if (!taskDeleted) 62 | return NextResponse.json( 63 | { 64 | message: "Task not found", 65 | }, 66 | { 67 | status: 404, 68 | } 69 | ); 70 | 71 | return NextResponse.json(taskDeleted); 72 | } catch (error) { 73 | return NextResponse.json(error.message, { 74 | status: 400, 75 | }); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/app/api/tasks/route.js: -------------------------------------------------------------------------------- 1 | import Task from "@/models/Task"; 2 | import { dbConnect } from "@/utils/mongoose"; 3 | import { NextResponse } from "next/server"; 4 | 5 | export async function GET() { 6 | await dbConnect(); 7 | const tasks = await Task.find(); 8 | return NextResponse.json(tasks); 9 | } 10 | 11 | export async function POST(request) { 12 | try { 13 | const body = await request.json(); 14 | const newTask = new Task(body); 15 | const savedTask = await newTask.save(); 16 | return NextResponse.json(savedTask); 17 | } catch (error) { 18 | return NextResponse.json(error.message, { 19 | status: 400, 20 | }); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/app/layout.jsx: -------------------------------------------------------------------------------- 1 | import { Navbar } from "components/Navbar"; 2 | import "../styles/globals.css"; 3 | 4 | export const metadata = { 5 | title: "NextMongo", 6 | description: "NextMongo is a simple app to manage tasks.", 7 | } 8 | 9 | function RootLayout({ children }) { 10 | return ( 11 | 12 |
13 |6 | Page not found :( 7 |
8 |{task.description}
9 |10 | Created at: 11 | {new Date(task.createdAt).toLocaleDateString()} 12 |
13 |