diff --git a/.env b/.env deleted file mode 100644 index 2bd93a236ca19f454aad9525615ad6b08fd3f7bd..0000000000000000000000000000000000000000 --- a/.env +++ /dev/null @@ -1,7 +0,0 @@ -PORT=3000 -DB_HOST=localhost -DB_USER=root -DB_PASSWORD=EhMH2Baifs -DB_NAME=project -SESSION_SECRET=notesappsupersecretkey123 -TOKEN_SECRET=randomstringthisisexamplefortokensecret \ No newline at end of file diff --git a/config/database.js b/config/database.js index 374f16ad0925c39040e475c802685f4be2c8acb2..fba878ee4490c15f0468fc6480bef51f94ec59a2 100644 --- a/config/database.js +++ b/config/database.js @@ -2,10 +2,10 @@ const mysql = require("mysql"); require("dotenv/config"); var mysqlConnection = mysql.createConnection({ - host: process.env.DB_HOST1, - user: process.env.DB_USER1, - password: process.env.DB_PASSWORD1, - database: process.env.DB_NAME1, + host: process.env.DB_HOST, + user: process.env.DB_USER, + password: process.env.DB_PASSWORD, + database: process.env.DB_NAME, multipleStatements: true }); diff --git a/controllers/CategoryController.js b/controllers/CategoryController.js index 0e7202eaea56290a080f3494694d54e1f8ad0413..b1f6e83109691b18d359ed9ad1fc59392f51ae76 100644 --- a/controllers/CategoryController.js +++ b/controllers/CategoryController.js @@ -3,10 +3,10 @@ const Category = require('../Models/Category'); class CategoryController { // Display all categories (web) static index(req, res) { - Category.getByUserId(req.user.id, (err, categories) => { + Category.getAll((err, categories) => { if (err) { console.log(err); - return res.status(500).send('Error loading categories'); + return res.status(500).send('Database error'); } res.render('categories', { categories: categories, @@ -17,7 +17,7 @@ class CategoryController { // Get all categories (API) static apiIndex(req, res) { - Category.getByUserId(req.user.id, (err, categories) => { + Category.getAll((err, categories) => { if (err) { console.log(err); return res.status(500).json({ error: 'Database error' }); @@ -30,14 +30,13 @@ class CategoryController { static create(req, res) { const categoryData = { name: req.body.name, - description: req.body.description, - userId: req.user.id + description: req.body.description }; Category.create(categoryData, (err, result) => { if (err) { console.log(err); - return res.status(500).send('Error creating category'); + return res.status(500).send('Failed to create category'); } res.redirect('/categories'); }); @@ -47,66 +46,71 @@ class CategoryController { static apiCreate(req, res) { const categoryData = { name: req.body.name, - description: req.body.description, - userId: req.user.id + description: req.body.description }; Category.create(categoryData, (err, result) => { if (err) { console.log(err); - return res.status(500).json({ error: 'Error creating category' }); + return res.status(500).json({ error: 'Failed to create category' }); } - res.status(201).json({ - message: 'Category created successfully', - categoryId: result.insertId - }); + res.status(201).json(result); }); } // Update a category (web) static update(req, res) { - const categoryId = req.body.category_id; + const id = req.params.id || req.body.category_id; + console.log('Update Category - ID from params:', req.params.id); + console.log('Update Category - ID from body:', req.body.category_id); + console.log('Using ID:', id); + + if (!id) { + console.log('Error: No category_id provided!'); + return res.status(400).send('Category ID is required'); + } + const categoryData = { name: req.body.name, - description: req.body.description, - userId: req.user.id + description: req.body.description }; + console.log('Category Data:', categoryData); - Category.update(categoryId, categoryData, (err, result) => { + Category.update(id, categoryData, (err, result) => { if (err) { - console.log(err); - return res.status(500).send('Error updating category'); + console.log('Error updating category:', err); + return res.status(500).send('Failed to update category'); } + console.log('Category updated successfully'); res.redirect('/categories'); }); } // Update a category (API) static apiUpdate(req, res) { - const categoryId = req.params.id; + const id = req.params.id; const categoryData = { name: req.body.name, - description: req.body.description, - userId: req.user.id + description: req.body.description }; - Category.update(categoryId, categoryData, (err, result) => { + Category.update(id, categoryData, (err, result) => { if (err) { console.log(err); - return res.status(500).json({ error: 'Error updating category' }); + return res.status(500).json({ error: 'Failed to update category' }); } - res.json({ message: 'Category updated successfully' }); + res.json(result); }); } // Delete a category (web) static delete(req, res) { - const categoryId = req.params.id; - - Category.delete(categoryId, req.user.id, (err, result) => { + const id = req.params.id; + + Category.delete(id, (err, result) => { if (err) { console.log(err); - return res.status(500).send('Error deleting category'); + return res.status(500).send('Failed to delete category'); } res.redirect('/categories'); }); @@ -114,14 +118,14 @@ class CategoryController { // Delete a category (API) static apiDelete(req, res) { - const categoryId = req.params.id; - - Category.delete(categoryId, req.user.id, (err, result) => { + const id = req.params.id; + + Category.delete(id, (err, result) => { if (err) { console.log(err); - return res.status(500).json({ error: 'Error deleting category' }); + return res.status(500).json({ error: 'Failed to delete category' }); } - res.json({ message: 'Category deleted successfully' }); + res.json(result); }); } } diff --git a/controllers/NoteController.js b/controllers/NoteController.js index acd4f0e752d7e6dd8fa4d4927d5336704a1ac3a6..83bbba7f23ea209f64861adc0c8d19c17323c116 100644 --- a/controllers/NoteController.js +++ b/controllers/NoteController.js @@ -7,17 +7,17 @@ class NoteController { // แสดงหน้าแรกที่มีโน้ตทั้งหมด static async index(req, res) { // ดึงข้อมูลโน้ตทั้งหมด - Note.getAll(req.user.id, (err, notes) => { + Note.getAll((err, notes) => { if (err) { console.log(err); - return res.status(500).send('Error loading notes'); + return res.status(500).send('เกิดข้อผิดพลาดในการดึงข้อมูล'); } - // ดึงข้อมูล categories สำหรับ dropdown (เฉพาะของ user นี้) - Category.getByUserId(req.user.id, (err, categories) => { + // ดึงข้อมูล categories สำหรับ dropdown + Category.getAll((err, categories) => { if (err) { console.log(err); - return res.status(500).send('Error loading categories'); + return res.status(500).send('เกิดข้อผิดพลาดในการดึงข้อมูล'); } // ส่งข้อมูลไปแสดงที่หน้า dashboard @@ -32,7 +32,7 @@ class NoteController { // Get all notes (API) static apiIndex(req, res) { - Note.getAll(req.user.id, (err, notes) => { + Note.getAll((err, notes) => { if (err) { console.log(err); return res.status(500).json({ error: 'Database error' }); @@ -43,18 +43,21 @@ class NoteController { // สร้างโน้ตใหม่ static create(req, res) { + // เตรียมข้อมูลจากฟอร์ม const noteData = { title: req.body.title, content: req.body.content, - userId: req.user.id, + userId: req.user.id, // ได้จาก middleware authentication categoryId: req.body.category_id || null }; + // บันทึกลงฐานข้อมูล Note.create(noteData, (err, result) => { if (err) { console.log(err); - return res.status(500).send('Error creating note'); + return res.status(500).send('เกิดข้อผิดพลาดในการบันทึกโน้ต'); } + // กลับไปหน้า dashboard เมื่อบันทึกสำเร็จ res.redirect('/dashboard'); }); } @@ -71,61 +74,68 @@ class NoteController { Note.create(noteData, (err, result) => { if (err) { console.log(err); - return res.status(500).json({ error: 'Error creating note' }); + return res.status(500).json({ error: 'Failed to create note' }); } - res.status(201).json({ - message: 'Note created successfully', - noteId: result.insertId - }); + res.status(201).json(result); }); } // อัพเดทโน้ต static update(req, res) { - const noteId = req.body.note_id; + console.log('ข้อมูลที่ส่งมา:', req.body); + + // ตรวจสอบว่ามี note_id หรือไม่ + const id = req.body.note_id; + if (!id) { + console.log('ไม่พบ note_id'); + return res.status(400).json({ error: 'ต้องระบุ Note ID' }); + } + + // เตรียมข้อมูลสำหรับอัพเดท const noteData = { title: req.body.title, content: req.body.content, - categoryId: req.body.category_id || null, - userId: req.user.id + categoryId: req.body.category_id || null }; + console.log('ข้อมูลที่จะอัพเดท:', { id, noteData }); - Note.update(noteId, noteData, (err, result) => { + // อัพเดทลงฐานข้อมูล + Note.update(id, noteData, (err, result) => { if (err) { - console.log(err); - return res.status(500).send('Error updating note'); + console.log('เกิดข้อผิดพลาดในการอัพเดท:', err); + return res.status(500).json({ error: 'เกิดข้อผิดพลาดในการอัพเดทโน้ต', details: err.message }); } - res.redirect('/dashboard'); + console.log('อัพเดทสำเร็จ:', result); + res.json({ success: true, message: 'อัพเดทโน้ตสำเร็จ' }); }); } // Update a note (API) static apiUpdate(req, res) { - const noteId = req.params.id; + const id = req.params.id; const noteData = { title: req.body.title, content: req.body.content, - categoryId: req.body.category_id || null, - userId: req.user.id + categoryId: req.body.category_id || null }; - Note.update(noteId, noteData, (err, result) => { + Note.update(id, noteData, (err, result) => { if (err) { console.log(err); - return res.status(500).json({ error: 'Error updating note' }); + return res.status(500).json({ error: 'Failed to update note' }); } - res.json({ message: 'Note updated successfully' }); + res.json(result); }); } // ลบโน้ต static delete(req, res) { - const noteId = req.params.id; - - Note.delete(noteId, req.user.id, (err, result) => { + const id = req.params.id; + + Note.delete(id, (err, result) => { if (err) { console.log(err); - return res.status(500).send('Error deleting note'); + return res.status(500).send('เกิดข้อผิดพลาดในการลบโน้ต'); } res.redirect('/dashboard'); }); @@ -133,14 +143,14 @@ class NoteController { // Delete a note (API) static apiDelete(req, res) { - const noteId = req.params.id; - - Note.delete(noteId, req.user.id, (err, result) => { + const id = req.params.id; + + Note.delete(id, (err, result) => { if (err) { console.log(err); - return res.status(500).json({ error: 'Error deleting note' }); + return res.status(500).json({ error: 'Failed to delete note' }); } - res.json({ message: 'Note deleted successfully' }); + res.json(result); }); } @@ -152,13 +162,13 @@ class NoteController { return res.redirect('/dashboard'); } - Note.search(searchTerm, req.user.id, (err, notes) => { + Note.search(searchTerm, (err, notes) => { if (err) { console.log(err); return res.status(500).send('Database error'); } - Category.getByUserId(req.user.id, (err, categories) => { + Category.getAll((err, categories) => { if (err) { console.log(err); return res.status(500).send('Database error'); @@ -182,14 +192,14 @@ class NoteController { Note.getByUserId(userId, (err, notes) => { if (err) { console.log(err); - return res.status(500).send('Error loading notes'); + return res.status(500).send('เกิดข้อผิดพลาดในการดึงข้อมูล'); } - // ดึง categories สำหรับ dropdown (เฉพาะของ user นี้) - Category.getByUserId(userId, (err, categories) => { + // ดึง categories สำหรับ dropdown + Category.getAll((err, categories) => { if (err) { console.log(err); - return res.status(500).send('Error loading categories'); + return res.status(500).send('เกิดข้อผิดพลาดในการดึงข้อมูล'); } res.render('dashboard', { diff --git a/models/Category.js b/models/Category.js index 8959c7823255e82d953dd8e5cb2ce9e3265d9434..7851e65ba2a8532593e7677f5279ad2c993e37fd 100644 --- a/models/Category.js +++ b/models/Category.js @@ -4,25 +4,8 @@ class Category { // Get all categories static getAll(callback) { mysqlConnection.query( - `SELECT category.*, user.name as user_name - FROM category - LEFT JOIN user ON category.user_id = user.id - ORDER BY category.name`, - (err, rows) => { - if (err) { - return callback(err, null); - } - return callback(null, rows); - } - ); - } - - // Get categories by user - static getByUserId(userId, callback) { - mysqlConnection.query( - `SELECT * FROM category WHERE user_id = ? ORDER BY name`, - [userId], - (err, rows) => { + "SELECT * FROM category ORDER BY name", + (err, rows, fields) => { if (err) { return callback(err, null); } @@ -34,8 +17,8 @@ class Category { // Create a new category static create(categoryData, callback) { mysqlConnection.query( - "INSERT INTO category (name, description, user_id) VALUES (?, ?, ?)", - [categoryData.name, categoryData.description || null, categoryData.userId], + "INSERT INTO category (name, description) VALUES (?,?)", + [categoryData.name, categoryData.description || null], (err, result) => { if (err) { return callback(err, null); @@ -48,8 +31,8 @@ class Category { // Update a category static update(id, categoryData, callback) { mysqlConnection.query( - "UPDATE category SET name = ?, description = ? WHERE id = ? AND user_id = ?", - [categoryData.name, categoryData.description, id, categoryData.userId], + "UPDATE category SET name = ?, description = ? WHERE id = ?", + [categoryData.name, categoryData.description, id], (err, result) => { if (err) { return callback(err, null); @@ -60,10 +43,10 @@ class Category { } // Delete a category - static delete(id, userId, callback) { + static delete(id, callback) { mysqlConnection.query( - "DELETE FROM category WHERE id = ? AND user_id = ?", - [id, userId], + "DELETE FROM category WHERE id = ?", + [id], (err, result) => { if (err) { return callback(err, null); @@ -76,10 +59,7 @@ class Category { // Get category by id static getById(id, callback) { mysqlConnection.query( - `SELECT category.*, user.name as user_name - FROM category - LEFT JOIN user ON category.user_id = user.id - WHERE category.id = ?`, + "SELECT * FROM category WHERE id = ?", [id], (err, rows) => { if (err) { diff --git a/models/Note.js b/models/Note.js index 1eeed7b0cb1cb10aba40a51dffd5565716dedbd2..0418b12e8bc38550ea82c15bcf85b580a54c854b 100644 --- a/models/Note.js +++ b/models/Note.js @@ -2,16 +2,13 @@ const mysqlConnection = require('../config/database'); // Note Model - จัดการการทำงานกับฐานข้อมูลสำหรับโน้ต class Note { - // ดึงโน้ตทั้งหมดของผู้ใช้ - static getAll(userId, callback) { + // ดึงโน้ตทั้งหมด + static getAll(callback) { mysqlConnection.query( `SELECT note.*, user.name as user_name, category.name as category_name FROM note LEFT JOIN user ON note.user_id = user.id - LEFT JOIN category ON note.category_id = category.id - WHERE note.user_id = ? - ORDER BY note.created_at DESC`, - [userId], + LEFT JOIN category ON note.category_id = category.id`, (err, results) => { if (err) { return callback(err, null); @@ -41,8 +38,8 @@ class Note { // noteData ประกอบด้วย: title, content, categoryId static update(id, noteData, callback) { mysqlConnection.query( - "UPDATE note SET title = ?, content = ?, category_id = ? WHERE id = ? AND user_id = ?", - [noteData.title, noteData.content, noteData.categoryId, id, noteData.userId], + "UPDATE note SET title = ?, content = ?, category_id = ? WHERE id = ?", + [noteData.title, noteData.content, noteData.categoryId, id], (err, result) => { if (err) { return callback(err, null); @@ -54,10 +51,10 @@ class Note { // ลบโน้ต // id: รหัสของโน้ตที่ต้องการลบ - static delete(id, userId, callback) { + static delete(id, callback) { mysqlConnection.query( - "DELETE FROM note WHERE id = ? AND user_id = ?", - [id, userId], + "DELETE FROM note WHERE id = ?", + [id], (err, result) => { if (err) { return callback(err, null); @@ -74,8 +71,7 @@ class Note { `SELECT note.*, category.name as category_name FROM note LEFT JOIN category ON note.category_id = category.id - WHERE note.user_id = ? - ORDER BY note.created_at DESC`, + WHERE note.user_id = ?`, [userId], (err, results) => { if (err) { @@ -87,15 +83,15 @@ class Note { } // Search notes by title or content - static search(searchTerm, userId, callback) { + static search(searchTerm, callback) { let sql = `SELECT note.*, user.name as user_name, category.name as category_name FROM note LEFT JOIN user ON note.user_id = user.id LEFT JOIN category ON note.category_id = category.id - WHERE note.user_id = ? AND (note.title LIKE ? OR note.content LIKE ?) + WHERE note.title LIKE ? OR note.content LIKE ? ORDER BY note.created_at DESC`; - mysqlConnection.query(sql, [userId, `%${searchTerm}%`, `%${searchTerm}%`], (err, results) => { + mysqlConnection.query(sql, [`%${searchTerm}%`, `%${searchTerm}%`], (err, results) => { if (err) { return callback(err, null); } diff --git a/nodecrud.sql b/nodecrud.sql index 3bf1b5fa916ce081b0902db2e0e6f8d59ae9be99..5d0cf7ff0a776d39a4b8cf299a9b4b02a46fcb20 100644 --- a/nodecrud.sql +++ b/nodecrud.sql @@ -31,7 +31,6 @@ CREATE TABLE `category` ( `id` int(11) NOT NULL, `name` varchar(50) NOT NULL, `description` text DEFAULT NULL, - `user_id` int(11) NOT NULL, `created_at` timestamp NOT NULL DEFAULT current_timestamp() ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; @@ -73,8 +72,7 @@ CREATE TABLE `user` ( -- Indexes for table `category` -- ALTER TABLE `category` - ADD PRIMARY KEY (`id`), - ADD KEY `user_id` (`user_id`); + ADD PRIMARY KEY (`id`); -- -- Indexes for table `note` @@ -117,12 +115,6 @@ ALTER TABLE `user` -- Constraints for dumped tables -- --- --- Constraints for table `category` --- -ALTER TABLE `category` - ADD CONSTRAINT `category_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE; - -- -- Constraints for table `note` -- diff --git a/package.json b/package.json index e026be5c83fac2db922e2223f337600eb4af37c9..3ca07fb4755d9b774cb80413b07621e3f02f2eef 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "start": "node index.js" + "start": "nodemon index.js" }, "author": "", "license": "ISC", diff --git a/routes/apiRoute.js b/routes/apiRoute.js index ad2ce1c6bbef3cd1e5bd3dbd85721e0503474520..e9d02d993c214b0e2f33deb21ffe752dbf8fc01d 100644 --- a/routes/apiRoute.js +++ b/routes/apiRoute.js @@ -8,14 +8,14 @@ const jwt = require('jsonwebtoken'); // Middleware ตรวจสอบ API token แบบง่าย const apiAuth = (req, res, next) => { const token = req.header('auth-token'); - if (!token) return res.status(401).json({ error: 'Authentication required' }); + if (!token) return res.status(401).json({ error: 'กรุณาเข้าสู่ระบบก่อน' }); try { const verified = jwt.verify(token, process.env.TOKEN_SECRET); req.user = verified; next(); } catch (err) { - res.status(401).json({ error: 'Invalid token' }); + res.status(401).json({ error: 'Token ไม่ถูกต้อง' }); } }; diff --git a/routes/web.js b/routes/web.js index 7171e189bd113a8a0fd1d715c7ae1a987ab58cac..62e9015158b814875372dd61f425436de971d839 100644 --- a/routes/web.js +++ b/routes/web.js @@ -21,25 +21,43 @@ const webAuth = (req, res, next) => { } }; -// Routes สำหรับหน้าเว็บ -router.get('/', AuthController.showLoginPage); -router.get('/register', AuthController.showRegisterPage); -router.post('/register', AuthController.signup); -router.post('/login', AuthController.login); -router.post('/logout', AuthController.logout); - -// Routes สำหรับจัดการโน้ต -router.get('/dashboard', webAuth, NoteController.index); -router.get('/dashboard/my-notes', webAuth, NoteController.myNotes); -router.get('/dashboard/search', webAuth, NoteController.search); -router.post('/dashboard/create', webAuth, NoteController.create); -router.post('/dashboard/update', webAuth, NoteController.update); -router.get('/dashboard/:id/delete', webAuth, NoteController.delete); - -// Routes สำหรับจัดการหมวดหมู่ -router.get('/categories', webAuth, CategoryController.index); -router.post('/categories/create', webAuth, CategoryController.create); -router.post('/categories/update', webAuth, CategoryController.update); -router.get('/categories/:id/delete', webAuth, CategoryController.delete); +// Login routes +router.get("/", AuthController.showLoginPage); +router.post("/", AuthController.login); +router.post("/login", AuthController.login); + +// Register routes +router.get("/register", AuthController.showRegisterPage); +router.post("/register", AuthController.signup); +router.post("/signup", AuthController.signup); + +// Logout route +router.post("/logout", (req, res, next) => { + const token = req.cookies ? req.cookies['auth-token'] : null; + if (!token) { + return res.redirect('/'); + } + next(); +}, AuthController.logout); + +// Dashboard & Notes routes +router.get("/dashboard", webAuth, NoteController.index); +router.post("/dashboard", webAuth, NoteController.index); +router.get("/dashboard/search", webAuth, NoteController.search); +router.get("/dashboard/my-notes", webAuth, NoteController.myNotes); +router.post("/dashboard/create", webAuth, NoteController.create); +router.post("/dashboard/update", webAuth, NoteController.update); +router.post("/dashboard/update/:id", webAuth, NoteController.update); +router.post("/dashboard/:id/update", webAuth, NoteController.update); +router.get("/dashboard/:id/delete", webAuth, NoteController.delete); + +// Categories routes +router.get("/categories", webAuth, CategoryController.index); +router.post("/categories", webAuth, CategoryController.index); +router.post("/categories/create", webAuth, CategoryController.create); +router.post("/categories/update", webAuth, CategoryController.update); +router.post("/categories/update/:id", webAuth, CategoryController.update); +router.post("/categories/:id/update", webAuth, CategoryController.update); +router.get("/categories/:id/delete", webAuth, CategoryController.delete); module.exports = router; \ No newline at end of file diff --git a/views/categories.ejs b/views/categories.ejs index 0c65f333eab5dec3dd65765677c36657f7aeb274..2ec17ba3326640fc311d61fe2e23a7ec0ba3d990 100644 --- a/views/categories.ejs +++ b/views/categories.ejs @@ -89,7 +89,7 @@ </button> </div> <div class="modal-body"> - <form method="POST" id="edit-form" action="/categories/update" class="form-edit"> + <form method="POST" id="edit-form" action="" class="form-edit"> <input type="hidden" name="category_id" id="category_id_hidden" value=""> <div class="form-group"> <label for="name">Name</label> @@ -107,17 +107,13 @@ </div> <script> - // Set form action on page load - $(document).ready(function() { - $('#edit-form').attr('action', '/categories/update'); - }); - // Edit form $('.edit-btn').on('click', function(){ var id = $(this).data('id'); var name = $(this).data('name'); var description = $(this).data('description'); + $('#edit-form').attr('action', '/categories/update'); $('#category_id_hidden').val(id); $('#name').val(name); $('#description').val(description); diff --git a/views/dashboard.ejs b/views/dashboard.ejs index 6b49cabecd046c94e7c479e56ef34f4cc9940bf2..f81105e84c364247e57eaf7bd908bf2c0369855b 100644 --- a/views/dashboard.ejs +++ b/views/dashboard.ejs @@ -1,7 +1,7 @@ <%- include('./partials/header', {title: 'Dashboard'}) %> <div class="header-container"> - <h2 class="page-title">Note</h2> + <h2 class="page-title">Notes Dashboard</h2> <div class="action-buttons"> <a class='btn btn-info' data-toggle="modal" data-target="#addModal"> Add New Note @@ -56,7 +56,7 @@ <div class="card-header d-flex justify-content-between align-items-center"> <h5 class="card-title mb-0"><%= note.title %></h5> <span class="badge badge-<%= note.category_id ? 'info' : 'secondary' %>"> - <%= note.category_name || 'No Category' %> + <%= note.category_name || 'ไม่มีหมวดหมู่' %> </span> </div> <!-- เนื้อหาของการ์ด --> @@ -64,8 +64,8 @@ <p class="card-text"> <%= note.content.length > 150 ? note.content.substring(0, 150) + '...' : note.content %> </p> - <p class="card-author">Author: <%= note.user_name %></p> - <p class="card-date">Created: <%= new Date(note.created_at).toLocaleString() %></p> + <p class="card-author">ผู้เขียน: <%= note.user_name %></p> + <p class="card-date">สร้างเมื่อ: <%= new Date(note.created_at).toLocaleString() %></p> <!-- ปุ่มแก้ไขและลบ --> <div class="card-actions"> <a class="btn btn-sm btn-outline-info edit-btn" @@ -75,12 +75,12 @@ data-title="<%= note.title %>" data-content="<%= note.content %>" data-category="<%= note.category_id || '' %>"> - Edit + แก้ไข </a> <a class="btn btn-sm btn-outline-danger delete-btn" href="/dashboard/<%= note.id %>/delete" - onclick="return confirm('Are you sure you want to delete this note?')"> - Delete + onclick="return confirm('คุณแน่ใจหรือไม่ที่จะลบโน้ตนี้?')"> + ลบ </a> </div> </div> @@ -91,7 +91,7 @@ <% } else { %> <!-- แสดงข้อความเมื่อไม่มีโน้ต --> <div class="no-notes"> - <p class="text-center">No notes found</p> + <p class="text-center">ไม่พบโน้ต</p> </div> <% } %> </div> @@ -101,7 +101,7 @@ <div class="modal-dialog modal-lg" role="document"> <div class="modal-content"> <div class="modal-header"> - <h5 class="modal-title" id="addModalLabel">Add New Note</h5> + <h5 class="modal-title" id="addModalLabel">เพิ่มโน้ตใหม่</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> @@ -110,17 +110,17 @@ <!-- ฟอร์มเพิ่มโน้ตใหม่ --> <form method="POST" action="/dashboard/create" class="form-add"> <div class="form-group"> - <label for="title">Title</label> + <label for="title">หัวข้อ</label> <input name="title" type="text" class="form-control" required> </div> <div class="form-group"> - <label for="content">Content</label> + <label for="content">เนื้อหา</label> <textarea name="content" class="form-control" rows="5" required></textarea> </div> <div class="form-group"> - <label for="category_id">Category</label> + <label for="category_id">หมวดหมู่</label> <select name="category_id" class="form-control"> - <option value="">-- Select Category --</option> + <option value="">-- เลือกหมวดหมู่ --</option> <% if(categories && categories.length > 0) { %> <% categories.forEach((category) => { %> <option value="<%= category.id %>"><%= category.name %></option> @@ -128,7 +128,7 @@ <% } %> </select> </div> - <button type="submit" class="btn btn-success">Save</button> + <button type="submit" class="btn btn-success">บันทึก</button> </form> </div> </div> @@ -140,7 +140,7 @@ <div class="modal-dialog modal-lg" role="document"> <div class="modal-content"> <div class="modal-header"> - <h5 class="modal-title" id="editModalLabel">Edit Note</h5> + <h5 class="modal-title" id="editModalLabel">แก้ไขโน้ต</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> @@ -150,17 +150,17 @@ <form method="POST" id="edit-form" action="" class="form-edit"> <input type="hidden" name="note_id" id="note_id" value=""> <div class="form-group"> - <label for="title">Title</label> + <label for="title">หัวข้อ</label> <input name="title" type="text" id="title" class="form-control" required> </div> <div class="form-group"> - <label for="content">Content</label> + <label for="content">เนื้อหา</label> <textarea name="content" id="content" class="form-control" rows="5" required></textarea> </div> <div class="form-group"> - <label for="category_id">Category</label> + <label for="category_id">หมวดหมู่</label> <select name="category_id" id="category_id" class="form-control"> - <option value="">-- Select Category --</option> + <option value="">-- เลือกหมวดหมู่ --</option> <% if(categories && categories.length > 0) { %> <% categories.forEach((category) => { %> <option value="<%= category.id %>"><%= category.name %></option> @@ -168,7 +168,7 @@ <% } %> </select> </div> - <button type="submit" class="btn btn-success">Save Changes</button> + <button type="submit" class="btn btn-success">บันทึกการเปลี่ยนแปลง</button> </form> </div> </div> @@ -187,7 +187,7 @@ var content = $(this).data('content'); var category = $(this).data('category'); - console.log('Editing note:', {id, title, content, category}); + console.log('กำลังแก้ไขโน้ต:', {id, title, content, category}); // ใส่ข้อมูลลงในฟอร์ม $('#edit-form').attr('action', '/dashboard/update'); @@ -203,7 +203,7 @@ // เตรียมข้อมูลที่จะส่ง var formData = $(this).serialize(); - console.log('Sending data:', formData); + console.log('กำลังส่งข้อมูล:', formData); // ส่งข้อมูลด้วย AJAX $.ajax({ @@ -211,13 +211,13 @@ type: 'POST', data: formData, success: function(response) { - console.log('Update successful:', response); + console.log('อัพเดทสำเร็จ:', response); $('#editModal').modal('hide'); // ปิด modal location.reload(); // รีโหลดหน้าเพื่อแสดงข้อมูลใหม่ }, error: function(xhr, status, error) { - console.error('Error occurred:', {status, error, response: xhr.responseText}); - alert('Error updating note: ' + error + '\nPlease check console for more details'); + console.error('เกิดข้อผิดพลาด:', {status, error, response: xhr.responseText}); + alert('เกิดข้อผิดพลาดในการอัพเดทโน้ต: ' + error + '\nกรุณาตรวจสอบ console สำหรับรายละเอียดเพิ่มเติม'); } }); }); diff --git a/views/login.ejs b/views/login.ejs index 2645d4831188bde68210f3f0fc6e29ec05a0b694..f4613ccac0101c4de14d7a510f171846d51247d8 100644 --- a/views/login.ejs +++ b/views/login.ejs @@ -1,7 +1,7 @@ <%- include('./partials/header', {title: 'Login'}) %> <div class="container shadow p-5 mb-5 bg-white rounded"> - <h2 class="page-title mb-4">Greate note</h2> + <h2 class="page-title mb-4">Note Taking App</h2> <% if (typeof error !== 'undefined' && error) { %> <div class="alert alert-danger" role="alert"> diff --git a/views/partials/header.ejs b/views/partials/header.ejs index 4cf1b7351fd6cc95751a824ea59fc89fe5adae77..12339f6fbb310e3ce1a1e2b3b229d4edb609e975 100644 --- a/views/partials/header.ejs +++ b/views/partials/header.ejs @@ -14,6 +14,16 @@ body { background-color: #f5f5f5; } + .container { + width: 30rem; + margin: 0 auto; + margin-top: 5rem; + } + form { + margin: 0 auto !important; + margin-top: 0.7rem; + width: 100%; + } .page-title { color: #6c5ce7; font-weight: bolder; @@ -35,6 +45,17 @@ .action-buttons { display: flex; } + .search-container { + width: 70%; + margin: 1rem auto; + } + .search-form { + display: flex; + width: 100%; + } + .input-group-append .btn { + margin-left: 5px; + } .notes-container { width: 90%; margin: 2rem auto; @@ -74,11 +95,4 @@ } </style> </head> - <body> - <!-- Content will be injected here --> - <!-- jQuery first, then Popper.js, then Bootstrap JS --> - <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> - <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script> - <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script> - </body> -</html> \ No newline at end of file + <body> \ No newline at end of file