Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrupatel031205 committed Nov 19, 2024
0 parents commit 17dd423
Show file tree
Hide file tree
Showing 37 changed files with 20,777 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Getting Started with Create React App

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in your browser.

The page will reload when you make changes.\
You may also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can't go back!**

If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.

You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).

### Code Splitting

This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)

### Analyzing the Bundle Size

This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)

### Making a Progressive Web App

This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)

### Advanced Configuration

This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)

### Deployment

This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)

### `npm run build` fails to minify

This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
4 changes: 4 additions & 0 deletions backend/Models/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import mongoose from "mongoose";


module.exports = mongoose.model('user',UserSchema);
29 changes: 29 additions & 0 deletions backend/Routes/CreateUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import express from "express";
import { User } from "../Models/User";
import { body, validationResult } from "express-validator";
const router = express.Router();

const user = User;
router.post(
"/createuser",
(req, res) => {
const error = validationResult(req);
if (!error.isEmpty()) {
return res.status(400).json({ error: error.array() });
}
try {
user.create({
name: "Shyam",
password: "123456",
email: "[email protected]",
location: "Qwerty",
});
res.json({ success: true });
} catch (error) {
console.log(error);
res.json({ success: false });
}
}
);

module.exports = router;
Empty file added backend/db.js
Empty file.
154 changes: 154 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import express from "express";
import mongoose from "mongoose";

const app = express();
const port = 5000;

// MongoDB connection string
const MONGO_URI = "mongodb+srv://dhruv:[email protected]/gofoodmern";

// Middleware
app.use(express.json());
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
next();
});

// MongoDB Connection
mongoose
.connect(MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("Connected to MongoDB"))
.catch((err) => {
console.error("Error connecting to MongoDB:", err.message);
process.exit(1);
});

// Global variables to store data
global.food_items = [];
global.foodCatagory = [];

// Fetch food items and categories on server start
mongoose.connection.once("open", async () => {
try {
const foodItemsCollection = mongoose.connection.db.collection("food_items");
const foodCategoryCollection = mongoose.connection.db.collection("foodCatagory");

global.food_items = await foodItemsCollection.find({}).toArray();
global.foodCatagory = await foodCategoryCollection.find({}).toArray();

if (global.food_items.length === 0) {
console.log("No data found in 'food_items' collection");
} else {
console.log("Fetched food items:", global.food_items);
}

if (global.foodCatagory.length === 0) {
console.log("No data found in 'food_categories' collection");
} else {
console.log("Fetched food categories:", global.foodCatagory);
}
} catch (error) {
console.error("Error fetching initial data:", error.message);
}
});

// Mongoose Schema and Model for User
const UserSchema = new mongoose.Schema({
name: { type: String, required: true },
location: { type: String, required: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
date: { type: Date, default: Date.now },
});
const User = mongoose.model("User", UserSchema);

// Routes
const router = express.Router();

// Route: Fetch Food Data
router.get("/foodData", (req, res) => {
try {
if (global.food_items && global.foodCatagory) {
res.json([global.food_items, global.foodCatagory]);
} else {
res.status(404).json({ error: "Food data not found" });
}
} catch (error) {
console.error("Error in /foodData:", error.message);
res.status(500).json({ error: "Server error" });
}
});

// Route: Create User with validation
router.post("/createuser", async (req, res) => {
const { name, email, password, location } = req.body;

// Validate name length (greater than 5 characters)
if (!name || name.length <= 5) {
return res.status(400).json({ error: "Name must be greater than 5 characters." });
}

// Validate email format (basic regex check)
const emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
if (!email || !emailRegex.test(email)) {
return res.status(400).json({ error: "Please enter a valid email address." });
}

// Validate password length (greater than 4 characters)
if (!password || password.length <= 4) {
return res.status(400).json({ error: "Password must be greater than 4 characters." });
}

// Validate if all fields are provided
if (!location) {
return res.status(400).json({ error: "Location is required." });
}

try {
// Create new user and save to database
const user = new User({ name, email, password, location });
await user.save();
res.json({ success: true, user });
} catch (error) {
console.error("Error in /createuser:", error.message);
res.status(500).json({ success: false, error: error.message });
}
});

// Route: Login User
router.post("/loginuser", async (req, res) => {
const { email, password } = req.body;

if (!email || !password) {
return res.status(400).json({ error: "Email and password are required" });
}

try {
const user = await User.findOne({ email });
if (!user || user.password !== password) {
return res.status(400).json({ error: "Invalid email or password" });
}

res.json({ success: true });
} catch (error) {
console.error("Error in /loginuser:", error.message);
res.status(500).json({ error: "Server error" });
}
});

app.use("/api", router);

// Default Route
app.get("/", (req, res) => {
res.send("Hello World");
});

// Start the Server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
Loading

0 comments on commit 17dd423

Please sign in to comment.