From b84192e6749ada473e79f3fa346745a79ef14570 Mon Sep 17 00:00:00 2001
From: Tucker Gary Siegel <tgsiegel@terpmail.umd.edu>
Date: Thu, 30 Sep 2021 15:13:50 -0400
Subject: [PATCH] Merge work into master for now

---
 config/error_codes.go           |   6 ++
 controllers/routes.go           |   2 +-
 controllers/seed_controllers.go |  20 ++---
 docs/docs.go                    | 141 ++++++++++----------------------
 docs/swagger.json               | 141 ++++++++++----------------------
 docs/swagger.yaml               | 115 +++++++++-----------------
 models/seeds.go                 |   7 +-
 persistence/mongodb.go          |   4 +-
 services/seed_service.go        |   9 +-
 9 files changed, 148 insertions(+), 297 deletions(-)

diff --git a/config/error_codes.go b/config/error_codes.go
index 815d1a5..e0eac4a 100644
--- a/config/error_codes.go
+++ b/config/error_codes.go
@@ -32,6 +32,12 @@ var NO_DATA_FOUND = &common.DawnError{
 
 // 500s
 
+var DATABSE_CONNECTION_ERROR = &common.DawnError{
+	Name:        "DATABSE_CONNECTION_ERROR",
+	Description: "Cannot connect to database",
+	Code:        500,
+}
+
 var INTERNAL_SERVER_STANDARD_ERROR = &common.DawnError{
 	Name:        "INTERNAL_SERVER_ERROR",
 	Description: "Unkown internal server error occurred",
diff --git a/controllers/routes.go b/controllers/routes.go
index 2df6c3b..727996d 100644
--- a/controllers/routes.go
+++ b/controllers/routes.go
@@ -16,6 +16,6 @@ func GddRoutes(route fiber.Router) {
 
 	route.Get("gdd/csv", GetCSVFile)
 
-	route.Get("gdd/seeds/corn", GetCornSeedMaturityDate)
+	route.Get("gdd/maturity/corn", GetCornSeedMaturityDate)
 	route.Get("gdd/seeds", GetSeedList)
 }
diff --git a/controllers/seed_controllers.go b/controllers/seed_controllers.go
index 9e5ff9a..d6e28ee 100644
--- a/controllers/seed_controllers.go
+++ b/controllers/seed_controllers.go
@@ -8,16 +8,6 @@ import (
 	"github.com/gofiber/fiber/v2"
 )
 
-// GetSeedList godoc
-// @Summary Get list of seeds in database
-// @Tags GDD Seed Data
-// @Description Get list of seeds in database
-// @Accept  json
-// @Produce  json
-// @Success 200 {object} models.SeedListResponse
-// @Failure 400 {object} common.StandardError
-// @Param product query string true "Crop type to use" Enums(corn, soybean)
-// @Router /api/weather/gdd/seeds [get]
 func GetSeedList(c *fiber.Ctx) error {
 	request := models.SeedListRequest{Product: c.Query("product")}
 	return c.Status(fiber.StatusOK).JSON(
@@ -26,19 +16,19 @@ func GetSeedList(c *fiber.Ctx) error {
 }
 
 // GetCornSeedMaturityDate godoc
-// @Summary Get estimated maturity date from corn seed (uses CFS data if current GDUs are less than the crop maturity)
-// @Tags GDD Seed Data
-// @Description Get estimated maturity date from corn seed
+// @Summary Get estimated maturity date from given gdd (uses CFS data if current GDUs are less than the crop maturity)
+// @Tags GDD Maturity Data
+// @Description Get estimated maturity date from given gdd
 // @Accept  json
 // @Produce  json
 // @Success 200 {object} models.CornMaturityResponse
 // @Failure 400 {object} common.StandardError
 // @Param latitude query number true "Latitude to search for"
 // @Param longitude query number true "Longitude to search for"
-// @Param seed query string true "Corn seed to use"
+// @Param gdds query number true "number of gdds given"
 // @Param month query number true "month planted"
 // @Param date query number true "date planted"
-// @Router /api/weather/gdd/seeds/corn [get]
+// @Router /api/weather/gdd/maturity/corn [get]
 func GetCornSeedMaturityDate(c *fiber.Ctx) error {
 	ctx := common.DawnCtx{FiberCtx: c}
 	request := models.BuildCornMaturityRequest(c)
diff --git a/docs/docs.go b/docs/docs.go
index 3645be8..f978dfd 100644
--- a/docs/docs.go
+++ b/docs/docs.go
@@ -581,9 +581,9 @@ var doc = `{
                 }
             }
         },
-        "/api/weather/gdd/normals": {
+        "/api/weather/gdd/maturity/corn": {
             "get": {
-                "description": "get gdd normals",
+                "description": "Get estimated maturity date from given gdd",
                 "consumes": [
                     "application/json"
                 ],
@@ -591,33 +591,10 @@ var doc = `{
                     "application/json"
                 ],
                 "tags": [
-                    "Gdd"
+                    "GDD Maturity Data"
                 ],
-                "summary": "Get gdd normals",
+                "summary": "Get estimated maturity date from given gdd (uses CFS data if current GDUs are less than the crop maturity)",
                 "parameters": [
-                    {
-                        "enum": [
-                            "corn",
-                            "soybean",
-                            "sunflower",
-                            "tomato",
-                            "sugar_beet",
-                            "peanut",
-                            "cotton",
-                            "potato",
-                            "wheat",
-                            "pea",
-                            "oat",
-                            "spring_wheat",
-                            "rice",
-                            "sorghum"
-                        ],
-                        "type": "string",
-                        "description": "Crop to calculate gdd for",
-                        "name": "product",
-                        "in": "query",
-                        "required": true
-                    },
                     {
                         "type": "number",
                         "description": "Latitude to search for",
@@ -633,9 +610,23 @@ var doc = `{
                         "required": true
                     },
                     {
-                        "type": "boolean",
-                        "description": "Accumulate gdd values",
-                        "name": "accumulate",
+                        "type": "number",
+                        "description": "number of gdds given",
+                        "name": "gdds",
+                        "in": "query",
+                        "required": true
+                    },
+                    {
+                        "type": "number",
+                        "description": "month planted",
+                        "name": "month",
+                        "in": "query",
+                        "required": true
+                    },
+                    {
+                        "type": "number",
+                        "description": "date planted",
+                        "name": "date",
                         "in": "query",
                         "required": true
                     }
@@ -644,7 +635,7 @@ var doc = `{
                     "200": {
                         "description": "OK",
                         "schema": {
-                            "$ref": "#/definitions/models.GddResponse"
+                            "$ref": "#/definitions/models.CornMaturityResponse"
                         }
                     },
                     "400": {
@@ -656,9 +647,9 @@ var doc = `{
                 }
             }
         },
-        "/api/weather/gdd/seeds": {
+        "/api/weather/gdd/normals": {
             "get": {
-                "description": "Get list of seeds in database",
+                "description": "get gdd normals",
                 "consumes": [
                     "application/json"
                 ],
@@ -666,52 +657,33 @@ var doc = `{
                     "application/json"
                 ],
                 "tags": [
-                    "GDD Seed Data"
+                    "Gdd"
                 ],
-                "summary": "Get list of seeds in database",
+                "summary": "Get gdd normals",
                 "parameters": [
                     {
                         "enum": [
                             "corn",
-                            "soybean"
+                            "soybean",
+                            "sunflower",
+                            "tomato",
+                            "sugar_beet",
+                            "peanut",
+                            "cotton",
+                            "potato",
+                            "wheat",
+                            "pea",
+                            "oat",
+                            "spring_wheat",
+                            "rice",
+                            "sorghum"
                         ],
                         "type": "string",
-                        "description": "Crop type to use",
+                        "description": "Crop to calculate gdd for",
                         "name": "product",
                         "in": "query",
                         "required": true
-                    }
-                ],
-                "responses": {
-                    "200": {
-                        "description": "OK",
-                        "schema": {
-                            "$ref": "#/definitions/models.SeedListResponse"
-                        }
                     },
-                    "400": {
-                        "description": "Bad Request",
-                        "schema": {
-                            "$ref": "#/definitions/common.StandardError"
-                        }
-                    }
-                }
-            }
-        },
-        "/api/weather/gdd/seeds/corn": {
-            "get": {
-                "description": "Get estimated maturity date from corn seed",
-                "consumes": [
-                    "application/json"
-                ],
-                "produces": [
-                    "application/json"
-                ],
-                "tags": [
-                    "GDD Seed Data"
-                ],
-                "summary": "Get estimated maturity date from corn seed (uses CFS data if current GDUs are less than the crop maturity)",
-                "parameters": [
                     {
                         "type": "number",
                         "description": "Latitude to search for",
@@ -727,23 +699,9 @@ var doc = `{
                         "required": true
                     },
                     {
-                        "type": "string",
-                        "description": "Corn seed to use",
-                        "name": "seed",
-                        "in": "query",
-                        "required": true
-                    },
-                    {
-                        "type": "number",
-                        "description": "month planted",
-                        "name": "month",
-                        "in": "query",
-                        "required": true
-                    },
-                    {
-                        "type": "number",
-                        "description": "date planted",
-                        "name": "date",
+                        "type": "boolean",
+                        "description": "Accumulate gdd values",
+                        "name": "accumulate",
                         "in": "query",
                         "required": true
                     }
@@ -752,7 +710,7 @@ var doc = `{
                     "200": {
                         "description": "OK",
                         "schema": {
-                            "$ref": "#/definitions/models.CornMaturityResponse"
+                            "$ref": "#/definitions/models.GddResponse"
                         }
                     },
                     "400": {
@@ -961,17 +919,6 @@ var doc = `{
                     }
                 }
             }
-        },
-        "models.SeedListResponse": {
-            "type": "object",
-            "properties": {
-                "seeds": {
-                    "type": "array",
-                    "items": {
-                        "type": "string"
-                    }
-                }
-            }
         }
     }
 }`
diff --git a/docs/swagger.json b/docs/swagger.json
index a110e77..8907929 100644
--- a/docs/swagger.json
+++ b/docs/swagger.json
@@ -567,9 +567,9 @@
                 }
             }
         },
-        "/api/weather/gdd/normals": {
+        "/api/weather/gdd/maturity/corn": {
             "get": {
-                "description": "get gdd normals",
+                "description": "Get estimated maturity date from given gdd",
                 "consumes": [
                     "application/json"
                 ],
@@ -577,33 +577,10 @@
                     "application/json"
                 ],
                 "tags": [
-                    "Gdd"
+                    "GDD Maturity Data"
                 ],
-                "summary": "Get gdd normals",
+                "summary": "Get estimated maturity date from given gdd (uses CFS data if current GDUs are less than the crop maturity)",
                 "parameters": [
-                    {
-                        "enum": [
-                            "corn",
-                            "soybean",
-                            "sunflower",
-                            "tomato",
-                            "sugar_beet",
-                            "peanut",
-                            "cotton",
-                            "potato",
-                            "wheat",
-                            "pea",
-                            "oat",
-                            "spring_wheat",
-                            "rice",
-                            "sorghum"
-                        ],
-                        "type": "string",
-                        "description": "Crop to calculate gdd for",
-                        "name": "product",
-                        "in": "query",
-                        "required": true
-                    },
                     {
                         "type": "number",
                         "description": "Latitude to search for",
@@ -619,9 +596,23 @@
                         "required": true
                     },
                     {
-                        "type": "boolean",
-                        "description": "Accumulate gdd values",
-                        "name": "accumulate",
+                        "type": "number",
+                        "description": "number of gdds given",
+                        "name": "gdds",
+                        "in": "query",
+                        "required": true
+                    },
+                    {
+                        "type": "number",
+                        "description": "month planted",
+                        "name": "month",
+                        "in": "query",
+                        "required": true
+                    },
+                    {
+                        "type": "number",
+                        "description": "date planted",
+                        "name": "date",
                         "in": "query",
                         "required": true
                     }
@@ -630,7 +621,7 @@
                     "200": {
                         "description": "OK",
                         "schema": {
-                            "$ref": "#/definitions/models.GddResponse"
+                            "$ref": "#/definitions/models.CornMaturityResponse"
                         }
                     },
                     "400": {
@@ -642,9 +633,9 @@
                 }
             }
         },
-        "/api/weather/gdd/seeds": {
+        "/api/weather/gdd/normals": {
             "get": {
-                "description": "Get list of seeds in database",
+                "description": "get gdd normals",
                 "consumes": [
                     "application/json"
                 ],
@@ -652,52 +643,33 @@
                     "application/json"
                 ],
                 "tags": [
-                    "GDD Seed Data"
+                    "Gdd"
                 ],
-                "summary": "Get list of seeds in database",
+                "summary": "Get gdd normals",
                 "parameters": [
                     {
                         "enum": [
                             "corn",
-                            "soybean"
+                            "soybean",
+                            "sunflower",
+                            "tomato",
+                            "sugar_beet",
+                            "peanut",
+                            "cotton",
+                            "potato",
+                            "wheat",
+                            "pea",
+                            "oat",
+                            "spring_wheat",
+                            "rice",
+                            "sorghum"
                         ],
                         "type": "string",
-                        "description": "Crop type to use",
+                        "description": "Crop to calculate gdd for",
                         "name": "product",
                         "in": "query",
                         "required": true
-                    }
-                ],
-                "responses": {
-                    "200": {
-                        "description": "OK",
-                        "schema": {
-                            "$ref": "#/definitions/models.SeedListResponse"
-                        }
                     },
-                    "400": {
-                        "description": "Bad Request",
-                        "schema": {
-                            "$ref": "#/definitions/common.StandardError"
-                        }
-                    }
-                }
-            }
-        },
-        "/api/weather/gdd/seeds/corn": {
-            "get": {
-                "description": "Get estimated maturity date from corn seed",
-                "consumes": [
-                    "application/json"
-                ],
-                "produces": [
-                    "application/json"
-                ],
-                "tags": [
-                    "GDD Seed Data"
-                ],
-                "summary": "Get estimated maturity date from corn seed (uses CFS data if current GDUs are less than the crop maturity)",
-                "parameters": [
                     {
                         "type": "number",
                         "description": "Latitude to search for",
@@ -713,23 +685,9 @@
                         "required": true
                     },
                     {
-                        "type": "string",
-                        "description": "Corn seed to use",
-                        "name": "seed",
-                        "in": "query",
-                        "required": true
-                    },
-                    {
-                        "type": "number",
-                        "description": "month planted",
-                        "name": "month",
-                        "in": "query",
-                        "required": true
-                    },
-                    {
-                        "type": "number",
-                        "description": "date planted",
-                        "name": "date",
+                        "type": "boolean",
+                        "description": "Accumulate gdd values",
+                        "name": "accumulate",
                         "in": "query",
                         "required": true
                     }
@@ -738,7 +696,7 @@
                     "200": {
                         "description": "OK",
                         "schema": {
-                            "$ref": "#/definitions/models.CornMaturityResponse"
+                            "$ref": "#/definitions/models.GddResponse"
                         }
                     },
                     "400": {
@@ -947,17 +905,6 @@
                     }
                 }
             }
-        },
-        "models.SeedListResponse": {
-            "type": "object",
-            "properties": {
-                "seeds": {
-                    "type": "array",
-                    "items": {
-                        "type": "string"
-                    }
-                }
-            }
         }
     }
 }
\ No newline at end of file
diff --git a/docs/swagger.yaml b/docs/swagger.yaml
index 8310da7..ad514eb 100644
--- a/docs/swagger.yaml
+++ b/docs/swagger.yaml
@@ -128,13 +128,6 @@ definitions:
           type: number
         type: array
     type: object
-  models.SeedListResponse:
-    properties:
-      seeds:
-        items:
-          type: string
-        type: array
-    type: object
 host: localhost:8080
 info:
   contact:
@@ -539,32 +532,12 @@ paths:
       summary: Get GDD values calculated from GEFS
       tags:
       - Gdd
-  /api/weather/gdd/normals:
+  /api/weather/gdd/maturity/corn:
     get:
       consumes:
       - application/json
-      description: get gdd normals
+      description: Get estimated maturity date from given gdd
       parameters:
-      - description: Crop to calculate gdd for
-        enum:
-        - corn
-        - soybean
-        - sunflower
-        - tomato
-        - sugar_beet
-        - peanut
-        - cotton
-        - potato
-        - wheat
-        - pea
-        - oat
-        - spring_wheat
-        - rice
-        - sorghum
-        in: query
-        name: product
-        required: true
-        type: string
       - description: Latitude to search for
         in: query
         name: latitude
@@ -575,59 +548,62 @@ paths:
         name: longitude
         required: true
         type: number
-      - description: Accumulate gdd values
+      - description: number of gdds given
         in: query
-        name: accumulate
+        name: gdds
         required: true
-        type: boolean
+        type: number
+      - description: month planted
+        in: query
+        name: month
+        required: true
+        type: number
+      - description: date planted
+        in: query
+        name: date
+        required: true
+        type: number
       produces:
       - application/json
       responses:
         "200":
           description: OK
           schema:
-            $ref: '#/definitions/models.GddResponse'
+            $ref: '#/definitions/models.CornMaturityResponse'
         "400":
           description: Bad Request
           schema:
             $ref: '#/definitions/common.StandardError'
-      summary: Get gdd normals
+      summary: Get estimated maturity date from given gdd (uses CFS data if current
+        GDUs are less than the crop maturity)
       tags:
-      - Gdd
-  /api/weather/gdd/seeds:
+      - GDD Maturity Data
+  /api/weather/gdd/normals:
     get:
       consumes:
       - application/json
-      description: Get list of seeds in database
+      description: get gdd normals
       parameters:
-      - description: Crop type to use
+      - description: Crop to calculate gdd for
         enum:
         - corn
         - soybean
+        - sunflower
+        - tomato
+        - sugar_beet
+        - peanut
+        - cotton
+        - potato
+        - wheat
+        - pea
+        - oat
+        - spring_wheat
+        - rice
+        - sorghum
         in: query
         name: product
         required: true
         type: string
-      produces:
-      - application/json
-      responses:
-        "200":
-          description: OK
-          schema:
-            $ref: '#/definitions/models.SeedListResponse'
-        "400":
-          description: Bad Request
-          schema:
-            $ref: '#/definitions/common.StandardError'
-      summary: Get list of seeds in database
-      tags:
-      - GDD Seed Data
-  /api/weather/gdd/seeds/corn:
-    get:
-      consumes:
-      - application/json
-      description: Get estimated maturity date from corn seed
-      parameters:
       - description: Latitude to search for
         in: query
         name: latitude
@@ -638,34 +614,23 @@ paths:
         name: longitude
         required: true
         type: number
-      - description: Corn seed to use
-        in: query
-        name: seed
-        required: true
-        type: string
-      - description: month planted
-        in: query
-        name: month
-        required: true
-        type: number
-      - description: date planted
+      - description: Accumulate gdd values
         in: query
-        name: date
+        name: accumulate
         required: true
-        type: number
+        type: boolean
       produces:
       - application/json
       responses:
         "200":
           description: OK
           schema:
-            $ref: '#/definitions/models.CornMaturityResponse'
+            $ref: '#/definitions/models.GddResponse'
         "400":
           description: Bad Request
           schema:
             $ref: '#/definitions/common.StandardError'
-      summary: Get estimated maturity date from corn seed (uses CFS data if current
-        GDUs are less than the crop maturity)
+      summary: Get gdd normals
       tags:
-      - GDD Seed Data
+      - Gdd
 swagger: "2.0"
diff --git a/models/seeds.go b/models/seeds.go
index 3c87ed4..c837463 100644
--- a/models/seeds.go
+++ b/models/seeds.go
@@ -21,7 +21,7 @@ type SeedListResponse struct {
 type CornMaturityRequest struct {
 	Latitude  float64 `json:"latitude"`
 	Longitude float64 `json:"longitude"`
-	Seed      string  `json:"seed"`
+	Gdds      float64 `json:"gdds"`
 	Month     int     `json:"month"`
 	Date      int     `json:"date"`
 }
@@ -38,6 +38,7 @@ func (r CornMaturityRequest) Validate() error {
 	return validation.ValidateStruct(&r,
 		validation.Field(&r.Latitude, validation.Required, validation.Min(-90.0), validation.Max(90.0)),
 		validation.Field(&r.Longitude, validation.Required, validation.Min(-180.0), validation.Max(180.0)),
+		validation.Field(&r.Gdds, validation.Required, validation.Min(0.0), validation.Max(6000.0)),
 		validation.Field(&r.Month, validation.Required, validation.Min(1), validation.Max(12)),
 		validation.Field(&r.Date, validation.Required, validation.Min(1), validation.Max(31)),
 	)
@@ -49,12 +50,12 @@ func BuildCornMaturityRequest(c *fiber.Ctx) CornMaturityRequest {
 	lon, _ := strconv.ParseFloat(c.Query("longitude", "-10000.0"), 64)
 	month, _ := strconv.Atoi(c.Query("month", "0"))
 	date, _ := strconv.Atoi(c.Query("date", "0"))
-	seed := c.Query("seed", "-10000.0")
+	gdds, _ := strconv.ParseFloat(c.Query("gdds", "-10000.0"), 64)
 
 	newRequest := CornMaturityRequest{
 		Latitude:  lat,
 		Longitude: lon,
-		Seed:      seed,
+		Gdds:      gdds,
 		Month:     month,
 		Date:      date,
 	}
diff --git a/persistence/mongodb.go b/persistence/mongodb.go
index 3eb8b9c..fc489a2 100644
--- a/persistence/mongodb.go
+++ b/persistence/mongodb.go
@@ -28,11 +28,11 @@ func CreateDBSession() error {
 	Conn, err = mongo.Connect(Ctx, options.Client().
 		ApplyURI(ConnString))
 	if err != nil {
-		return err
+		panic(config.DATABSE_CONNECTION_ERROR)
 	}
 	err = Conn.Ping(Ctx, nil)
 	if err != nil {
-		return err
+		panic(config.DATABSE_CONNECTION_ERROR)
 	}
 	return nil
 }
diff --git a/services/seed_service.go b/services/seed_service.go
index 4d57c7c..d1a9eed 100644
--- a/services/seed_service.go
+++ b/services/seed_service.go
@@ -29,11 +29,6 @@ func GetSeedList(c *fiber.Ctx, request models.SeedListRequest) models.SeedListRe
 
 func GetCornMaturityDate(ctx common.DawnCtx, request models.CornMaturityRequest) models.CornMaturityResponse {
 
-	seed := persistence.FindSeed(request.Seed)
-	if seed.Type != "corn" {
-		panic(config.SEED_TYPE_MISMATCH(request.Seed, "corn"))
-	}
-
 	gddRequest := models.GddRequest{
 		Year:       time.Now().Year(),
 		Product:    "corn",
@@ -57,14 +52,14 @@ func GetCornMaturityDate(ctx common.DawnCtx, request models.CornMaturityRequest)
 	for i := startingIdxVal; i < len(gdds.GddValues); i++ {
 		value := gdds.GddValues[i]
 		gdus += value
-		if math.Abs(gdus-seed.BlackLayerGdus) < math.Abs(closestValue-seed.BlackLayerGdus) {
+		if math.Abs(gdus-request.Gdds) < math.Abs(closestValue-request.Gdds) {
 			closestValue = gdus
 			closestIdx = i
 		}
 	}
 	response := models.CornMaturityResponse{
 		Date:             utils.ConvertDateIdxToDate(closestIdx),
-		GDD:              seed.BlackLayerGdus,
+		GDD:              request.Gdds,
 		ClosestGDD:       closestValue,
 		ClosestLatitude:  gdds.ClosestLatitude,
 		ClosestLongitude: gdds.ClosestLongitude,
-- 
GitLab