// Dependencies
const express = require("express")
const mongoose = require("mongoose")
const cors = require("cors")
require("dotenv").config()
const userRoute = require("./routes/user")
const productRoute = require("./routes/product")
const cartRoute = require("./routes/cart")

// Server start
const app = express()

// Middlewares
app.use(express.json())
app.use(express.urlencoded({ extended: true }))

app.use(cors())

//Database
mongoose
	.connect(process.env.MONGO_URL, {
		useNewUrlParser: true,
		useUnifiedTopology: true,
		dbName: "CSP2",
	})
	.then(() => {
		console.log("Connected to Database..")
	})
	.catch((err) => {
		console.log(err)
	})

// Routes
app.use("/user", userRoute)
app.use("/product", productRoute)
app.use("/cart", cartRoute)

// Server up
app.listen(process.env.PORT || 3000, () => {
	console.log(`Server is running on port ${process.env.PORT}..`)
})
