+ Press n or j to go to the next uncovered block, b, p or k for the previous block. +
+ +| File | ++ | Statements | ++ | Branches | ++ | Functions | ++ | Lines | ++ |
|---|---|---|---|---|---|---|---|---|---|
| pizza_app | +
+
+ |
+ 88.46% | +23/26 | +100% | +0/0 | +40% | +2/5 | +88.46% | +23/26 | +
| pizza_app/model | +
+
+ |
+ 100% | +10/10 | +100% | +0/0 | +100% | +0/0 | +100% | +10/10 | +
| pizza_app/router | +
+
+ |
+ 31.94% | +23/72 | +0% | +0/18 | +6.25% | +1/16 | +31.94% | +23/72 | +
+ Press n or j to go to the next uncovered block, b, p or k for the previous block. +
+ +| 1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21 +22 +23 +24 +25 +26 | 1x +1x +1x +1x +1x + +1x +1x +1x + +1x + +1x + + + +1x + +1x + + +1x +1x + + +1x | const express = require("express")
+require("dotenv").config()
+const {connectToMongodb} = require("./db")
+const orderRoute = require("./router/order")
+const userRoute = require("./router/users")
+
+connectToMongodb()
+const app = express()
+app.use(express.json())
+
+const PORT = process.env.PORT
+
+app.get('/',()=>{
+ console.log("Home route")
+})
+
+app.use("/order",orderRoute)
+
+app.use("/user", userRoute)
+
+
+app.listen(PORT,()=>{
+ console.log("Server is listening at PORT",PORT)
+})
+
+module.exports = app |
+ Press n or j to go to the next uncovered block, b, p or k for the previous block. +
+ +| 1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21 | 1x +1x +1x + + + +1x + +1x + +1x + + +1x + + + + +1x + + | const app = require("express")
+const mongoose = require("mongoose")
+require("dotenv")
+
+
+function connectToMongodb(){
+ const database_url = process.env.DATABASE_URL
+
+ mongoose.connect(database_url)
+
+ mongoose.connection.on("connected",()=>{
+ console.log("connection to database is successful")
+ })
+ mongoose.connection.on("error",()=>{
+ console.log("Error connecting to database")
+ })
+}
+
+module.exports = {connectToMongodb}
+
+ |
+ Press n or j to go to the next uncovered block, b, p or k for the previous block. +
+ ++ Press n or j to go to the next uncovered block, b, p or k for the previous block. +
+ +| File | ++ | Statements | ++ | Branches | ++ | Functions | ++ | Lines | ++ |
|---|---|---|---|---|---|---|---|---|---|
| orderModel.js | +
+
+ |
+ 100% | +6/6 | +100% | +0/0 | +100% | +0/0 | +100% | +6/6 | +
| userModel.js | +
+
+ |
+ 100% | +4/4 | +100% | +0/0 | +100% | +0/0 | +100% | +4/4 | +
+ Press n or j to go to the next uncovered block, b, p or k for the previous block. +
+ +| 1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21 | 1x + +1x +1x + +1x + + + + + + + + + + + + +1x + +1x | const mongoose = require('mongoose');
+
+const Schema = mongoose.Schema;
+const ObjectId = Schema.ObjectId;
+
+const OrderSchema = new Schema({
+ id: ObjectId,
+ created_at: Date,
+ state: { type: Number, default: 1 },
+ total_price: Number,
+ items: [{
+ name: String,
+ price: Number,
+ size: { type: String, enum: ['m', 's', 'l']},
+ quantity: Number,
+ }]
+});
+
+const Order = mongoose.model('order', OrderSchema);
+
+module.exports = Order; |
+ Press n or j to go to the next uncovered block, b, p or k for the previous block. +
+ +| 1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21 | 1x + +1x + +1x + + + + + + + + + + + + + + +1x + | const mongoose = require("mongoose")
+
+const Schema = mongoose.Schema
+
+const userModel = new Schema({
+ username:{
+ type: "String",
+ required: true
+ },
+ password:{
+ type:"String",
+ required: true,
+ unique:true,
+ },
+ user_type:{
+ type: String, enum: ["admin", "user"]
+ }
+})
+
+module.exports = mongoose.model("users",userModel)
+ |
+ Press n or j to go to the next uncovered block, b, p or k for the previous block. +
+ ++ Press n or j to go to the next uncovered block, b, p or k for the previous block. +
+ +| 1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21 +22 +23 +24 +25 +26 +27 +28 +29 +30 +31 +32 +33 +34 +35 +36 +37 +38 +39 +40 +41 +42 +43 +44 +45 +46 +47 +48 +49 +50 +51 +52 +53 +54 +55 +56 +57 +58 +59 +60 +61 +62 +63 +64 +65 +66 +67 +68 +69 +70 +71 +72 +73 +74 +75 +76 +77 +78 +79 +80 +81 +82 +83 +84 +85 +86 +87 +88 +89 +90 +91 +92 +93 +94 +95 +96 +97 +98 +99 +100 +101 +102 +103 +104 +105 +106 +107 | 1x +1x + +1x +1x +1x + +1x +1x + +1x + + + + + + + + +1x + + + + + + + + + + + + + + + + +1x + + + + + + + + + + +1x + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1x + + + + + + + + + + + + + + + + + + + + +1x + + + + + + + + +1x | const express = require("express")
+const moment = require("moment")
+
+const orderRoute = express.Router()
+const orderModel = require("../model/orderModel")
+const userModel = require("../model/userModel")
+
+orderRoute.use(async(req,res,next)=>{
+ const password = req.query.password
+
+ const user = await userModel.find({status:password})
+ console.log(user, "user")
+ if (user.length>0) {
+ next()
+ }else{
+ res.status(401).send({message: "unauthorized", data:"please register as a user"})
+ }
+})
+
+orderRoute.post('/', async (req, res) => {
+ const body = req.body;
+
+ const total_price = body.items.reduce((prev, curr) => {
+ prev += curr.price
+ return prev
+ }, 0);
+
+ const order = await orderModel.create({
+ items: body.items,
+ created_at: moment().toDate(),
+ total_price
+ })
+
+ return res.json({ status: true, order })
+})
+
+orderRoute.get('/:orderId', async (req, res) => {
+ const { orderId } = req.params;
+ const order = await orderModel.findById(orderId)
+
+ if (!order) {
+ return res.status(404).json({ status: false, order: null })
+ }
+
+ return res.json({ status: true, order })
+})
+
+orderRoute.get('/', async (req, res) => {
+
+ let orders = await orderModel.find()
+ if(req.query.state){
+ orders = await orderModel.find({status:req.query.state})
+ }
+
+ if(req.query.total_price === "asc"){
+ orders = await orderModel.find().sort({total_price:1,_id:1})
+
+ }
+ if(req.query.total_price === "desc"){
+ orders = await orderModel.find().sort({total_price:-1,_id:-1})
+
+ }
+ if(req.query.date === "asc"){
+ orders = await orderModel.find().sort({created_at:1,_id:1})
+
+ }
+ if(req.query.date === "desc"){
+ orders = await orderModel.find().sort({created_at:-1,_id:-1})
+
+ }
+
+
+
+ return res.json({ status: true, orders })
+})
+
+orderRoute.patch('/:id', async (req, res) => {
+ const { id } = req.params;
+ const { state } = req.body;
+
+ const order = await orderModel.findById(id)
+
+ if (!order) {
+ return res.status(404).json({ status: false, order: null })
+ }
+
+ if (state < order.state) {
+ return res.status(422).json({ status: false, order: null, message: 'Invalid operation' })
+ }
+
+ order.state = state;
+
+ await order.save()
+
+ return res.json({ status: true, order })
+})
+
+orderRoute.delete('/:id', async (req, res) => {
+ const { id } = req.params;
+
+ const order = await orderModel.deleteOne({ _id: id})
+
+ return res.json({ status: true, order })
+})
+
+
+module.exports = orderRoute |
+ Press n or j to go to the next uncovered block, b, p or k for the previous block. +
+ +| 1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21 +22 +23 +24 +25 +26 +27 +28 +29 +30 +31 +32 +33 +34 +35 +36 +37 +38 +39 +40 +41 +42 +43 +44 +45 +46 +47 +48 | 1x + +1x +1x + + +1x + + + + + + + + + + + + + + +1x + + + +1x + + + + + + + + + + + + + + +1x + + +1x + + + +1x | const express = require("express")
+
+const userRoute = express.Router()
+const userModel = require("../model/userModel")
+
+
+userRoute.get("/",(req, res)=>{
+ userModel.find({}).then((users)=>{
+ res.status(200).send({
+ message: "users retrieved successfully",
+ data: users
+ })
+ }).catch((err)=>{
+ console.log(err)
+ res.status(500).send(err.message)
+
+ })
+
+
+})
+
+userRoute.get("/:id",async(req, res)=>{
+
+})
+
+userRoute.post("/",(req, res)=>{
+ const user = req.body
+ userModel.create(user).then((user)=>{
+ res.status(201).send({
+ message: "user created successfully",
+ data: user
+ })
+
+ }).catch((err)=>{
+ console.log(err)
+ res.status(400).send(err)
+
+ })
+})
+
+userRoute.delete("/:id",async(req, res)=>{
+
+})
+userRoute.put("/:id ",async(req, res)=>{
+
+})
+
+module.exports = userRoute |