123 lines
3.3 KiB
123 lines
3.3 KiB
import blogModel from "../models/blogModel.js";
|
|
import blogImage from "../models/blogImage.js";
|
|
|
|
// Get all blogs with images
|
|
export const getAllBlogs = async (req, res) => {
|
|
try {
|
|
const blogs = await blogModel.findAll({
|
|
include: [{ model: blogImage, as: "images" }], // Tambahkan alias "images"
|
|
});
|
|
res.json(blogs);
|
|
} catch (error) {
|
|
res.status(500).json({ message: error.message });
|
|
}
|
|
};
|
|
|
|
// Get a single blog by ID
|
|
export const getBlogById = async (req, res) => {
|
|
try {
|
|
const blog = await blogModel.findByPk(req.params.id, {
|
|
include: [{ model: blogImage, as: "images" }], // Tambahkan alias "images"
|
|
});
|
|
if (!blog) return res.status(404).json({ message: "Blog not found" });
|
|
res.json(blog);
|
|
} catch (error) {
|
|
res.status(500).json({ message: error.message });
|
|
}
|
|
};
|
|
|
|
// 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;
|
|
|
|
if (!req.files || req.files.length === 0) {
|
|
return res.status(400).json({ message: "Minimal 1 gambar diperlukan!" });
|
|
}
|
|
|
|
const slug = generateSlug(title); // 🔥 generate slug di sini
|
|
|
|
const blog = await blogModel.create({ title, description, slug });
|
|
|
|
const imagePaths = req.files.map((file) => ({
|
|
blogId: blog.id,
|
|
imageUrl: `/uploads/${file.filename}`,
|
|
}));
|
|
|
|
await blogImage.bulkCreate(imagePaths);
|
|
|
|
const newBlog = await blogModel.findByPk(blog.id, {
|
|
include: [{ model: blogImage, as: "images" }],
|
|
});
|
|
|
|
res.status(201).json({
|
|
message: "Blog created successfully",
|
|
blog: newBlog,
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({ message: error.message });
|
|
}
|
|
};
|
|
|
|
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" });
|
|
|
|
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 });
|
|
}
|
|
};
|
|
|
|
// Delete a blog
|
|
export const deleteBlog = 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.destroy();
|
|
res.json({ message: "Blog deleted successfully" });
|
|
} catch (error) {
|
|
res.status(500).json({ message: error.message });
|
|
}
|
|
};
|