-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge with dev branch & Added some docs
- Loading branch information
Showing
48 changed files
with
9,225 additions
and
1,593 deletions.
There are no files selected for viewing
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Nixon Lizcano | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,24 @@ | ||
# Simple EComerce | ||
Made with MERN Stack | ||
|
||
## Installation | ||
Backend | ||
```bash | ||
cd backend | ||
yarn install | ||
yarn dev | ||
``` | ||
Frontend | ||
```bash | ||
cd frontend | ||
yarn install | ||
yarn start | ||
``` | ||
|
||
## Contributing | ||
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. | ||
|
||
Please make sure to update tests as appropriate. (I'all add some tests in the future :) ) | ||
|
||
## License | ||
[MIT](https://choosealicense.com/licenses/mit/) |
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 |
---|---|---|
@@ -1,9 +1,56 @@ | ||
import express from 'express'; | ||
import express, { Application } from 'express'; | ||
import mongoose from 'mongoose'; | ||
import morgan from 'morgan'; | ||
import cors from 'cors'; | ||
import { config } from 'dotenv'; | ||
|
||
const app = express(); | ||
import ProductRoutes from './routes/product.routes'; | ||
/* import userModel from './models/user.model'; | ||
import { jwtUser, seed } from './middlewares/user.middlewares'; */ | ||
|
||
// ... | ||
/* Initializations */ | ||
const app: Application = express(); | ||
if (process.env.NODE_ENV === 'dev' || process.env.NODE_ENV === undefined) | ||
config(); | ||
|
||
app.listen(process.env.PORT || 65000, ()=>{ | ||
console.log(`Server Running on port ${process.env.PORT || 65000}`) | ||
//Middlewares | ||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: false })); | ||
/* app.use(passport.initialize()); | ||
app.use(passport.session()); */ | ||
/* passport.use('userJwt', jwtUser); */ | ||
app.use(morgan('dev')); | ||
app.use(cors()); | ||
|
||
/* passport.serializeUser((user: any, done) => { | ||
done(null, user._id); | ||
}); | ||
passport.deserializeUser(async (id: any, done) => { | ||
const user = await userModel.findById(id); | ||
if (!user) | ||
return done(new Error('UserNotFound'), false); | ||
return done(null, user); | ||
}); */ | ||
|
||
//Routes | ||
app.use('/product', ProductRoutes); | ||
|
||
/* DB and server setup */ | ||
mongoose.connect( | ||
process.env.MONGO_URI || 'mongodb://localhost:27017/simple-ecomerce', | ||
{ | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true, | ||
useCreateIndex: true | ||
}, (err) => { | ||
if (err) throw err; | ||
console.log('DB connected'); | ||
} | ||
); | ||
app.listen(process.env.PORT || 65000, () => { | ||
console.log(`Server running in port ${process.env.PORT || 65000}`); | ||
}); | ||
|
||
console.log(`Server running in mode ${process.env.NODE_ENV || 'dev'}`); |
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,66 @@ | ||
import { Request, Response } from 'express'; | ||
|
||
import ProductModel, { IProduct } from '../models/product.model'; | ||
|
||
class ProductControllers { | ||
|
||
async createProduct(req: Request, res: Response) { | ||
const { name, price, photo, description, category } = req.body; | ||
|
||
const newProduct = new ProductModel({ | ||
name, price, photo, description, category | ||
}); | ||
await newProduct.save(); | ||
|
||
return res.json({ statusText: 'done' }); | ||
} | ||
|
||
async getProducts(req: Request, res: Response) { | ||
const products = await ProductModel.find(); | ||
if (!products || products.length === 0) | ||
return res.json({ statusText: 'NoProductsFound' }).status(404); | ||
|
||
return res.json({ statusText: 'done', products }); | ||
} | ||
|
||
async deleteProduct(req: Request, res: Response) { | ||
const { id } = req.query; | ||
|
||
await ProductModel.findByIdAndDelete(id); | ||
|
||
return res.json({ statusText: 'done' }); | ||
} | ||
|
||
async editProduct(req: Request, res: Response) { | ||
const { name, price, photo, description, category } = req.body; | ||
const { id } = req.query; | ||
|
||
const product = await ProductModel.findById(id); | ||
if (!product) | ||
return res.json({ statusText: 'ProductNotFound' }).status(404); | ||
|
||
if (name) product.name = name; | ||
if (price) product.price = price; | ||
if (photo) product.photo = photo; | ||
if (description) product.description = description; | ||
if (category) product.category = category; | ||
|
||
await product.save(); | ||
|
||
return res.json({ statusText: 'done' }); | ||
} | ||
|
||
async getOneProduct(req: Request, res: Response) { | ||
const { id } = req.query; | ||
|
||
const product = await ProductModel.findById(id); | ||
if (!product) | ||
return res.json({ statusText:'ProductNotFound' }).status(404); | ||
|
||
return res.json({ statusText:'done', product }); | ||
} | ||
|
||
} | ||
|
||
const productControllers = new ProductControllers(); | ||
export default productControllers; |
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,26 @@ | ||
import { Schema, model, Document } from 'mongoose'; | ||
|
||
const validCategories = { | ||
values: ['MALE', 'FEMALE', 'OTHER'], | ||
message: '{VALUE} it\'s not a valid category' | ||
}; | ||
|
||
const ProductSchema = new Schema({ | ||
name: { type: String, required: true }, | ||
price: { type: Number, required: true }, | ||
photo: { type: String, required: true }, | ||
description: { type: String, required: true }, | ||
category: { type: String, default: 'OTHER', enum: validCategories, required: false }, | ||
},{ | ||
timestamps: true | ||
}); | ||
|
||
export interface IProduct extends Document { | ||
name: string; | ||
price: number; | ||
photo: string; | ||
description: string; | ||
category: string; | ||
} | ||
|
||
export default model<IProduct>('Product', ProductSchema); |
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,32 @@ | ||
import { Router } from 'express'; | ||
|
||
import productControllers from '../controllers/product.controllers'; | ||
|
||
const router = Router(); | ||
|
||
router.get( | ||
'/getall', | ||
productControllers.getProducts | ||
); | ||
|
||
router.post( | ||
'/create', | ||
productControllers.createProduct | ||
); | ||
|
||
router.put( | ||
'/edit', | ||
productControllers.editProduct | ||
); | ||
|
||
router.delete( | ||
'/delete:id', | ||
productControllers.deleteProduct | ||
); | ||
|
||
router.get( | ||
'/get:id', | ||
productControllers.getOneProduct | ||
); | ||
|
||
export default router; |
Oops, something went wrong.