It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.
BACKEND/package.json
{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "lkh-krishen",
"license": "ISC",
"dependencies": {
"@koa/router": "^10.1.1",
"koa": "^2.13.4",
"koa-bodyparser": "^4.3.0",
"mongodb": "^4.7.0"
}
}
BACKEND/index.js
const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const productRoutes = require('./routes/products.routes');
const promotionRoutes = require('./routes/promotions.routes');
const app = new Koa();
app.use(bodyParser());
app.use(productRoutes.routes()).use(productRoutes.allowedMethods());
app.use(promotionRoutes.routes()).use(promotionRoutes.allowedMethods());
app.listen(3000, () => {
console.log("Application is running on port 3000");
});
BACKEND/dal/index.js
const {MongoClient} = require("mongodb");
const client = new MongoClient('mongodb://localhost:27017', {
useNewUrlParser: true,
useUnifiedTopology: true
});
client.connect(err => {
if(err){
console.error(err);
process.exit(-1);
}
console.log("Successfully connected to MongoDB");
});
module.exports = client;
BACKEND/dal/products.dao.js
const products = require('./index').db('store').collection('products');
const ObjectId = require('mongodb').ObjectId;
const save = async ({name, description, qty, price}) => {
const result = await products.insertOne({name, description, qty, price});
//console.log(result.ops);
return await getById(result.insertedId)
//return result;
}
const getAll = async () => {
const cursor = await products.find();
return cursor.toArray();
}
const getById = async (id) => {
return await products.findOne({_id:ObjectId(id)});
}
const update = async (id, {name, description, qty, price}) => {
const result = await products.replaceOne({_id:ObjectId(id)}, {name, description, qty, price});
return result.ops[0];
}
const removeById = async id => {
await products.deleteOne({_id:ObjectId(id)});
}
module.exports = {save, getAll, getById, update, removeById};
BACKEND/api/products.api.js
const {getAll, save, update, getById, removeById} = require('../dal/products.dao');
const createProduct = async ({name, description, qty, price}) => {
const product = {
name,
description,
qty,
price
}
return await save(product);
}
const getProducts = async () => {
return await getAll();
}
const getProduct = async id => {
return await getById(id);
}
const deleteProduct = async id => {
return await removeById(id);
}
const updateProduct = async (id, {name, description, qty, price}) => {
return await update(id, {name, description, qty, price});
}
module.exports = {createProduct, getProducts, getProduct, deleteProduct, updateProduct};
BACKEND/routes/products.routes.js
const Router = require('@koa/router');
const {createProduct, deleteProduct, updateProduct, getProduct, getProducts} = require('../api/products.api');
const router = new Router ({
prefix: '/products'
});
router.get('/', async ctx => {
ctx.body = await getProducts();
});
router.post('/', async ctx => {
let product = ctx.request.body;
product = await createProduct(product);
ctx.response.status = 200;
ctx.body = product;
});
router.get('/:id', async ctx => {
const id = ctx.params.id;
ctx.body = await getProduct(id);
});
router.delete('/:id', async ctx => {
const id = router.params.id;
ctx.body = await deleteProduct(id);
});
router.put('/:id', async ctx => {
const id = ctx.params.id;
let product = ctx.request.body;
product = await updateProduct(product);
ctx.response.status = 200;
ctx.body = product;
});
module.exports = router;
CodePudding user response:
backend/api/customer.api.js
import { Customer } from '../models/Customer.js';
import {randomBytes} from 'crypto'
const customerList = new Map();
//
export const saveCustomer = ({ name }) => {
const newCustomer = new Customer(
randomBytes(16).toString('hex'),
name,
[],
[]
);
customerList.set(newCustomer.id, newCustomer);
return newCustomer;
}
export const addToCart = (customerId, { item }) => {
const customer = getCustomer(customerId);
customer.cart.push(item);
console.log(customer);
customerList.set(customerId, customer);
}
export const viewCart = (customerId) => {
const customer = getCustomer(customerId);
return [...customer.cart];
}
export const addToWishList = (customerId, { item }) => {
const customer = getCustomer(customerId);
customer.wishList.push(item);
console.log(customer);
customerList.set(customerId, customer);
}
export const viewwishList = (customerId) => {
const customer = getCustomer(customerId);
return [...customer.wishList];
}
export const listAllCustomers = () => {
return [...customerList.values()];
}
const getCustomer = (id) => {
return customerList.get(id);
}
backend/models/Customer.js
export class Customer {
constructor(id, name, cart, wishList) {
this.id = id;
this.name = name;
this.cart = cart;
this.wishList = wishList;
}
}
backend/router/customer.router.js
import Router from '@koa/router'
import { saveCustomer, listAllCustomers, addToCart, addToWishList, viewCart, viewwishList } from '../api/customer.api.js'
const customerRouter = new Router({
prefix: '/customers'
});
customerRouter.post('/save', (ctx) => {
const data = ctx.request.body;
console.log(`Data: ${data}`)
ctx.body=saveCustomer(data);
ctx.set('Content-Type','application/json')
ctx.status = 201;
});
customerRouter.put('/:customerId/addToCart', (ctx) => {
const id = ctx.params.customerId;
ctx.body = addToCart(id, ctx.request.body);
ctx.set('Content-Type','application/json')
ctx.status = 200;
});
customerRouter.put('/:customerId/addToWishList', (ctx) => {
const id = ctx.params.customerId;
ctx.body = addToWishList(id, ctx.request.body);
ctx.set('Content-Type','application/json')
ctx.status = 200;
});
customerRouter.get('/customerId/viewCart', (ctx)=>{
const id = ctx.params.customerId;
ctx.body = viewCart(id);
ctx.set('Content-Type','application/json')
ctx.status = 200;
});
customerRouter.get('/customerId/viewWishList', (ctx)=>{
const id = ctx.params.customerId;
ctx.body = viewwishList(id, ctx.request.body);
ctx.set('Content-Type','application/json')
ctx.status = 200;
});
customerRouter.get('/', (ctx)=>{
ctx.body = listAllCustomers();
ctx.set('Content-Type','application/json')
ctx.status = 200;
});
export default customerRouter;
CodePudding user response:
const mongoose = require("mongoose");
const StudentGroupModel = mongoose.Schema({
students: [{
type: mongoose.Schema.Types.ObjectId,
required: false,
ref:'User'
}],
topic: {
type: String,
required: false
},
supervisor: {
type: mongoose.Schema.Types.ObjectId,
required: false,
ref:'User'
},
co_supervisor: {
type: mongoose.Schema.Types.ObjectId,
required: false,
ref: 'User'
},
attachments:[{
type: String,
required: false,
}],
panel: [{
type: mongoose.Schema.Types.ObjectId,
retuired: false,
ref: 'User'
}]
});
const StudentGroup = mongoose.model('StudentGroup', StudentGroupModel);
module.exports = StudentGroup;