Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backend : CLOUDNARY connected, Frontend Register new Prod with img an… #26

Merged
merged 1 commit into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ app.use(

// routes
app.use("/greeting", greeting);
app.use("/", product);
app.use("/seller", product);
app.use("/user", user);
app.use("/admin", admin);
app.use("/product-order", product_order);
Expand Down
9 changes: 9 additions & 0 deletions backend/config/cloudinaryConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import cloudinary from "cloudinary";

cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET,
});

export default cloudinary;
51 changes: 46 additions & 5 deletions backend/controllers/product.controller.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import multer from "multer";
import cloudinary from "../config/cloudinaryConfig.js";
import { Product } from "../models/product.model.js";
import catchAysncErrors from "../middleware/catchAysncErrors.js";
import ErrorHander from "../utils/error-handler.js";
Expand Down Expand Up @@ -38,12 +40,51 @@ export const getProductDetails = catchAysncErrors(async (req, res, next) => {

// Create a Product -------ADMIN routes

// Configure Multer for file handling
const storage = multer.memoryStorage();
const upload = multer({ storage });

export const createProduct = catchAysncErrors(async (req, res, next) => {
req.body.user = req.user.id;
const newProduct = await Product.create(req.body);
res.status(201).json({
success: true,
newProduct,
upload.array("images")(req, res, async (err) => {
if (err) {
return next(new ErrorHander("Image upload failed", 500));
}
let images = req.files;
if (!images || images.length === 0) {
return next(new ErrorHander("Images are required", 404));
}

const imagesLinks = [];

for (let i = 0; i < images.length; i++) {
try {
const result = await new Promise((resolve, reject) => {
const uploadStream = cloudinary.v2.uploader.upload_stream(
{ folder: "products-images" },
(error, result) => {
if (error) reject(error);
else resolve(result);
}
);
uploadStream.end(images[i].buffer);
});

imagesLinks.push({
public_id: result.public_id,
url: result.secure_url,
});
} catch (error) {
return next(new ErrorHander(`Error uploading image ${i}`, 500));
}
}

req.body.images = imagesLinks;
req.body.user = req.user.id;
const newProduct = await Product.create(req.body);
res.status(201).json({
success: true,
newProduct,
});
});
});

Expand Down
22 changes: 22 additions & 0 deletions backend/controllers/user-controller/profile.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,28 @@ export const registerUser = catchAysncErrors(async (req, res, next) => {
});
sendToken(user, 201, res);
});
// user Register
export const registerSellerUser = catchAysncErrors(async (req, res, next) => {
const { name, email, password, address, mobile_no, location } = req.body;

const user = await User.create({
name,
email,
password,
address,
mobile_no,
location: {
longitude: location.longitude,
latitude: location.latitude,
},
avatar: {
public_id: "this is a sample id",
url: "profilepicUrl",
},
role: "seller",
});
sendToken(user, 201, res);
});

// Get User Profile
export const getUserProfile = catchAysncErrors(async (req, res, next) => {
Expand Down
22 changes: 0 additions & 22 deletions backend/example.env

This file was deleted.

21 changes: 21 additions & 0 deletions backend/models/user.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ const userSchema = new mongoose.Schema({
required: true,
},
},

address: {
type: String,
default: "",
},
mobile_no: {
type: Number,
minLength: [10, "Mobile Number should be greater than 10 characters"],
},
location: {
longitude: {
type: String,
required: true,
default: "0.0",
},
latitude: {
type: String,
required: true,
default: "0.0",
},
},
role: {
type: String,
default: "user",
Expand Down
2 changes: 1 addition & 1 deletion backend/routes/product.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const router = express();

router.get("/products", getAllProductDetails);
router.post(
"/product",
"/new-products",
isAuthenticatedUser,
authorizeRole("admin"),
createProduct
Expand Down
2 changes: 2 additions & 0 deletions backend/routes/user.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "../controllers/user-controller/auth.controller.js";
import {
getUserProfile,
registerSellerUser,
registerUser,
updateUserProfile,
} from "../controllers/user-controller/profile.controller.js";
Expand All @@ -17,6 +18,7 @@ const router = express();

// profile
router.post("/register", registerUser);
router.post("/seller/register", registerSellerUser);
router.get("/me", isAuthenticatedUser, getUserProfile);

// auth
Expand Down
26 changes: 26 additions & 0 deletions example.development.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

# DATABASE CONNECTION
SERVER_PORT=5000
SERVER_MODE="development"


# DATABASE CONNECTION
DB_URI=mongodb://127.0.0.1:27017
DB_NAME=

# JWT AND COOKIE INFORMATION
JWT_EXPIRE=
JWT_SECRET=
COOKIE_EXPIRE=

# MAIL INFORMATION
SMTP_MAIL=
SMTP_PASSWORD=
SMTP_SERVICE="gmail"
SMTP_HOST="smtp.gmail.com"
SMTP_PORT=

# CLOUDINARY INFO
CLOUDINARY_CLOUD_NAME=
CLOUDINARY_API_KEY=
CLOUDINARY_API_SECRET=
1 change: 1 addition & 0 deletions frontend/example.development.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REACT_APP_BASE_URI = "http://localhost:5000/api/v1"
Loading
Loading