Create New Advertisements - noemenend/Nodepop GitHub Wiki
We must bear in mind that for this it is necessary to pass a token data by query to confirm the authentication, otherwise the api will not give us access to anything.The API allow create new adverts with these parameters:
- nombre: String (required)
- venta : Boolean (required)
- precio : Numeric (required)
- foto: File (required). The image will be stored in /public/images
- tags: tag that belongs to one of these categories ['Lifestyle', 'work', 'mobile', 'motor'] - required.
If one of these tags is missed on the request or contains an incorrect value the API will return an error in HTML format and the advert will not be created. If all was ok, the API return the advertisement created in JSON format
URL : /api/v1/advertisements/?token=xxxxxxxxx where xxxxxxx: token generated throught the authentication method.
Method: POST
{
"success": true,
"result": {
"foto": "cd-437725_640.jpg",
"tags": [
"work"
],
"_id": "5b66239b4fa9cc3f9892b841",
"nombre": "cd-rom pack 9",
"venta": true,
"precio": 9.8,
"__v": 0
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>ValidationError: Advertisement validation failed: nombre: Path `nombre` is required.
<br> at new ValidationError (C:\Personal\KEEPCODING\BOOTCAMP - WEB\03-Fundamentos Node\Practica\nodepop\node_modules\mongoose\lib\error\validation.js:27:11)
<br> at model.Document.invalidate (C:\Personal\KEEPCODING\BOOTCAMP - WEB\03-Fundamentos Node\Practica\nodepop\node_modules\mongoose\lib\document.js:1876:32)
<br> at p.doValidate.skipSchemaValidators (C:\Personal\KEEPCODING\BOOTCAMP - WEB\03-Fundamentos Node\Practica\nodepop\node_modules\mongoose\lib\document.js:1744:17)
<br> at C:\Personal\KEEPCODING\BOOTCAMP - WEB\03-Fundamentos Node\Practica\nodepop\node_modules\mongoose\lib\schematype.js:808:9
<br> at _combinedTickCallback (internal/process/next_tick.js:131:7)
<br> at process._tickCallback (internal/process/next_tick.js:180:9)
</pre>
</body>
</html>
Moreover, the post method of the api will call a microservice that will generate the thumbnail of the image on the rute /public/images with the name of the original file ended with _small.
To run the microservice you must execute the following sentence
$ npm run createThumb
If the microservice was not running the different requests made will be processed once the microservice was started.