-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
405 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const asyncHandle = require('../middleware/asyncHandle'); | ||
const { | ||
Product | ||
} = require("../models"); | ||
|
||
exports.createProduct = asyncHandle(async (req, res) => { | ||
let { | ||
name, | ||
description, | ||
price, | ||
categoryId, | ||
stock, | ||
} = req.body; | ||
|
||
const file = req.file; | ||
|
||
if (!file) { | ||
res.status(400); | ||
throw new Error("No image uploaded"); | ||
} | ||
|
||
const fileName = file.filename; | ||
const pathFile = `${req.protocol}://${req.get("host")}/public/uploads/${fileName}`; | ||
|
||
const newProduct = await Product.create({ | ||
name, | ||
description, | ||
price, | ||
categoryId, | ||
stock, | ||
image: pathFile | ||
}); | ||
|
||
res.status(201).json({ | ||
status: "success", | ||
message: "Product added successfully", | ||
product: newProduct | ||
}); | ||
}); | ||
|
||
// Route for reading all products | ||
exports.getProducts = asyncHandle(async (req, res) => { | ||
const products = await Product.findAll(); | ||
return res.status(200).json({ | ||
data: products | ||
}); | ||
}); | ||
|
||
// Route for getting details of a single product by ID | ||
exports.getProductById = asyncHandle(async (req, res) => { | ||
const id = req.params.id; | ||
const productData = await Product.findByPk(id); | ||
|
||
if (!productData) { | ||
return res.status(404).json({ | ||
error: "Product not found" | ||
}); | ||
} | ||
|
||
return res.status(200).json({ | ||
data: productData | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
'use strict'; | ||
/** @type {import('sequelize-cli').Migration} */ | ||
module.exports = { | ||
async up(queryInterface, Sequelize) { | ||
await queryInterface.createTable('Products', { | ||
id: { | ||
allowNull: false, | ||
primaryKey: true, | ||
type: Sequelize.UUID, | ||
defaultValue: Sequelize.UUIDV4 | ||
}, | ||
name: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
unique: true | ||
}, | ||
description: { | ||
type: Sequelize.STRING | ||
}, | ||
price: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
|
||
}, | ||
categoryId: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
references: { | ||
model: 'Categories', | ||
key: 'id' | ||
}, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE' | ||
}, | ||
image: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
stock: { | ||
type: Sequelize.INTEGER, | ||
defaultValue: 0 | ||
}, | ||
countReview: { | ||
type: Sequelize.INTEGER, | ||
defaultValue: 0 | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}); | ||
}, | ||
async down(queryInterface, Sequelize) { | ||
await queryInterface.dropTable('Products'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
'use strict'; | ||
const { | ||
Model | ||
} = require('sequelize'); | ||
module.exports = (sequelize, DataTypes) => { | ||
class Product extends Model { | ||
/** | ||
* Helper method for defining associations. | ||
* This method is not a part of Sequelize lifecycle. | ||
* The `models/index` file will call this method automatically. | ||
*/ | ||
static associate(models) { | ||
// define association here | ||
} | ||
} | ||
Product.init({ | ||
id: { | ||
type: DataTypes.UUID, | ||
primaryKey: true, | ||
defaultValue: DataTypes.UUIDV4 | ||
}, | ||
name: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
unique: { | ||
args: true, | ||
msg: 'Nama sudah digunakan' | ||
}, | ||
validate: { | ||
notNull: { | ||
msg: 'Nama tidak boleh kosong' | ||
} | ||
} | ||
}, | ||
description: DataTypes.STRING, | ||
price: { | ||
type: DataTypes.INTEGER, | ||
allowNull: false, | ||
validate: { | ||
notNull: { | ||
msg: 'Harga tidak boleh kosong' | ||
}, | ||
isNumeric: { | ||
msg: 'Harga harus berupa angka' | ||
}, | ||
min: { | ||
args: [1], | ||
msg: 'Harga minimal 1' | ||
} | ||
} | ||
}, | ||
categoryId: { | ||
type: DataTypes.INTEGER, | ||
allowNull: false, | ||
references: { | ||
model: 'Categories', | ||
key: 'id' | ||
}, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
validate: { | ||
notNull: { | ||
msg: 'Kategori tidak boleh kosong' | ||
}, | ||
isExist(value) { | ||
return sequelize.models.Category.findByPk(value).then((el) => { | ||
if (!el) { | ||
throw new Error('Kategori tidak ditemukan') | ||
} | ||
}) | ||
} | ||
} | ||
}, | ||
image: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
validate: { | ||
notNull: { | ||
msg: 'Image tidak boleh kosong' | ||
} | ||
} | ||
}, | ||
stock: { | ||
type: DataTypes.INTEGER, | ||
defaultValue: 0, | ||
}, | ||
countReview: { | ||
type: DataTypes.INTEGER, | ||
defaultValue: 0, | ||
} | ||
}, { | ||
sequelize, | ||
modelName: 'Product', | ||
}); | ||
return Product; | ||
}; |
Oops, something went wrong.