-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from bhaskarblur/main
Setup Database , Added books, categories + books by categories APIs
- Loading branch information
Showing
12 changed files
with
802 additions
and
2 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const booksHelper = require('../database/booksHelper'); | ||
|
||
export async function exploreBooks(req, res) { | ||
try { | ||
const pageSize = req.query.pageSize ? req.query.pageSize : 20; | ||
const page = req.query.page ? req.query.page : 1; | ||
const offSet = pageSize * page - pageSize; | ||
|
||
const books = await booksHelper.exploreBooks(pageSize, offSet); | ||
|
||
if(books.length > 0) { | ||
res.status(200).json({"flag": true, "message":"All books", "result" : books}) | ||
} | ||
else { | ||
res.status(404).json({"flag": false, "message":"No books"}) | ||
} | ||
} | ||
catch(err) { | ||
res.status(403).json({"flag":false, "message":err.message}); | ||
} | ||
} | ||
|
||
export async function booksByCategory(req, res) { | ||
try { | ||
const pageSize = req.query.pageSize ? req.query.pageSize : 20; | ||
const page = req.query.page ? req.query.page : 1; | ||
const offSet = pageSize * page - pageSize; | ||
const category = req.query.categoryId; | ||
|
||
const books = await booksHelper.booksByCategory(pageSize, offSet, category); | ||
|
||
if(books.length > 0) { | ||
res.status(200).json({"flag": true, "message":"All books", "result" : books}) | ||
} | ||
else { | ||
res.status(404).json({"flag": false, "message":"No books"}) | ||
} | ||
} | ||
catch(err) { | ||
res.status(403).json({"flag":false, "message":err.message}); | ||
} | ||
} | ||
|
||
|
||
export async function addBook(req, res) { | ||
try { | ||
const title = req.body.title; | ||
const desc = req.body.description; | ||
const image = req.body.coverImage; | ||
const rating = req.body.rating; | ||
const price = req.body.price; | ||
const categories = JSON.parse(req.body.categories); | ||
|
||
const books = await booksHelper.addBook(title, desc, image, rating, price,categories) | ||
|
||
if(books._id) { | ||
res.status(200).json({"flag": true, "message":"Book added", "result" : books}) | ||
} | ||
else { | ||
res.status(404).json({"flag": false, "message":"Failed to add book, check that you're sending right and all body paramteres"}) | ||
} | ||
} | ||
catch(err) { | ||
res.status(403).json({"flag":false, "message":err.message}); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const helper = require('../database/categoriesHelper'); | ||
|
||
export async function categoriesList(req, res) { | ||
try { | ||
const pageSize = req.query.pageSize ? req.query.pageSize : 20; | ||
const page = req.query.page ? req.query.page : 1; | ||
const offSet = pageSize * page - pageSize; | ||
|
||
const categories = await helper.categoriesList(pageSize, offSet); | ||
|
||
if(categories.length > 0) { | ||
res.status(200).send({"flag": true, "message":"All categories", "result" : categories}) | ||
} | ||
else { | ||
res.status(404).send({"flag": false, "message":"No categories"}) | ||
} | ||
} | ||
catch(err) { | ||
res.status(403).send({"flag":false, "message":err.message}); | ||
} | ||
} | ||
|
||
export async function addCategory(req, res) { | ||
try { | ||
const name = req.body.categoryName; | ||
|
||
const cat = await helper.addCategory(name); | ||
|
||
if(cat._id) { | ||
res.status(200).json({"flag": true, "message":"Category added", "result" : cat}) | ||
} | ||
else { | ||
res.status(404).json({"flag": false, "message":"Failed to add category, check that you're sending right and all body paramteres"}) | ||
} | ||
} | ||
catch(err) { | ||
res.status(403).json({"flag":false, "message":err.message}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require('dotenv').config(); | ||
const booksModel = require('../models/booksModel'); | ||
|
||
|
||
export async function exploreBooks(pageSize, offset) { | ||
|
||
return await booksModel.find().skip(offset).limit(pageSize); | ||
|
||
} | ||
|
||
export async function booksByCategory(pageSize, offset, category) { | ||
|
||
return await booksModel.find({categories : category}).skip(offset).limit(pageSize); | ||
|
||
} | ||
|
||
export async function addBook(title, desc, image, rating, price,category) { | ||
try { | ||
const create = await booksModel.create({title: title, description: desc, | ||
coverImage: image, rating: rating, price: price, categories: category}) | ||
console.log(create); | ||
return create; | ||
|
||
} | ||
catch(err) { | ||
console.log(err); | ||
return err; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require('dotenv').config() | ||
const catModel = require('../models/categoriesModel'); | ||
|
||
export async function categoriesList(pageSize, offset) { | ||
|
||
return await catModel.find().skip(offset).limit(pageSize); | ||
|
||
} | ||
|
||
export async function addCategory(name) { | ||
try { | ||
const create = await catModel.create({ categoryName: name }) | ||
console.log(create); | ||
return create; | ||
|
||
} | ||
catch(err) { | ||
console.log(err); | ||
return err; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require('dotenv').config() | ||
import mongoose from "mongoose"; | ||
|
||
export function initDatabase() { | ||
const connectionParams = { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true | ||
}; | ||
|
||
try { | ||
const uri : any = process.env.MONGODB_URI | ||
mongoose.connect( | ||
uri); | ||
console.log("Database connection established!") | ||
} | ||
catch (error) { | ||
console.log(error); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const mongoose = require('mongoose'); | ||
const mongoosePaginate = require('mongoose-paginate'); | ||
|
||
const booksScheme = new mongoose.Schema({ | ||
title: { | ||
type: String, | ||
required: true, | ||
}, | ||
description : { | ||
type: String, | ||
required: true | ||
}, | ||
coverImage : { | ||
type : String, | ||
required: true, | ||
}, | ||
price : { | ||
type : Number, | ||
required : true | ||
}, | ||
rating : { | ||
type : Number | ||
}, | ||
categories : { | ||
type : Array<string> | ||
} | ||
}, { collection: 'Books'}); | ||
|
||
booksScheme.plugin(mongoosePaginate); | ||
module.exports = mongoose.model('Books', booksScheme); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const mongoose = require('mongoose'); | ||
const mongoosePaginate = require('mongoose-paginate'); | ||
|
||
const categoryScheme = new mongoose.Schema( | ||
{ | ||
categoryName : { | ||
type : String, | ||
required: true, | ||
} | ||
|
||
}, { collection: 'Categories'}); | ||
|
||
categoryScheme.plugin(mongoosePaginate); | ||
module.exports = mongoose.model('Categories', categoryScheme); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import express from 'express'; | ||
const router = express.Router(); | ||
const controller = require('../controller/booksController'); | ||
const catController = require('../controller/categoriesController'); | ||
var endPoint = '/books'; | ||
|
||
export function getRouter() { | ||
return router; | ||
} | ||
|
||
router.get(endPoint+"/explore-books", async (req, res) => { | ||
controller.exploreBooks(req, res) | ||
}) | ||
|
||
router.get(endPoint+"/books-by-category", async (req, res) => { | ||
controller.booksByCategory(req, res) | ||
}) | ||
|
||
router.get(endPoint+"/categories", async (req, res) => { | ||
catController.categoriesList(req, res) | ||
}) | ||
|
||
router.post(endPoint+"/add-book", async (req, res) => { | ||
controller.addBook(req, res) | ||
}) | ||
|
||
router.post(endPoint+"/add-category", async (req, res) => { | ||
catController.addCategory(req, res) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as bcrypt from 'bcrypt'; | ||
|
||
export const password = { | ||
encryptPassword: (password: string) => | ||
bcrypt.genSalt(10) | ||
.then((salt => bcrypt.hash(password, salt))) | ||
.then(hash => hash), | ||
|
||
checkPassword: (password: string, hashPassword: string) => | ||
bcrypt.compare(password, hashPassword) | ||
.then(resp => resp) | ||
|
||
} | ||
|
||
export var generateRandomNumber = function (length) { | ||
return Math.floor(Math.pow(10, length-1) + Math.random() * (Math.pow(10, length) - Math.pow(10, length-1) - 1)); | ||
} |