Skip to content

Commit

Permalink
addfirst
Browse files Browse the repository at this point in the history
  • Loading branch information
huutinh0605 committed Feb 11, 2023
0 parents commit 91b4d0b
Show file tree
Hide file tree
Showing 4,424 changed files with 482,610 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MONGO=mongodb+srv://huutinh:[email protected]/restaurant-booking?retryWrites=true&w=majority
63 changes: 63 additions & 0 deletions controllers/bill.controllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const { Bill } = require("../models/Bill.js")

const getAllBill=async(req,res)=>{
const getList=await Bill.find();
try {
res.status(200).send(getList);
} catch (error) {
res.status(404).send(error);
}
}
const getDetailBill=async(req,res)=>{
const getDetail=await Bill.findById(req.params.id);
try {
if(getDetail){
res.status(200).send(getDetail);
}else{
res.status(404).send("Khong ton tai id nay");
}
} catch (error) {
res.status(500).send(error);
}
}
const createBill=async(req,res)=>{
const newBill=new Bill(req.body);
try {
if(newBill){
await newBill.save();
res.status(201).send(newBill);
}else{
res.status(404).send("Not found");
}
} catch (error) {
res.status(500).send(error)
}
}
const updateBill=async(req,res)=>{
const updatedBill=await Bill.findByIdAndUpdate(req.params.id,{$set:req.body},{new:true});
try {
res.status(200).send(updatedBill);
} catch (error) {
res.status(500).send(error)
}
}

const deleteBill=async(req,res)=>{
try {
const delBill=await Bill.findByIdAndDelete(req.params.id);
if(delBill){
res.status(200).send("Xoa thanh cong")
}else{
res.status(404).send("Id khong ton tai");
}
} catch (error) {
res.status(500).send(error);
}
}
module.exports={
getAllBill,
getDetailBill,
createBill,
updateBill,
deleteBill
}
63 changes: 63 additions & 0 deletions controllers/order.controllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const { Order } = require("../models/Order.js")

const getAllOrder=async(req,res)=>{
const getList=await Order.find();
try {
res.status(200).send(getList);
} catch (error) {
res.status(404).send(error);
}
}
const getDetailOrder=async(req,res)=>{
const getDetail=await Order.findById(req.params.id);
try {
if(getDetail){
res.status(200).send(getDetail);
}else{
res.status(404).send("Khong ton tai id nay");
}
} catch (error) {
res.status(500).send(error);
}
}
const createOrder=async(req,res)=>{
const newOrder=new Order(req.body);
try {
if(newOrder){
await newOrder.save();
res.status(201).send(newOrder);
}else{
res.status(404).send("Not found");
}
} catch (error) {
res.status(500).send(error)
}
}
const updateOrder=async(req,res)=>{
const updatedOrder=await Order.findByIdAndUpdate(req.params.id,{$set:req.body},{new:true});
try {
res.status(200).send(updatedOrder);
} catch (error) {
res.status(500).send(error)
}
}

const deleteOrder=async(req,res)=>{
try {
const delOrder=await Order.findByIdAndDelete(req.params.id);
if(delOrder){
res.status(200).send("Xoa thanh cong")
}else{
res.status(404).send("Id khong ton tai");
}
} catch (error) {
res.status(500).send(error);
}
}
module.exports={
getAllOrder,
getDetailOrder,
createOrder,
updateOrder,
deleteOrder
}
86 changes: 86 additions & 0 deletions controllers/product.controllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const { Product } = require("../models/Product.js");
const gravatarUrl = require("gravatar-url");

const getAllProduct = async (req, res) => {
const getList = await Product.find();
try {
res.status(200).send(getList);
} catch (error) {
res.status(404).send(error);
}
};
const getDetailProduct = async (req, res) => {
const getDetail = await Product.findById(req.params.id);
try {
if (getDetail) {
res.status(200).send(getDetail);
} else {
res.status(404).send("Khong ton tai id nay");
}
} catch (error) {
res.status(500).send(error);
}
};
const createProduct = async (req, res) => {
//upload img
const avatarDefault = gravatarUrl(req.body.name);
const newProduct = new Product({
...req.body,
img:avatarDefault
});
try {
if (newProduct) {
//dat avatar mac dinh khi tao tk moi
await newProduct.save();
res.status(201).send(newProduct);
} else {
res.status(404).send("Not found");
}
} catch (error) {
res.status(500).send(error);
}
};
const updateProduct = async (req, res) => {
const updatedProduct = await Product.findByIdAndUpdate(
req.params.id,
{ $set: req.body },
{ new: true }
);
try {
res.status(200).send(updatedProduct);
} catch (error) {
res.status(500).send(error);
}
};

