diff --git a/app.js b/app.js index 6797a654..a0c9159e 100644 --- a/app.js +++ b/app.js @@ -1,132 +1,132 @@ -const express = require('express'); -const mongoose = require('mongoose'); -const bcrypt = require('bcrypt'); -const cors = require('cors'); - -// Import required modules - -// Create an Express app -const app = express(); - -// Enable CORS -app.use(express.json()); -app.use(cors()); - -// Connect to MongoDB -mongoose.connect('mongodb://localhost:27017/cafe', { useNewUrlParser: true, useUnifiedTopology: true }) - .then(() => { - console.log('Connected to MongoDB'); - }) - .catch((error) => { - console.error('Error connecting to MongoDB:', error); - }); - -// Define a user schema -const userSchema = new mongoose.Schema({ - username: { type: String, required: true }, - password: { type: String, required: true } -}); - -// Define a user model -const User = mongoose.model('User', userSchema); -const productSchema = new mongoose.Schema({ - name: { type: String, required: true }, - price: { type: Number, required: true }, - quantity: { type: Number, required: true } -}); -const Product = mongoose.model('Product', productSchema); -// Register a new user -app.post('/register', async (req, res) => { - try { - const { username, password } = req.body; - - // Check if the username already exists - const existingUser = await User.findOne({ username }); - if (existingUser) { - return res.status(400).json({ message: 'Username already exists' }); - } - - // Hash the password - const hashedPassword = await bcrypt.hash(password, 10); - - // Create a new user - const newUser = new User({ username, password: hashedPassword }); - await newUser.save(); - - res.status(201).json({ message: 'User registered successfully' }); - } catch (error) { - console.error('Error registering user:', error); - res.status(500).json({ message: 'Internal server error' }); - } -}); - -// User login -app.post('/login', async (req, res) => { - try { - const { username, password } = req.body; - - // Find the user by username - const user = await User.findOne({ username }); - if (!user) { - return res.status(404).json({ message: 'User not found' }); - } - - // Compare the password - const isPasswordValid = await bcrypt.compare(password, user.password); - if (!isPasswordValid) { - return res.status(401).json({ message: 'Invalid password' }); - } - - res.status(200).json({ message: 'Login successful' }); - } catch (error) { - console.error('Error logging in:', error); - res.status(500).json({ message: 'Internal server error' }); - } -}); - -app.get('/products',async (req,res)=>{ - try{ - const products = await Product.find(); - res.status(200).json(products); - }catch(error){ - console.error('Error getting products:',error); - res.status(500).json({message:'Internal server error'}); - } -}) - -app.post('/products',async (req,res)=>{ - try{ - const {name,price,quantity} = req.body; - const newProduct = new Product({name,price,quantity}); - await newProduct.save(); - res.status(201).json({message:'Product added successfully'}); - }catch(error){ - console.error('Error adding product:',error); - res.status(500).json({message:'Internal server error'}); - } -}) - -app.put('/products/:id',async (req,res)=>{ - try{ - const {name,price,quantity} = req.body; - await Product.findByIdAndUpdate(req.params.id,{name,price,quantity}); - res.status(200).json({message:'Product updated successfully'}); - }catch(error){ - console.error('Error updating product:',error); - res.status(500).json({message:'Internal server error'}); - } -}) - -app.delete('/products/:id',async (req,res)=>{ - try{ - await Product.findByIdAndDelete(req.params.id); - res.status(200).json({message:'Product deleted successfully'}); - }catch(error){ - console.error('Error deleting product:',error); - res.status(500).json({message:'Internal server error'}); - } -}) -// Start the server -app.listen(3000, () => { - console.log('Server started on port 3000'); +const express = require('express'); +const mongoose = require('mongoose'); +const bcrypt = require('bcrypt'); +const cors = require('cors'); + +// Import required modules + +// Create an Express app +const app = express(); + +// Enable CORS +app.use(express.json()); +app.use(cors()); + +// Connect to MongoDB +mongoose.connect('mongodb://localhost:27017/cafe', { useNewUrlParser: true, useUnifiedTopology: true }) + .then(() => { + console.log('Connected to MongoDB'); + }) + .catch((error) => { + console.error('Error connecting to MongoDB:', error); + }); + +// Define a user schema +const userSchema = new mongoose.Schema({ + username: { type: String, required: true }, + password: { type: String, required: true } +}); + +// Define a user model +const User = mongoose.model('User', userSchema); +const productSchema = new mongoose.Schema({ + name: { type: String, required: true }, + price: { type: Number, required: true }, + quantity: { type: Number, required: true } +}); +const Product = mongoose.model('Product', productSchema); +// Register a new user +app.post('/register', async (req, res) => { + try { + const { username, password } = req.body; + + // Check if the username already exists + const existingUser = await User.findOne({ username }); + if (existingUser) { + return res.status(400).json({ message: 'Username already exists' }); + } + + // Hash the password + const hashedPassword = await bcrypt.hash(password, 10); + + // Create a new user + const newUser = new User({ username, password: hashedPassword }); + await newUser.save(); + + res.status(201).json({ message: 'User registered successfully' }); + } catch (error) { + console.error('Error registering user:', error); + res.status(500).json({ message: 'Internal server error' }); + } +}); + +// User login +app.post('/login', async (req, res) => { + try { + const { username, password } = req.body; + + // Find the user by username + const user = await User.findOne({ username }); + if (!user) { + return res.status(404).json({ message: 'User not found' }); + } + + // Compare the password + const isPasswordValid = await bcrypt.compare(password, user.password); + if (!isPasswordValid) { + return res.status(401).json({ message: 'Invalid password' }); + } + + res.status(200).json({ message: 'Login successful' }); + } catch (error) { + console.error('Error logging in:', error); + res.status(500).json({ message: 'Internal server error' }); + } +}); + +app.get('/products',async (req,res)=>{ + try{ + const products = await Product.find(); + res.status(200).json(products); + }catch(error){ + console.error('Error getting products:',error); + res.status(500).json({message:'Internal server error'}); + } +}) + +app.post('/products',async (req,res)=>{ + try{ + const {name,price,quantity} = req.body; + const newProduct = new Product({name,price,quantity}); + await newProduct.save(); + res.status(201).json({message:'Product added successfully'}); + }catch(error){ + console.error('Error adding product:',error); + res.status(500).json({message:'Internal server error'}); + } +}) + +app.put('/products/:id',async (req,res)=>{ + try{ + const {name,price,quantity} = req.body; + await Product.findByIdAndUpdate(req.params.id,{name,price,quantity}); + res.status(200).json({message:'Product updated successfully'}); + }catch(error){ + console.error('Error updating product:',error); + res.status(500).json({message:'Internal server error'}); + } +}) + +app.delete('/products/:id',async (req,res)=>{ + try{ + await Product.findByIdAndDelete(req.params.id); + res.status(200).json({message:'Product deleted successfully'}); + }catch(error){ + console.error('Error deleting product:',error); + res.status(500).json({message:'Internal server error'}); + } +}) +// Start the server +app.listen(3000, () => { + console.log('Server started on port 3000'); }); \ No newline at end of file diff --git "a/\342\200\216Data-Structures-and-Algorithms-for-Interviews/Main.java" "b/\342\200\216Data-Structures-and-Algorithms-for-Interviews/Main.java" new file mode 100644 index 00000000..58598909 --- /dev/null +++ "b/\342\200\216Data-Structures-and-Algorithms-for-Interviews/Main.java" @@ -0,0 +1,80 @@ +// Following program is a Java implementation +// of Rabin Karp Algorithm given in the CLRS book + +public class Main { + // d is the number of characters in the input alphabet + public final static int d = 256; + + /* pat -> pattern + txt -> text + q -> A prime number + */ + static void search(String pat, String txt, int q) + { + int M = pat.length(); + int N = txt.length(); + int i, j; + int p = 0; // hash value for pattern + int t = 0; // hash value for txt + int h = 1; + + // The value of h would be "pow(d, M-1)%q" + for (i = 0; i < M - 1; i++) + h = (h * d) % q; + + // Calculate the hash value of pattern and first + // window of text + for (i = 0; i < M; i++) { + p = (d * p + pat.charAt(i)) % q; + t = (d * t + txt.charAt(i)) % q; + } + + // Slide the pattern over text one by one + for (i = 0; i <= N - M; i++) { + + // Check the hash values of current window of + // text and pattern. If the hash values match + // then only check for characters one by one + if (p == t) { + /* Check for characters one by one */ + for (j = 0; j < M; j++) { + if (txt.charAt(i + j) != pat.charAt(j)) + break; + } + + // if p == t and pat[0...M-1] = txt[i, i+1, + // ...i+M-1] + if (j == M) + System.out.println( + "Pattern found at index " + i); + } + + // Calculate hash value for next window of text: + // Remove leading digit, add trailing digit + if (i < N - M) { + t = (d * (t - txt.charAt(i) * h) + + txt.charAt(i + M)) + % q; + + // We might get negative value of t, + // converting it to positive + if (t < 0) + t = (t + q); + } + } + } + + /* Driver Code */ + public static void main(String[] args) + { + String txt = "GEEKS FOR GEEKS"; + String pat = "GEEK"; + + // A prime number + int q = 101; + + // Function Call + search(pat, txt, q); + } +} +