Skip to content

Commit

Permalink
Merge with dev branch & Added some docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Totoratsu committed Nov 18, 2020
1 parent 4f37c71 commit e4171f6
Show file tree
Hide file tree
Showing 48 changed files with 9,225 additions and 1,593 deletions.
21 changes: 21 additions & 0 deletions LICENSE
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.
24 changes: 24 additions & 0 deletions README.md
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/)
11 changes: 2 additions & 9 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,21 @@
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt-nodejs": "0.0.3",
"cors": "^2.8.5",
"debug": "^4.1.1",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.10.14",
"morgan": "^1.10.0",
"passport": "^0.4.1",
"passport-jwt": "^4.0.0"
"morgan": "^1.10.0"
},
"devDependencies": {
"@types/bcrypt-nodejs": "0.0.31",
"@types/cors": "^2.8.6",
"@types/debug": "^4.1.5",
"@types/dotenv": "^8.2.0",
"@types/express": "^4.17.3",
"@types/jsonwebtoken": "^8.3.8",
"@types/mongoose": "^5.10.0",
"@types/morgan": "^1.9.2",
"@types/node": "^14.0.24",
"@types/passport": "^1.0.4",
"@types/passport-jwt": "^3.0.3",
"nodemon": "^2.0.2",
"ts-node": "^8.7.0",
"typescript": "^3.9.7"
Expand Down
57 changes: 52 additions & 5 deletions backend/src/app.ts
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'}`);
66 changes: 66 additions & 0 deletions backend/src/controllers/product.controllers.ts
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;
26 changes: 26 additions & 0 deletions backend/src/models/product.model.ts
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);
32 changes: 32 additions & 0 deletions backend/src/routes/product.routes.ts
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;
Loading

0 comments on commit e4171f6

Please sign in to comment.