const deleteProduct = async (req, res) => {
try {
const delProduct = await Product.findByIdAndDelete(req.params.id);
if (delProduct) {
res.status(200).send("Xoa thanh cong");
} else {
res.status(404).send("Id khong ton tai");
}
} catch (error) {
res.status(500).send(error);
}
};

//upload img
const uploadAvatar = async (req, res) => {
const { id } = req.params;
const { path } = req.file;
const userFound = await Product.findById(id)
userFound.img = `http://localhost:8069/${path}`;
await userFound.save();
res.send(userFound);
};

module.exports = {
getAllProduct,
getDetailProduct,
createProduct,
updateProduct,
deleteProduct,
uploadAvatar,
};
63 changes: 63 additions & 0 deletions controllers/type.controllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const { Type } = require("../models/Type.js")

const getAllType=async(req,res)=>{
const getList=await Type.find();
try {
res.status(200).send(getList);
} catch (error) {
res.status(404).send(error);
}
}
const getDetailType=async(req,res)=>{
const getDetail=await Type.findById(req.params.id);
try {
if(getDetail){
res.status(200).send(getDetail);
}else{
res.status(404).send("Khong ton tai id nay");
}
} catch (error) {
res.status(500).send(error);
}
}
const createType=async(req,res)=>{
const newType=new Type(req.body);
try {
if(newType){
await newType.save();
res.status(201).send(newType);
}else{
res.status(404).send("Not found");
}
} catch (error) {
res.status(500).send(error)
}
}
const updateType=async(req,res)=>{
const updatedType=await Type.findByIdAndUpdate(req.params.id,{$set:req.body},{new:true});
try {
res.status(200).send(updatedType);
} catch (error) {
res.status(500).send(error)
}
}

const deleteType=async(req,res)=>{
try {
const delType=await Type.findByIdAndDelete(req.params.id);
if(delType){
res.status(200).send("Xoa thanh cong")
}else{
res.status(404).send("Id khong ton tai");
}
} catch (error) {
res.status(500).send(error);
}
}
module.exports={
getAllType,
getDetailType,
createType,
updateType,
deleteType
}
27 changes: 27 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const express = require("express");
const app = express();
const dotenv = require("dotenv");
const { default: mongoose } = require("mongoose");
const { rootRouter } = require("./routers");

dotenv.config();
mongoose.set("strictQuery", true);
const connect = async () => {
try {
await mongoose.connect(process.env.MONGO);
console.log("Connected to MongoDB");
} catch (error) {
throw error;
}
};

connect();
app.use(express.json());
app.use("/api/v1",rootRouter)
mongoose.connection.on("disconnected", () => {
console.log("mongoDB disconnected");
});
const port = 8069|| process.env.PORT;
app.listen(port, () => {
console.log(`App run on http://localhost:${port}/api/v1`);
});
35 changes: 35 additions & 0 deletions middlewares/upload-images.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// thư viện multer upload file img
const mkdirp = require("mkdirp");
const multer = require("multer");

const uploadImages = (type) => {
mkdirp.sync(`./public/images/${type}`);
const storage = multer.diskStorage({
//destination: khai báo đg dẫn thư mục chứa img.
destination: (req, file, cb) => {
cb(null, `./public/images/${type}`); // /images/avatar : phải tạo tay thư mục này, vì mặc định chỉ dẫn ảnh đến thư mục public.
},
// filenam: định nghĩa tên file khi lưu
filename: (req, file, cb) => {
cb(null, Date.now() + "_" + file.originalname);
// để tránh bị file ghi đè khi đã tồn tại file đó r, Date.now(): để thêm ngày tránh bị đè ảnh
},
});
const uploads = multer({
storage: storage,
fileFilter: function (req, file, cb) {
const extensionImgList = [".jpg", ".png"];
const extension = file.originalname.slice(-4); // slice(-4): lấy 4 kí tự cuối cùng.
const check = extensionImgList.includes(extension); // includes: kiểm tra trong extensionImgList có tồn tại extension ko. TRUE/ FALSE.
if (check) {
cb(null, true);
} else {
cb(new Error("Loại file không hợp lệ !"));
}
},
});
return uploads.single(type);
};
module.exports = {
uploadImages,
};
20 changes: 20 additions & 0 deletions models/Bill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const mongoose=require("mongoose");

const BillSchema=mongoose.Schema({
email:{
type:String,
required:true
},
date:{
type:[Date],
required:true
},
totalPrice:{
type:Number,
required:true
}
})
const Bill=mongoose.model("Bill",BillSchema);
module.exports={
Bill
};
18 changes: 18 additions & 0 deletions models/Order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const mongoose = require("mongoose");

const OrderSchema = mongoose.Schema({
name: {
type: String,
required: true,
},
products: [{
productName: String,
amount: {
type: Number
}
}]
});
const Order = mongoose.model("Order", OrderSchema);
module.exports = {
Order,
};
Loading

0 comments on commit 91b4d0b

Please sign in to comment.