From 5681e60ad155083fe41d69d7042c622ae89ab91a Mon Sep 17 00:00:00 2001 From: Ally Elvis Nzeyimana Date: Sat, 1 Feb 2025 23:31:02 +0200 Subject: [PATCH 1/2] Create development.sh --- development.sh | 189 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 development.sh diff --git a/development.sh b/development.sh new file mode 100644 index 00000000..2045fe1a --- /dev/null +++ b/development.sh @@ -0,0 +1,189 @@ +#!/bin/bash + +# Variables +PROJECT_NAME="shop_management_system" +BACKEND_DIR="$PROJECT_NAME/backend" +FRONTEND_DIR="$PROJECT_NAME/frontend" +PRODUCT_MODEL_PATH="$BACKEND_DIR/models/Product.js" +PRODUCT_ROUTES_PATH="$BACKEND_DIR/routes/products.js" +PRODUCT_FORM_PATH="$FRONTEND_DIR/ShopManagementSystem/ProductForm.js" +SERVER_PATH="$BACKEND_DIR/server.js" + +# Create project directories +echo "Creating project directories..." +mkdir -p $BACKEND_DIR $FRONTEND_DIR/ShopManagementSystem + +# Initialize Node.js backend +echo "Initializing Node.js backend..." +cd $BACKEND_DIR +npm init -y + +# Install necessary backend packages +echo "Installing backend dependencies..." +npm install express mongoose cors dotenv + +# Create product model +cat < $PRODUCT_MODEL_PATH +const mongoose = require('mongoose'); + +const productSchema = new mongoose.Schema({ + name: { type: String, required: true }, + price: { type: Number, required: true }, + quantity: { type: Number, required: true }, + description: { type: String }, + category: { type: String }, +}); + +const Product = mongoose.model('Product', productSchema); + +module.exports = Product; +EOT + +# Create product routes +cat < $PRODUCT_ROUTES_PATH +const express = require('express'); +const Product = require('../models/Product'); + +const router = express.Router(); + +// Get all products +router.get('/', async (req, res) => { + try { + const products = await Product.find(); + res.json(products); + } catch (err) { + res.status(500).json({ message: err.message }); + } +}); + +// Create new product +router.post('/', async (req, res) => { + const product = new Product(req.body); + try { + const newProduct = await product.save(); + res.status(201).json(newProduct); + } catch (err) { + res.status(400).json({ message: err.message }); + } +}); + +// Export the router +module.exports = router; +EOT + +# Create the server file +cat < $SERVER_PATH +const express = require('express'); +const mongoose = require('mongoose'); +const cors = require('cors'); +require('dotenv').config(); + +const app = express(); +const PORT = process.env.PORT || 3000; + +// Middleware +app.use(cors()); +app.use(express.json()); + +// Connect to MongoDB +mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true }) + .then(() => console.log('MongoDB Connected')) + .catch(err => console.error(err)); + +// Define routes +const productsRouter = require('./routes/products'); +app.use('/api/products', productsRouter); + +// Basic route +app.get('/', (req, res) => { + res.send('Welcome to the Shop Management System API!'); +}); + +// Start server +app.listen(PORT, () => { + console.log(\`Server is running on port \${PORT}\`); +}); +EOT + +# Navigate to frontend directory +cd ../$FRONTEND_DIR + +# Initialize React Native app +echo "Creating React Native app..." +npx react-native init ShopManagementSystem + +# Navigate to the Google Cloud Storage setup +cd ShopManagementSystem + +# Install axios for HTTP requests +echo "Installing Axios for front-end HTTP requests..." +npm install axios + +# Create ProductForm.js for handling product form submission +cat < ProductForm.js +import React, { useState } from 'react'; +import { View, TextInput, Button, Alert } from 'react-native'; +import axios from 'axios'; + +const ProductForm = () => { + const [name, setName] = useState(''); + const [price, setPrice] = useState(''); + const [quantity, setQuantity] = useState(''); + const [description, setDescription] = useState(''); + const [category, setCategory] = useState(''); + + const addProduct = async () => { + try { + const response = await axios.post('http:///api/products', { + name, + price, + quantity, + description, + category, + }); + Alert.alert('Product Added', \`Product \${response.data.name} has been added.\`); + } catch (error) { + Alert.alert('Error', 'Failed to add product.'); + } + }; + + return ( + + + + + + +