From 4584f63334d26df331abd18363ed29aff0869a98 Mon Sep 17 00:00:00 2001
From: tuckersiegel <siegel.tucker@gmail.com>
Date: Mon, 22 Feb 2021 21:19:57 -0500
Subject: [PATCH] futher file structure changes

---
 controllers/gddAccumulatedController.js       | 74 +++++++++++++++++
 .../product/accumulated/accumulated_year.js   |  6 --
 .../daily/{daily_year.js => daily_routes.js}  |  2 +
 .../normal/{normal.js => normal_routes.js}    |  4 +-
 routes/api/product/product_index.js           |  5 +-
 swagger_definition.yaml                       | 83 ++++++++++++++++++-
 6 files changed, 163 insertions(+), 11 deletions(-)
 delete mode 100644 routes/api/product/accumulated/accumulated_year.js
 rename routes/api/product/daily/{daily_year.js => daily_routes.js} (55%)
 rename routes/api/product/normal/{normal.js => normal_routes.js} (51%)

diff --git a/controllers/gddAccumulatedController.js b/controllers/gddAccumulatedController.js
index d9fb707..1a733bd 100644
--- a/controllers/gddAccumulatedController.js
+++ b/controllers/gddAccumulatedController.js
@@ -1,5 +1,6 @@
 gdd = require('../models/gdd.js');
 gdd_current = require('../models/gdd_current.js');
+gdd_normal = require('../models/normals.js');
 utils = require('../lib/utils');
 
 function send_response(message, gdds, data, res) {
@@ -122,4 +123,77 @@ exports.accumulated_gdd = function (req, res) {
         find(gdd_current, query, projection, temps, res);
     }
    
+};
+
+
+exports.accumulated_normal_gdd = function (req, res) {
+
+    var product = req.params.product;
+
+    var latitude = parseFloat(req.body.latitude)
+    var longitude = parseFloat(req.body.longitude)
+
+    var query = {
+        location: { 
+            "$near": {
+                "$geometry": {
+                    "type": "Point", 
+                    "coordinates": [longitude, latitude]
+                },
+            },
+        },
+    }
+
+    var t_base = 50
+    var t_max = 86
+    var t_min = 50 
+
+    errors = []
+
+    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) {
+            t_min = t_base;
+        }
+    } else {
+        out = utils.product_base_switch(product, errors);
+        t_base = out.t_base;
+        t_min = out.t_min;
+        errors = out.errors;
+    }
+    
+
+    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
+        })
+    }
+
+    var projection = {
+        min_temps: 1,
+        max_temps: 1,
+        location: 1,
+    }
+
+    temps = {
+        t_base: t_base,
+        t_max: t_max,
+        t_min: t_min,
+    }
+
+    find(gdd_normal, query, projection, temps, res);   
 };
\ No newline at end of file
diff --git a/routes/api/product/accumulated/accumulated_year.js b/routes/api/product/accumulated/accumulated_year.js
deleted file mode 100644
index 42830e8..0000000
--- a/routes/api/product/accumulated/accumulated_year.js
+++ /dev/null
@@ -1,6 +0,0 @@
-const router = require('express').Router();
-const controller = require("../../../../controllers/gddAccumulatedController")
-
-router.route("/:year").post(controller.accumulated_gdd)
-
-module.exports = router;
diff --git a/routes/api/product/daily/daily_year.js b/routes/api/product/daily/daily_routes.js
similarity index 55%
rename from routes/api/product/daily/daily_year.js
rename to routes/api/product/daily/daily_routes.js
index 4b74c66..2c2e85f 100644
--- a/routes/api/product/daily/daily_year.js
+++ b/routes/api/product/daily/daily_routes.js
@@ -1,6 +1,8 @@
 const router = require('express').Router();
+const accController = require("../../../../controllers/gddAccumulatedController")
 const controller = require("../../../../controllers/gddController")
 
+router.route("/:year/accumulated").post(accController.accumulated_gdd)
 router.route("/:year").post(controller.year_gdd)
 
 module.exports = router;
diff --git a/routes/api/product/normal/normal.js b/routes/api/product/normal/normal_routes.js
similarity index 51%
rename from routes/api/product/normal/normal.js
rename to routes/api/product/normal/normal_routes.js
index 302bccc..9f47361 100644
--- a/routes/api/product/normal/normal.js
+++ b/routes/api/product/normal/normal_routes.js
@@ -1,6 +1,8 @@
 const router = require('express').Router();
-const controller = require("../../../../controllers/gddNormalController")
+const controller = require("../../../../controllers/gddNormalController");
+const accController = require("../../../../controllers/gddAccumulatedController");
 
 router.route("/").post(controller.normal)
+router.route("/accumulated/").post(accController.accumulated_normal_gdd)
 
 module.exports = router;
diff --git a/routes/api/product/product_index.js b/routes/api/product/product_index.js
index d669452..05b1cc6 100644
--- a/routes/api/product/product_index.js
+++ b/routes/api/product/product_index.js
@@ -1,7 +1,6 @@
 const router = require('express').Router();
 
-router.use('/normal', require('./normal/normal'));
-router.use('/daily', require('./daily/daily_year'));
-router.use('/accumulated', require('./accumulated/accumulated_year'));
+router.use('/normal', require('./normal/normal_routes'));
+router.use('/daily', require('./daily/daily_routes'));
 
 module.exports = router;
diff --git a/swagger_definition.yaml b/swagger_definition.yaml
index b9113e5..71b8d7d 100644
--- a/swagger_definition.yaml
+++ b/swagger_definition.yaml
@@ -177,7 +177,88 @@ paths:
                                               message:
                                                   type: string
                                                   example: 22.5 is out of bounds for GDD calculations. Must be between 24.083334 - 49.916668
-    /api/{product}/accumulated/{year}:
+    /api/{product}/normal/accumulated:
+      post:
+          summary: Returns accumulated GDD data on a 30 year normal
+          description: Returns accumulated GDD normals for a specific 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]
+          requestBody:
+              content:
+                  application/json:
+                      schema:
+                          type: object
+                          required:
+                              - longitude
+                              - latitude
+                          properties:
+                              latitude:
+                                  description: latitude to calculate gdd on
+                                  type: number
+                                  minimum: 24.083334
+                                  maximum: 49.916668
+                                  example: 38.99
+                              longitude:
+                                  description: longitude to calculate gdd on
+                                  type: number
+                                  minimum: -125.0
+                                  maximum: -66.5
+                                  example: -76.94
+                              t_base:
+                                  description: Base temperature to calculate gdd on, in fahrenheit. NOT REQUIRED
+                                  type: number
+                                  example: 50
+                  
+          responses:
+              200:
+                  description: Success
+                  content:
+                      application/json:
+                          schema:
+                              type: object
+                              properties:
+                                  message:
+                                      type: string
+                                      example: 30-year normal GDDs
+                                  data:
+                                      type: array
+                                      items:
+                                          type: number
+                                  closest_lat:
+                                      type: number
+                                      minimum: 24.083334
+                                      maximum: 49.916668
+                                      example: 38.99
+                                  closest_lon:
+                                      type: number
+                                      minimum: -125.0
+                                      maximum: -66.5
+                                      example: -78.5
+              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
+    /api/{product}/daily/{year}/accumulated:
       post:
           summary: Returns accumulated GDD data
           description: Returns accumulated GDD data for a specific product, year, lat, and lon
-- 
GitLab