Browse Source

First commit

master
mhmdarfiansyahp 1 week ago
commit
b73f6f44cb
  1. 7
      blog.rest
  2. 8
      config/database.js
  3. 65
      controllers/blogController.js
  4. 23
      index.js
  5. 24
      models/blogImage.js
  6. 17
      models/blogModel.js
  7. 1208
      package-lock.json
  8. 18
      package.json
  9. 19
      routes/index.js

7
blog.rest

@ -0,0 +1,7 @@
POST http://localhost:5000/blog/
Content-Type: application/json
{
"title": "GSI JAYA",
"description" : "lorem uasdasdadasdasdad"
}

8
config/database.js

@ -0,0 +1,8 @@
import { Sequelize } from "sequelize";
const db = new Sequelize('comproGSI', 'root', '',{
host: 'localhost',
dialect: 'mysql'
});
export default db;

65
controllers/blogController.js

@ -0,0 +1,65 @@
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 }]
});
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 }]
});
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) => {
const { title, description, images } = req.body;
try {
const blog = await blogModel.create({ title, description });
if (images && images.length > 0) {
await blogImage.bulkCreate(images.map(imageUrl => ({ blogId: blog.id, imageUrl })));
}
res.status(201).json(blog);
} catch (error) {
res.status(500).json({ message: error.message });
}
};
// 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);
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 });
}
};

23
index.js

@ -0,0 +1,23 @@
import express from "express";
import db from "./config/database.js";
import blogRoutes from "./routes/index.js"
// import Blog from "./models/blogModel.js";
// import BlogImage from "./models/blogImage.js";
import cors from "cors";
const app = express();
try {
// await db.sync();
await db.authenticate();
console.log('Database connected...');
} catch(error){
console.log('Connection error:', error);
};
app.use(cors());
app.use(express.json());
app.use ('/blog', blogRoutes);
app.listen(5000,() => console.log('server running at port 5000'));

24
models/blogImage.js

@ -0,0 +1,24 @@
import { Sequelize } from "sequelize";
import db from "../config/database.js";
const { DataTypes } = Sequelize;
const BlogImage = db.define("blog_images", {
blogId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: "blogs",
key: "id"
},
onDelete: "CASCADE"
},
imageUrl: {
type: DataTypes.STRING,
allowNull: false
}
}, {
freezeTableName: true
});
export default BlogImage;

17
models/blogModel.js

@ -0,0 +1,17 @@
import { Sequelize } from "sequelize";
import db from "../config/database.js";
const { DataTypes } = Sequelize;
const Blog = db.define('blogs', {
title:{
type: DataTypes.STRING
},
description:{
type: DataTypes.TEXT
}
},{
freezeTableName:true
});
export default Blog;

1208
package-lock.json
File diff suppressed because it is too large
View File

18
package.json

@ -0,0 +1,18 @@
{
"name": "becompro",
"version": "1.0.0",
"description": "",
"license": "ISC",
"author": "arfan",
"type": "module",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"cors": "^2.8.5",
"express": "^4.21.2",
"mysql2": "^3.12.0",
"sequelize": "^6.37.5"
}
}

19
routes/index.js

@ -0,0 +1,19 @@
import express from "express";
import {
getAllBlogs,
getBlogById,
createBlog,
updateBlog,
deleteBlog
} from "../controllers/blogController.js"
const router = express.Router();
router.get('/', getAllBlogs);
router.get('/:id', getBlogById);
router.post('/', createBlog);
router.patch('/:id', updateBlog);
router.delete('/:id', deleteBlog);
export default router;
Loading…
Cancel
Save