Last active
November 25, 2018 08:50
-
-
Save SaifRehman/57fbd66d0cf4cebc77781c89946856ac to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import * as mongoose from "mongoose"; | |
| import { ItemSchema } from "../models/crmModel"; | |
| import { Request, Response } from "express"; | |
| const Item = mongoose.model("Item", ItemSchema); | |
| export class ItemController { | |
| public addNewItem(req: Request, res: Response) { | |
| res.setHeader('Content-Type', 'application/json'); | |
| let newItem = new Item(req.body); | |
| newItem.save((err, data) => { | |
| if (err) { | |
| res.status(404).json({ err }); | |
| return; | |
| } | |
| res.json(data); | |
| }); | |
| } | |
| public getItem(req: Request, res: Response) { | |
| res.setHeader('Content-Type', 'application/json'); | |
| Item.find({}, (err, data) => { | |
| if (err) { | |
| res.status(404).json({ err }); | |
| return; | |
| } else { | |
| res.status(200).send(data); | |
| } | |
| }); | |
| } | |
| public getItemById(req: Request, res: Response) { | |
| res.setHeader('Content-Type', 'application/json'); | |
| Item.findById(req.params.ID, (err, data) => { | |
| if (err) { | |
| res.status(404).json({ err }); | |
| return; } else { | |
| res.status(200).send(data); | |
| } | |
| }); | |
| } | |
| public updateItem(req: Request, res: Response) { | |
| res.setHeader('Content-Type', 'application/json'); | |
| Item.findOneAndUpdate( | |
| { _id: req.params.ID }, req.body, { new: true },(err, data) => { | |
| if (err) { | |
| res.status(404).json({ err }); | |
| return; } | |
| res.json(data); | |
| } | |
| ); | |
| } | |
| public deleteItem (req: Request, res: Response) { | |
| res.setHeader('Content-Type', 'application/json'); | |
| Item.deleteOne({ _id: req.params.ID }, (err) => { | |
| if(err){ | |
| res.status(404).json({ err }); | |
| return; | |
| } | |
| res.json({ message: "success"}); | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment