diff --git a/controllers/bookController.js b/controllers/bookController.js index 3b799f86cfd1dd1c7496ce9ef8cd3908641d3623..920a46703dbb8ca8d18f1052cbd42fa7726e8602 100644 --- a/controllers/bookController.js +++ b/controllers/bookController.js @@ -2,6 +2,28 @@ const BookModel = require('../models/bookModel'); const multer = require('multer'); const path = require('path'); +// Configure multer for file uploads +const storage = multer.diskStorage({ + destination: (req, file, cb) => { + cb(null, 'public/uploads/') + }, + filename: (req, file, cb) => { + cb(null, `${Date.now()}-${file.originalname}`) + } +}); + +const upload = multer({ + storage, + limits: { fileSize: 5 * 1024 * 1024 }, + fileFilter: (req, file, cb) => { + const filetypes = /jpeg|jpg|png/; + const mimetype = filetypes.test(file.mimetype); + const extname = filetypes.test(path.extname(file.originalname).toLowerCase()); + if (mimetype && extname) return cb(null, true); + cb(new Error('Only .png, .jpg and .jpeg format allowed!')); + } +}).single('image_file'); + exports.getBookLists = async (req, res) => { if (!req.session.user) { return res.redirect('/login'); @@ -122,19 +144,31 @@ exports.getAddBook = async (req, res) => { }; exports.postBook = async (req, res) => { - if (!req.session.user) { - return res.redirect('/login'); - } + if (!req.session.user) return res.redirect('/login'); - const { bookname, author, category, type, list_id, image_url } = req.body; - const bookModel = new BookModel(req.db); + upload(req, res, async (err) => { + if (err) { + console.error('Upload error:', err); + return res.status(400).send('Error uploading file: ' + err.message); + } - try { - await bookModel.addBook(bookname, author, category, type, list_id, req.session.user.user_id, image_url); - res.redirect(`/books/${list_id}`); - } catch (error) { - res.status(500).send('เกิดข้อผิดพลาดในการเพิ่มหนังสือ'); - } + try { + const { bookname, author, category, type, list_id, imageOption } = req.body; + const image_url = imageOption === 'file' && req.file ? + `/uploads/${req.file.filename}` : req.body.image_url; + + const bookModel = new BookModel(req.db); + await bookModel.createBook({ + bookname, author, category, type, image_url, list_id, + user_id: req.session.user.user_id + }); + + res.redirect(`/books/${list_id}`); + } catch (error) { + console.error('Error creating book:', error); + res.status(500).send('เกิดข้อผิดพลาดในการเพิ่มหนังสือ'); + } + }); }; exports.getEditBook = async (req, res) => { @@ -251,70 +285,4 @@ exports.getBookInfo = async (req, res) => { } catch (error) { res.status(500).send('Error loading book information'); } -}; -// Configure multer for file uploads -const storage = multer.diskStorage({ - destination: (req, file, cb) => { - cb(null, 'public/uploads/') - }, - filename: (req, file, cb) => { - cb(null, `${Date.now()}-${file.originalname}`) - } -}); - -const upload = multer({ - storage: storage, - limits: { - fileSize: 5 * 1024 * 1024 // 5MB limit - }, - fileFilter: (req, file, cb) => { - const filetypes = /jpeg|jpg|png/; - const mimetype = filetypes.test(file.mimetype); - const extname = filetypes.test(path.extname(file.originalname).toLowerCase()); - - if (mimetype && extname) { - return cb(null, true); - } - cb(new Error('Only .png, .jpg and .jpeg format allowed!')); - } -}).single('image_file'); - -exports.postBook = async (req, res) => { - if (!req.session.user) { - return res.redirect('/login'); - } - - upload(req, res, async (err) => { - if (err) { - console.error('Upload error:', err); - return res.status(400).send('Error uploading file: ' + err.message); - } - - try { - const { bookname, author, category, type, list_id, imageOption } = req.body; - let image_url = ''; - - if (imageOption === 'file' && req.file) { - image_url = `/uploads/${req.file.filename}`; - } else { - image_url = req.body.image_url; - } - - const bookModel = new BookModel(req.db); - await bookModel.createBook({ - bookname, - author, - category, - type, - image_url, - list_id, - user_id: req.session.user.user_id - }); - - res.redirect(`/books/${list_id}`); - } catch (error) { - console.error('Error creating book:', error); - res.status(500).send('เกิดข้อผิดพลาดในการเพิ่มหนังสือ'); - } - }); }; \ No newline at end of file