#4 defined location model

parent cb658c25
......@@ -2,8 +2,6 @@ import {Request, Response} from 'express';
import {Model} from 'mongoose';
const create = (model: Model<any>) => (req: Request, res: Response) => {
console.log(`Create new document for ${model.modelName}`);
const doc = new model({
...req.body,
});
......@@ -14,11 +12,11 @@ const create = (model: Model<any>) => (req: Request, res: Response) => {
.catch((error: any) => res.status(500).json({error}));
};
const getAll = (model: Model<any>) => (req: Request, res: Response) => {
console.log(`Getting all documents for ${model.modelName}`);
const getAll =
(model: Model<any>, populate?: string[]) => (req: Request, res: Response) => {
model
.find<Document>({}, {description: 0})
.populate(populate || [])
.then((results) => {
console.log(results);
return res.status(200).send(results);
......@@ -27,9 +25,30 @@ const getAll = (model: Model<any>) => (req: Request, res: Response) => {
console.log(error);
return res.status(500).json({error});
});
};
};
const get =
(model: Model<any>, populate?: string[]) => (req: Request, res: Response) => {
const id = req.params.id;
model
.findById<Document>(id)
.populate(populate || [])
.then((result) => {
if (result) {
return res.status(200).send(result);
} else {
return res.status(404).json({message: 'Not found'});
}
})
.catch((error) => {
console.log(error);
return res.status(500).json({error});
});
};
export default {
create,
getAll,
get,
};
......@@ -3,6 +3,7 @@ import 'dotenv/config';
import cors from 'cors';
import {connect} from 'mongoose';
import categoryRouter from './routes/Category';
import locationRouter from './routes/Location';
const app: Express = express();
app.use(json());
......@@ -19,3 +20,4 @@ app.listen(process.env.PORT, () => {
});
app.use('/categories', categoryRouter);
app.use('/locations', locationRouter);
import mongoose, {Document, Schema} from 'mongoose';
export interface ILocation {
name: string;
description: string;
}
export interface ILocationModel extends ILocation, Document {}
const LocationSchema: Schema = new Schema(
{
name: {
type: String,
required: true,
},
description: {
type: String,
},
},
{versionKey: false}
);
export default mongoose.model<ILocationModel>('Location', LocationSchema);
import express, {Router} from 'express';
import controller from '../controllers/Generic';
import model from '../models/Location';
const router: Router = express.Router();
router.get('/', controller.getAll(model));
router.post('/', controller.create(model));
router.get('/:id', controller.get(model));
export = router;
import express, {Router} from 'express';
import controller from '../controllers/Generic';
import model from '../models/category';
import model from '../models/Category';
const router: Router = express.Router();
router.get('/', controller.getAll(model));
router.post('/', controller.create(model));
router.get('/:id', controller.get(model));
export = router;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment