|
|
@ -26,20 +26,35 @@ export const getBlogById = async (req, res) => { |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
// Get blog by slug
|
|
|
|
export const getBlogBySlug = async (req, res) => { |
|
|
|
try { |
|
|
|
const blog = await blogModel.findOne({ |
|
|
|
where: { slug: req.params.slug }, |
|
|
|
include: [{ model: blogImage, as: "images" }], |
|
|
|
}); |
|
|
|
|
|
|
|
if (!blog) return res.status(404).json({ message: "Blog not found" }); |
|
|
|
|
|
|
|
res.json(blog); |
|
|
|
} catch (error) { |
|
|
|
res.status(500).json({ message: error.message }); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
// Create a new blog
|
|
|
|
export const createBlog = async (req, res) => { |
|
|
|
try { |
|
|
|
const { title, description } = req.body; |
|
|
|
|
|
|
|
// Pastikan ada file yang diunggah
|
|
|
|
if (!req.files || req.files.length === 0) { |
|
|
|
return res.status(400).json({ message: "Minimal 1 gambar diperlukan!" }); |
|
|
|
} |
|
|
|
|
|
|
|
// Simpan data blog
|
|
|
|
const blog = await blogModel.create({ title, description }); |
|
|
|
const slug = generateSlug(title); // 🔥 generate slug di sini
|
|
|
|
|
|
|
|
const blog = await blogModel.create({ title, description, slug }); |
|
|
|
|
|
|
|
// Simpan gambar ke database
|
|
|
|
const imagePaths = req.files.map((file) => ({ |
|
|
|
blogId: blog.id, |
|
|
|
imageUrl: `/uploads/${file.filename}`, |
|
|
@ -47,9 +62,8 @@ export const createBlog = async (req, res) => { |
|
|
|
|
|
|
|
await blogImage.bulkCreate(imagePaths); |
|
|
|
|
|
|
|
// Ambil data blog beserta gambar setelah disimpan
|
|
|
|
const newBlog = await blogModel.findByPk(blog.id, { |
|
|
|
include: [{ model: blogImage, as: "images" }], // Pastikan alias sesuai
|
|
|
|
include: [{ model: blogImage, as: "images" }], |
|
|
|
}); |
|
|
|
|
|
|
|
res.status(201).json({ |
|
|
@ -61,12 +75,36 @@ export const createBlog = async (req, res) => { |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
const generateSlug = (title) => { |
|
|
|
return title |
|
|
|
.toLowerCase() |
|
|
|
.replace(/\s+/g, "-") |
|
|
|
.replace(/[^\w-]+/g, ""); |
|
|
|
}; |
|
|
|
|
|
|
|
// Update a blog
|
|
|
|
export const updateBlog = async (req, res) => { |
|
|
|
try { |
|
|
|
const blog = await blogModel.findByPk(req.params.id); |
|
|
|
if (!blog) return res.status(404).json({ message: "Blog not found" }); |
|
|
|
await blog.update(req.body); |
|
|
|
|
|
|
|
const { title, description, slug } = req.body; |
|
|
|
|
|
|
|
// Tentukan slug baru (jika ada title baru dan tidak ada slug manual)
|
|
|
|
let updatedSlug = blog.slug; // default: slug lama
|
|
|
|
if (title && !slug) { |
|
|
|
updatedSlug = generateSlug(title); |
|
|
|
} else if (slug) { |
|
|
|
updatedSlug = slug; |
|
|
|
} |
|
|
|
|
|
|
|
// Update data
|
|
|
|
await blog.update({ |
|
|
|
title: title ?? blog.title, |
|
|
|
description: description ?? blog.description, |
|
|
|
slug: updatedSlug, |
|
|
|
}); |
|
|
|
|
|
|
|
res.json(blog); |
|
|
|
} catch (error) { |
|
|
|
res.status(500).json({ message: error.message }); |
|
|
|
xxxxxxxxxx