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

test #3

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 4 additions & 0 deletions backend/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PORT = "5000"
MONGO_URI = "mongodb://localhost:27017/Booky"
EMAIL_USER = "[email protected]"
EMAIL_PASS = "brqk owyn synk lamb"
112 changes: 112 additions & 0 deletions backend/controllers/booksControllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const Book = require('../models/booksModel')

const getAllBook = async(req,res) => {
try {
const books = await Book.find()
console.log(books);
if(books.length <= 0){
return res.status(404).json({message: "Books Not Found"})
}

res.status(200).json(books)

} catch (error) {
res.status(500).json({message: error.message})
}
}

const getCategory = async(req,res) => {
try{
const catagory = await Book.find().select('catagory')

if(catagory.length <= 0){
res.status(404).json({message: "Catagory not found"})
}

res.status(200).json(catagory)

}catch (error) {
res.status(500).json({message: error.message})
}
}
const forYou = async (req, res) => {
try {
const { bookFilterMethod,category } = req.params;

console.log(bookFilterMethod);
const currentDate = new Date();

if (bookFilterMethod === "Less Than 6 months") {
const sixMonthsAgo = new Date(currentDate);
sixMonthsAgo.setMonth(sixMonthsAgo.getMonth() - 6);
console.log("Six months ago:", sixMonthsAgo);

let lessthan6MonthsBooks = await Book.find({ dateOfLastBuy: { $gte: sixMonthsAgo } });
if(category==="0"){
console.log("Books less than 6 months old:", lessthan6MonthsBooks);
res.status(200).json(lessthan6MonthsBooks);
}
else{
lessthan6MonthsBooks = lessthan6MonthsBooks.filter(book => book.category === category);
console.log("Books less than 6 months old:", lessthan6MonthsBooks);
res.status(200).json(lessthan6MonthsBooks);
}
// const filter = category ? { $and: [{ dateOfLastBuy: { $gte: sixMonthsAgo }}, {category: category}] } : { dateOfLastBuy: { $gte: sixMonthsAgo } };
// const lessthan6MonthsBooks = await Book.find(filter);
}if (bookFilterMethod === "Between 6 months and 1 Year") {
const oneYearAgo = new Date(currentDate);
oneYearAgo.setFullYear(oneYearAgo.getFullYear() - 1);
const sixMonthsAgo = new Date(currentDate);
sixMonthsAgo.setMonth(sixMonthsAgo.getMonth() - 6);

console.log("One year ago:", oneYearAgo);
console.log("Six months ago:", sixMonthsAgo);

let between6M1YBooks = await Book.find({ $and: [{ dateOfLastBuy: { $lte: sixMonthsAgo } },{ dateOfLastBuy: { $gte: oneYearAgo } }] });
if(category==="0"){
console.log("Books between 6 months and 1 year old:", between6M1YBooks);
res.status(200).json(between6M1YBooks);
}
else{
between6M1YBooks = between6M1YBooks.filter(book => book.category === category);
console.log("Books between 6 months and 1 year old:", between6M1YBooks);
res.status(200).json(between6M1YBooks);
}

}if (bookFilterMethod === "More Than a Year") {
const oneYearAgo = new Date(currentDate);
oneYearAgo.setFullYear(oneYearAgo.getFullYear() - 1);
console.log("One year ago:", oneYearAgo);

let oldBooks = await Book.find({ dateOfLastBuy: { $lt: oneYearAgo } });
if(category==="0"){
console.log("Books older than 1 year:", oldBooks);
res.status(200).json(oldBooks);
}
else{
oldBooks = oldBooks.filter(book => book.category === category);
console.log("Books older than 1 year:", oldBooks);
res.status(200).json(oldBooks);
}
}
} catch (error) {
res.status(500).json({ message: error.message });
}
};


// const setForYou = async (req, res) => {
// try{
// const { userIds , bookIds , offer , category } = req.body

// } catch{
// res.status(500).json({ message: error.message });
// }

// }

