cheked whether required fields are not empty

parent 9ebab5b6
......@@ -9,6 +9,7 @@
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
......@@ -275,12 +276,12 @@
}
},
"node_modules/body-parser": {
"version": "1.20.1",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz",
"integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==",
"version": "1.20.2",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz",
"integrity": "sha512-ml9pReCu3M61kGlqoTm2umSXTlRTuGTx0bfYj+uIUKKYycG5NtSbeetV3faSU6R7ajOPw0g/J1PvK4qNy7s5bA==",
"dependencies": {
"bytes": "3.1.2",
"content-type": "~1.0.4",
"content-type": "~1.0.5",
"debug": "2.6.9",
"depd": "2.0.0",
"destroy": "1.2.0",
......@@ -288,7 +289,7 @@
"iconv-lite": "0.4.24",
"on-finished": "2.4.1",
"qs": "6.11.0",
"raw-body": "2.5.1",
"raw-body": "2.5.2",
"type-is": "~1.6.18",
"unpipe": "1.0.0"
},
......@@ -576,6 +577,43 @@
"node": ">= 0.10.0"
}
},
"node_modules/express/node_modules/body-parser": {
"version": "1.20.1",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz",
"integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==",
"dependencies": {
"bytes": "3.1.2",
"content-type": "~1.0.4",
"debug": "2.6.9",
"depd": "2.0.0",
"destroy": "1.2.0",
"http-errors": "2.0.0",
"iconv-lite": "0.4.24",
"on-finished": "2.4.1",
"qs": "6.11.0",
"raw-body": "2.5.1",
"type-is": "~1.6.18",
"unpipe": "1.0.0"
},
"engines": {
"node": ">= 0.8",
"npm": "1.2.8000 || >= 1.4.16"
}
},
"node_modules/express/node_modules/raw-body": {
"version": "2.5.1",
"resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz",
"integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==",
"dependencies": {
"bytes": "3.1.2",
"http-errors": "2.0.0",
"iconv-lite": "0.4.24",
"unpipe": "1.0.0"
},
"engines": {
"node": ">= 0.8"
}
},
"node_modules/fill-range": {
"version": "7.0.1",
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",
......@@ -1180,9 +1218,9 @@
}
},
"node_modules/raw-body": {
"version": "2.5.1",
"resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz",
"integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==",
"version": "2.5.2",
"resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.2.tgz",
"integrity": "sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==",
"dependencies": {
"bytes": "3.1.2",
"http-errors": "2.0.0",
......@@ -1924,12 +1962,12 @@
"integrity": "sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA=="
},
"body-parser": {
"version": "1.20.1",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz",
"integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==",
"version": "1.20.2",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz",
"integrity": "sha512-ml9pReCu3M61kGlqoTm2umSXTlRTuGTx0bfYj+uIUKKYycG5NtSbeetV3faSU6R7ajOPw0g/J1PvK4qNy7s5bA==",
"requires": {
"bytes": "3.1.2",
"content-type": "~1.0.4",
"content-type": "~1.0.5",
"debug": "2.6.9",
"depd": "2.0.0",
"destroy": "1.2.0",
......@@ -1937,7 +1975,7 @@
"iconv-lite": "0.4.24",
"on-finished": "2.4.1",
"qs": "6.11.0",
"raw-body": "2.5.1",
"raw-body": "2.5.2",
"type-is": "~1.6.18",
"unpipe": "1.0.0"
}
......@@ -2156,6 +2194,38 @@
"type-is": "~1.6.18",
"utils-merge": "1.0.1",
"vary": "~1.1.2"
},
"dependencies": {
"body-parser": {
"version": "1.20.1",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz",
"integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==",
"requires": {
"bytes": "3.1.2",
"content-type": "~1.0.4",
"debug": "2.6.9",
"depd": "2.0.0",
"destroy": "1.2.0",
"http-errors": "2.0.0",
"iconv-lite": "0.4.24",
"on-finished": "2.4.1",
"qs": "6.11.0",
"raw-body": "2.5.1",
"type-is": "~1.6.18",
"unpipe": "1.0.0"
}
},
"raw-body": {
"version": "2.5.1",
"resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz",
"integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==",
"requires": {
"bytes": "3.1.2",
"http-errors": "2.0.0",
"iconv-lite": "0.4.24",
"unpipe": "1.0.0"
}
}
}
},
"fill-range": {
......@@ -2585,9 +2655,9 @@
"integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg=="
},
"raw-body": {
"version": "2.5.1",
"resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz",
"integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==",
"version": "2.5.2",
"resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.2.tgz",
"integrity": "sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==",
"requires": {
"bytes": "3.1.2",
"http-errors": "2.0.0",
......
......@@ -10,6 +10,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
......@@ -23,4 +24,4 @@
"@types/mongoose": "^5.11.97",
"@types/multer": "^1.4.7"
}
}
\ No newline at end of file
}
......@@ -2,6 +2,10 @@ import {Request, Response} from 'express';
import {Model} from 'mongoose';
const create = (model: Model<any>) => (req: Request, res: Response) => {
if (!req.body.name) {
res.json({error: 'Name is required'});
return;
}
const doc = new model({
...req.body,
});
......@@ -18,7 +22,6 @@ const getAll =
.find<Document>({}, {description: 0})
.populate(populate || [])
.then((results) => {
console.log(results);
return res.status(200).send(results);
})
.catch((error) => {
......
......@@ -4,10 +4,14 @@ import cors from 'cors';
import {connect} from 'mongoose';
import categoryRouter from './routes/Category';
import locationRouter from './routes/Location';
import productRouter from './routes/Product';
const app: Express = express();
app.use(json());
app.use(cors());
app.use('/categories', categoryRouter);
app.use('/locations', locationRouter);
app.use('/products', productRouter);
const run = async () => {
await connect(`${process.env.MONGO_URL}/office`);
......@@ -18,6 +22,3 @@ run().catch((err) => console.log(err));
app.listen(process.env.PORT, () => {
console.log('Server started on port ' + process.env.PORT);
});
app.use('/categories', categoryRouter);
app.use('/locations', locationRouter);
......@@ -2,7 +2,7 @@ import mongoose, {Document, Schema} from 'mongoose';
export interface ICategory {
name: string;
description: string;
description?: string;
}
export interface ICategoryModel extends ICategory, Document {}
......
......@@ -2,7 +2,7 @@ import mongoose, {Document, Schema} from 'mongoose';
export interface ILocation {
name: string;
description: string;
description?: string;
}
export interface ILocationModel extends ILocation, Document {}
......
import mongoose, {Document, Schema, Types} from 'mongoose';
export interface IProduct {
category: Types.ObjectId;
location: Types.ObjectId;
name: string;
description?: string;
image?: File;
}
export interface IProductModel extends IProduct, Document {}
const ProductSchema: Schema = new Schema(
{
category: {
type: Schema.Types.ObjectId,
ref: 'Category',
required: true,
},
location: {
type: Schema.Types.ObjectId,
ref: 'Location',
required: true,
},
name: {
type: String,
required: true,
},
description: {
type: String,
},
image: {
data: Buffer,
contentType: String,
},
},
{versionKey: false}
);
export default mongoose.model<IProductModel>('Product', ProductSchema);
import express, {Request, Response, Router} from 'express';
import controller from '../controllers/Generic';
import model from '../models/Product';
import multer from 'multer';
import fs from 'fs';
import path from 'path';
const upload = multer({dest: 'uploads/products'});
const router: Router = express.Router();
router.get('/', controller.getAll(model, ['category', 'location']));
router.get('/:id', controller.get(model, ['category', 'location']));
router.post(
'/',
upload.single('image'),
async (req: Request, res: Response) => {
try {
if (!req.body.category || !req.body.location || !req.body.name) {
res.json({error: 'Category, location and name are required'});
return;
}
let product;
if (!req.file) {
product = new model({
...req.body,
});
} else {
product = new model({
...req.body,
image: {
data: fs.readFileSync(
path.join('uploads/products/' + req.file?.filename)
),
contentType: 'image',
},
});
}
await product.save();
res.status(200).send(product);
} catch (err: unknown) {
res.status(500).send(err);
}
}
);
export = router;
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="175.7519989013672"
height="36"
viewBox="0 0 175.7519989013672 36"
class="css-1j8o68f"
version="1.1"
id="svg9"
sodipodi:docname="logo.svg">
<metadata
id="metadata13">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
</cc:Work>
</rdf:RDF>
</metadata>
<sodipodi:namedview
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1"
objecttolerance="10"
gridtolerance="10"
guidetolerance="10"
inkscape:pageopacity="0"
inkscape:pageshadow="2"
inkscape:window-width="640"
inkscape:window-height="480"
id="namedview11" />
<defs
id="SvgjsDefs3689" />
<g
id="SvgjsG3690"
featurekey="nameLeftFeature-0"
transform="matrix(2.4861879239010025,0,0,2.4861879239010025,-6.2154698097525065,-13.07734904876253)"
fill="#3a3a3a">
<path
d="M7.64 16.66 l-4.14 -7.42 l0 10.26 l-1 0 l0 -14 l6.3 11.16 l-1.16 0 z M14 19.5 l-1 0 l0 -10.26 l-3.9 6.9 l-6 -10.64 l1.14 0 l4.86 8.66 l4.9 -8.66 l0 14 z M12.26 5.5 l1.14 0 l-4.3 7.6 l-0.56 -0.94 z M5 19.5 l-1 0 l0 -8.34 l1 1.74 l0 6.6 z M11.5 19.5 l0 -6.6 l1 -1.74 l0 8.34 l-1 0 z M19 5.5 l1 0 l0 14 l-1 0 l0 -14 z M20.5 5.5 l1 0 l0 14 l-1 0 l0 -14 z M26 10.26 c0 -3 1.8 -5 4.5 -5 s4.5 2 4.5 5 l-1 0 c0 -2.4 -1.4 -4.02 -3.5 -4.02 s-3.5 1.62 -3.5 4.02 l0 4.5 c0 2.4 1.4 4 3.5 4 c0.96 0 1.74 -0.36 2.34 -0.96 l0.76 0.74 c-0.8 0.76 -1.84 1.2 -3.1 1.2 c-2.7 0 -4.5 -1.98 -4.5 -4.98 l0 -4.5 z M30.5 7.74 c-0.5 0 -0.9 0.16 -1.26 0.46 l-0.74 -0.7 c0.56 -0.5 1.2 -0.74 2 -0.74 c1.8 0 3 1.4 3 3.5 l-1 0 c0 -1.5 -0.8 -2.52 -2 -2.52 z M30.5 17.259999999999998 c1.2 0 2 -1 2 -2.5 l1 0 c0 2.1 -1.2 3.48 -3 3.48 s-3 -1.38 -3 -3.48 l0 -4.5 c0 -0.96 0.24 -1.76 0.7 -2.36 l0.7 0.7 c-0.26 0.46 -0.4 1 -0.4 1.66 l0 4.5 c0 1.5 0.8 2.5 2 2.5 z M35 14.76 c0 1.38 -0.4 2.54 -1.06 3.4 l-0.74 -0.7 c0.5 -0.72 0.8 -1.62 0.8 -2.7 l1 0 z"
id="path3" />
</g>
<g
id="SvgjsG3691"
featurekey="nameRightFeature-0"
transform="matrix(2.4,0,0,2.4,92.37600059509278,-12)"
fill="#ffffff">
<path
d="M4.64 17.759999999999998 l-0.74 2.24 l-3.64 0 l4.98 -15 l2.26 0 l5 15 l-3.66 0 l-0.78 -2.24 l-3.42 0 z M3.6 14.76 l3.04 -9.26 l-1.04 0 l-4.66 14 l1.06 0 l1.26 -3.74 l4.14 0 l-0.36 -1 l-3.44 0 z M7.16 8.7 l3.6 10.8 l1.04 0 l-4.6 -14 l-2.96 8.76 l1.06 0 z M3.94 16.259999999999998 l0.7 1 l3.76 0 l0.74 2.24 l1.1 0 l-3.08 -9.24 l-0.52 1.54 l1.46 4.46 l-4.16 0 z M6.34 12.66 l-0.54 1.6 l1.1 0 z M4.2 17.5 l-0.64 -1 l-1.06 3 l1.06 0 z M18.240000000000002 14.5 l0 2 l5 0 l0 3.5 l-8.5 0 l0 -15 l8.5 0 l0 3.5 l-5 0 l0 2.5 l4 0 l0 3.5 l-4 0 z M15.24 5.5 l0 12.5 l7.5 0 l0 -1 l-6.5 0 l0 -4.5 l5.5 0 l0 -1 l-5.5 0 l0 -5 l6.5 0 l0 -1 l-7.5 0 z M22.740000000000002 7 l-5.66 0 l1.06 1 l4.6 0 l0 -1 z M16.740000000000002 11 l1 0 l0 -2.66 l-1 -0.98 l0 3.64 z M21.740000000000002 13 l-4.66 0 l1.06 1 l3.6 0 l0 -1 z M16.740000000000002 16.5 l1 0 l0 -2.16 l-1 -0.98 l0 3.14 z M22.740000000000002 18.5 l-7.5 0 l0 1 l7.5 0 l0 -1 z M30.240000000000002 5 l0 11.5 l4.5 0 l0 3.5 l-8 0 l0 -15 l3.5 0 z M27.240000000000002 18 l7 0 l0 -1 l-6 0 l0 -11.5 l-1 0 l0 12.5 z M28.740000000000002 16.5 l1 0 l0 -11 l-1 0 l0 11 z M34.24 18.5 l-7 0 l0 1 l7 0 l0 -1 z"
id="path6" />
</g>
</svg>
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