Skip to content
Snippets Groups Projects
Commit 763e3353 authored by Tucker Gary Siegel's avatar Tucker Gary Siegel
Browse files

Merge branch 'swagger-implementation' into 'master'

Swagger implementation

See merge request !2
parents 31e609bb 0a79d331
No related branches found
No related tags found
1 merge request!2Swagger implementation
......@@ -12,6 +12,7 @@ The data in mongodb has the following fields:
* ```min_temps``` - Minimum daily temperature as an array. One element is one day
* ```max_temps``` - Maximum daily temperature as an array. One element is one day
Go to ```/docs``` to view the Swagger generated API docs
#### API Endpoints
* ```POST /api/:product/:year```
* Required url params: ```product``` and ```year```. Product is the crop (supports corn, soybean, wheat, tomatoes, potatoes, peas, sunflowers, sugar beets, etc.)
......
Gdd = require('./model');
/**
* @route POST /api/:product/:year
* @param {string} email.query.required - username or email - eg: user@domain
* @param {string} password.query.required - user's password.
* @returns {object} 200 - An array of user info
* @returns {Error} default - Unexpected error
*/
exports.year_gdd = function (req, res) {
var year = parseInt(req.params.year)
var product = req.params.product
......@@ -28,6 +37,21 @@ exports.year_gdd = function (req, res) {
var t_max = 86
var t_min = 50
errors = []
if (year < 1981 || year > new Date().getFullYear()) {
errors.push({
parameter_error: "year",
message: year.toString() + " is out of bounds for GDD calculations. Must be between 1981 - Current Year"
});
}
if (latitude < 24.083334 || latitude > 49.916668) {
errors.push({
parameter_error: "latitude",
message: latitude.toString() + " is out of bounds for GDD calculations. Must be between 24.083334 - 49.916668"
});
if (req.body.hasOwnProperty("t_base")) {
t_base = parseFloat(req.body.t_base);
if (t_base < t_min) {
......@@ -61,11 +85,27 @@ exports.year_gdd = function (req, res) {
t_min = 32;
break;
default:
res.status(404).send(product + " is not available for GDD calculations");
errors.push({
parameter_error: "product",
message: product + " is not available for GDD calculations"
});
break;
}
}
if (longitude < -125 || longitude > -66.5) {
errors.push({
parameter_error: "longitude",
message: longitude.toString() + " is out of bounds for GDD calculations. Must be between -125.0 - -66.5"
});
}
if (errors.length > 0) {
res.status(400).send({
errors: errors
})
}
Gdd.findOne(query, projection).then(function(data) {
var min_temps = data["min_temps"]
......
This diff is collapsed.
......@@ -12,6 +12,8 @@
"body-parser": "^1.19.0",
"express": "^4.17.1",
"mongoose": "^5.11.15",
"nodemon": "^2.0.7"
"nodemon": "^2.0.7",
"swagger-jsdoc": "5.0.1",
"swagger-ui-express": "^4.1.6"
}
}
......@@ -2,8 +2,99 @@
let router = require('express').Router();
var gddController = require('./gddController');
/**
* @swagger
* api/{product}/{year}:
* post:
* summary: Returns GDD data
* description: Returns GDD data for a specific product, year, lat, and lon
* parameters:
* - in: path
* name: product
* required: true
* description: Agricultural product to calculate gdd for
* schema:
* type: string
* enum: [corn, soybean, sugar_beet, sunflower, tomato, potato, wheat, peas, parsley, brussels_sprouts, cabbage]
* - in: path
* name: year
* required: true
* description: Year to calculate gdd on
* schema:
* type: integer
* minimum: 1981
* - in: body
* description: Data to calculate gdd on
* schema:
* type: object
* required:
* - longitude
* - latitude
* properties:
* latitude:
* description: latitude to calculate gdd on
* type: number
* minimum: 24.083334
* maximum: 49.916668
* example: 25.6
* longitude:
* description: longitude to calculate gdd on
* type: number
* minimum: -125.0
* maximum: -66.5
* example: -78.5
* t_base:
* description: Base temperature to calculate gdd on, in fahrenheit
* type: number
* example: 50
*
* responses:
* 200:
* description: Success
* content:
* application/json:
* schema:
* type: object
* properties:
* message:
* type: string
* example: GDDs
* date:
* type: string
* format: date
* data:
* type: array
* items:
* type: number
* minItems: 1
* maxItems: 365
* 400:
* description: Bad Request
* content:
* application/json:
* schema:
* type: object
* properties:
* errors:
* type: array
* items:
* type: object
* properties:
* parameter_error:
* type: string
* example: latitude
* message:
* type: string
* example: 22.5 is out of bounds for GDD calculations. Must be between 24.083334 - 49.916668
*
*
*
*
*/
router.route('/:product/:year')
.post(gddController.year_gdd)
module.exports = router;
\ No newline at end of file
module.exports = router;
const express = require('express');
let bodyParser = require('body-parser');
// const express = require('express');
// let bodyParser = require('body-parser');
let mongoose = require('mongoose');
require('./model.js');
const swaggerJSDoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const port = 3000;
// var swaggerJsdoc = require("swagger-jsdoc");
// var swaggerUi = require("swagger-ui-express");
var express = require("express"),
bodyParser = require("body-parser");
// swaggerUi = require("swagger-ui-express");
require('./model.js');
const port = 4000;
const app = express();
const expressSwagger = require('express-swagger-generator')(app);
let apiRoutes = require("./routes")
//Use API routes in the App
......@@ -17,6 +27,30 @@ const dbPath = 'mongodb://localhost/gdd_database';
const options = {useNewUrlParser: true, useUnifiedTopology: true}
const mongo = mongoose.connect(dbPath, options);
const swaggerDefinition = {
openapi: '3.0.0',
info: {
title: 'Express API for JSONPlaceholder',
version: '1.0.0',
},
servers: [
{
url: 'http://localhost:4000',
description: 'GDD server',
},
],
};
const swagger_options = {
swaggerDefinition,
// Paths to files containing OpenAPI definitions
apis: ['./routes.js'],
};
const swaggerSpec = swaggerJSDoc(swagger_options);
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
app.listen(port, function () {
console.log("Server is running on "+ port +" port");
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment