Add Brand Section

This commit is contained in:
Swift
2024-02-08 12:40:53 +03:30
parent a271dc0c90
commit 35407ffc1d
7 changed files with 210 additions and 28 deletions
+138
View File
@@ -0,0 +1,138 @@
const Brand = require("../models/Brand");
const { checkValidations } = require("../plugins/HelperFunctions")
const { body, validationResult } = require('express-validator')
const { _sr } = require("../plugins/resServer")
//@desc Create a brand
//@route POST /api/brands/
//@access Private
const createbrand = [
[
body('title').notEmpty().withMessage(_sr.fa.required.title),
body('description').notEmpty().withMessage(_sr.fa.required.description),
body('link').notEmpty().trim().isLowercase().withMessage(_sr.fa.required.field),
body('logo').notEmpty().withMessage(_sr.fa.required.image),
],
checkValidations(validationResult),
async (req, res) => {
const { title, link, description, logo } = req.body;
const linkAvailable = await Brand.findOne({ link });
if (linkAvailable) {
res.status(400);
throw new Error(_sr.fa.duplicated.link);
}
const brand = await Brand.create({
title,
link,
description,
logo,
});
res.status(201).json(brand);
}
]
//@desc Get to all brands
//@route GET /api/brands/
//@access Public
const getbrands = async (req, res) => {
const brand = await Brand.find();
res.status(200).json(brand);
};
//@desc Get to brand by id
//@route GET /api/brands/id/:id
//@access Public
const getbrand = async (req, res) => {
const brand = await Brand.findById(req.params.id);
if (!brand) {
res.status(404)
throw new Error(_sr.fa.not_found.item_id)
}
res.status(200).json(brand);
};
//@desc Update a brand
//@route Put /api/brands/:id
//@access Private
const updatebrand = [
[
body('title').notEmpty().withMessage(_sr.fa.required.title),
body('description').notEmpty().withMessage(_sr.fa.required.description),
body('link').notEmpty().trim().isLowercase().withMessage(_sr.fa.required.field),
body('logo').notEmpty().withMessage(_sr.fa.required.image),
],
checkValidations(validationResult),
async (req, res) => {
const { title, link, description, logo, native, ordering } = req.body;
const brand = await Brand.findById(req.params.id);
if (!brand) {
res.status(404);
throw new Error(_sr.fa.not_found.item_id);
}
const linkAvailable = await Brand.findOne({ link });
if (linkAvailable && linkAvailable.id != req.params.id) {
res.status(400);
throw new Error(_sr.fa.duplicated.link);
}
const updateBrand = await Brand.findByIdAndUpdate(
req.params.id,
{
title,
link,
description,
logo,
native,
ordering
},
{ new: true }
);
res.status(201).json(updateBrand);
}
]
//@desc Delete a brand
//@route DELETE /api/brands/:id
//@access Private
const deletebrand = async (req, res) => {
const brand = await Brand.findById(req.params.id);
if (!brand) {
res.status(404);
throw new Error(_sr.fa.not_found.item_id);
}
await brand.deleteOne();
res.status(201).json(brand);
};
module.exports = {
createbrand,
getbrands,
getbrand,
updatebrand,
deletebrand,
};
+2
View File
@@ -13,6 +13,7 @@ function init() {
const fileUpload = require('express-fileupload')
const authentication = require('./authentication')
const { checkThirdPartyAccess } = require('./plugins/checkThirdPartyAccess')
const { rs } = require('./plugins/resHeader')
// const { getVerityToken, getPanatechToken } = require('./ArpaWebService')
const cronJobs = require('./plugins/cronJobs')
const router = express.Router()
@@ -24,6 +25,7 @@ function init() {
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(fileUpload())
app.use(rs)
console.log('🟢 ~ Node Server is running ')
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
+17
View File
@@ -0,0 +1,17 @@
const mongoose = require('mongoose');
const brandSchema = new mongoose.Schema({
title: String,
link: {
type: String,
unique: [true, "This link already taken"]
},
description: String,
logo: String,
},
{
timestamps: true,
}
);
module.exports = mongoose.model('Brand', brandSchema);
+13
View File
@@ -0,0 +1,13 @@
module.exports.rs=(req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, siteuser');
res.setHeader('Author', "Mr Swift")
res.setHeader('X-Powered-By', "77 114 32 83 119 105 102 116")
res.setHeader(
"Strict-Transport-Security",
"max-age=31536000; includeSubDomains; preload"
);
next();
}