module.exports = {
getAllBook,
getCategory,
forYou
}
36 changes: 36 additions & 0 deletions backend/controllers/cartControllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const Cart = require('../models/cartModel')
const Book = require('../models/booksModel')

const addToCart = async (req,res) => {
try {
const {userId , bookId} = req.params
const {qty} = req.body

const book = await Book.findById(bookId)

if(!book){
return res.status(404).json({message: 'Book not found'})
}

const newCart = new Cart({
userId,
bookId,
qty
})

if(!newCart){
return res.status(404).json({message: "Cart not found"})
}

await newCart.save()

res.status(200).json({message: 'Add to Cart successfully'})

} catch (error) {
res.status(500).json({message: error.message})
}
}

module.exports = {
addToCart
}
33 changes: 33 additions & 0 deletions backend/controllers/usersControllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const User = require('../models/usersModel')

const getAllUsers = async (req,res) => {
try{
const usersInfo = await users.find()
res.status(200).send(usersInfo)
} catch{
res.status(500).json({ message: error.message });
}
}
const getUserCategory = async (req, res) => {
try {
const id = req.params.userId;
const user = await User.findById(id);
if (!user) {
return res.status(404).json({ message: "User not found" });
}

const priceSpentOnCategory = user.priceSpentOnCatagory;
const maxSpentCategory = [...priceSpentOnCategory.entries()].reduce((a, b) => {
return b[1] > a[1] ? b : a;
});

return res.json({ category: maxSpentCategory[0] });
} catch (error) {
res.status(500).json({ message: error.message });
}
}

module.exports = {
getAllUsers,
getUserCategory
}
10 changes: 10 additions & 0 deletions backend/db/connectionDB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const mongoose = require('mongoose')
require('dotenv').config()

const connectedDB = () => {
mongoose.connect(process.env.MONGO_URI)
.then(() => {console.log('MongoDB Connected')})
.catch((err) => {console.log(err)})
}

module.exports = connectedDB
32 changes: 32 additions & 0 deletions backend/models/booksModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const mongoose = require('mongoose')

const booksSchema = mongoose.Schema({
bookName : {
type : String,
required: true
},
category : {
type : String
},
description: {
type: String
},
price: {
type: Number,
required: true
},
image:{
type: String,
required: true
},
priceSpentOnBook:{
type: Number,
},
dateOfLastBuy:{
type: Date
}
})

const Book = mongoose.model('Book', booksSchema);

module.exports = Book;
20 changes: 20 additions & 0 deletions backend/models/cartModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const mongoose = require('mongoose')

const cartSchema = mongoose.Schema({
userId : {
type : mongoose.Schema.Types.ObjectId,
ref : 'User'
},
bookId : {
type : mongoose.Schema.Types.ObjectId,
ref : 'Book'
},
qty : {
type : Number,
required : true
}
})

const Cart = mongoose.model('Cart', cartSchema);

module.exports = Cart;
25 changes: 25 additions & 0 deletions backend/models/navbarOffers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const mongoose = require('mongoose')

const navbarOfferSchema = mongoose.Schema({
userId : [{
type : mongoose.Schema.Types.ObjectId,
ref : 'User'
}],
bookId : [{
type : mongoose.Schema.Types.ObjectId,
ref : 'Book'
}],
category:{
type: String,
required: true
},
offer : {
type : String,
required : true,
enum: ["Rent","Discount","Gift"]
}
})

const navbarOffer = mongoose.model('navbarOffer', navbarOfferSchema);

module.exports = navbarOffer;
29 changes: 29 additions & 0 deletions backend/models/usersModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const mongoose = require('mongoose')

const usersSchema = mongoose.Schema({
userName : {
type : String,
required: true
},
email : {
type : String,
required: true
},
password: {
type: String,
required: true
},
birthday: {
type: String,
required: true
},
priceSpentOnCatagory: {
type: Map,
of: Number
}

})

const User = mongoose.model('User', usersSchema);

module.exports = User;
16 changes: 16 additions & 0 deletions backend/node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions backend/node_modules/.bin/mime.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions backend/node_modules/.bin/mime.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions backend/node_modules/.bin/mkdirp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading