From 57efbb8e0f34f3587140886bc4ec3809b4c0fb5c Mon Sep 17 00:00:00 2001 From: Leo Vasquez Date: Mon, 30 May 2022 15:45:26 -0500 Subject: [PATCH] feat: Add required validation ballot_box_with_check ballot_box_with_check :ballot_box_with_check: --- README.md | 9 ++++++-- .../networks/{controler.go => controller.go} | 11 +++++----- src/api/networks/crud.go | 22 +++++++++---------- 3 files changed, 23 insertions(+), 19 deletions(-) rename src/api/networks/{controler.go => controller.go} (59%) diff --git a/README.md b/README.md index 47f6f60..b8e981b 100644 --- a/README.md +++ b/README.md @@ -21,5 +21,10 @@ ### Milestone 2 * [x] Research async implementation approach * [x] Use schema/model validation on queries - * [ ] Validate mandatory and optional body fields -* [ ] Try simple connection with a sqlite \ No newline at end of file + * [x] Validate mandatory and optional body fields +* [ ] Try simple connection with a sqlite + + +#### Summary +* Async way to go: Go-rutines +* Basic validation is out of the box. Complex could be solved using [go-playground validator](https://github.com/go-playground/validator) \ No newline at end of file diff --git a/src/api/networks/controler.go b/src/api/networks/controller.go similarity index 59% rename from src/api/networks/controler.go rename to src/api/networks/controller.go index 60ebbe8..7eeda8a 100644 --- a/src/api/networks/controler.go +++ b/src/api/networks/controller.go @@ -8,20 +8,21 @@ import ( func addNetworks(c *gin.Context) { start := time.Now() - create(c) + response_status, response_msg := create(c) elapsedTime := float64(time.Since(start).Milliseconds())/float64(1000) - c.JSON(200, gin.H{"msg": "Network created", "responseTimeInSeconds": elapsedTime}) + c.JSON(response_status, gin.H{"msg": response_msg, "responseTimeInSeconds": elapsedTime}) } func addNetworksAsync(c *gin.Context) { start := time.Now() - - createAsync(c) + + response_status, response_msg := createAsync(c) elapsedTime := float64(time.Since(start).Milliseconds())/float64(1000) - c.JSON(200, gin.H{"msg": "Network creation triggered", "responseTimeInSeconds": elapsedTime}) + + c.JSON(response_status, gin.H{"msg": response_msg, "responseTimeInSeconds": elapsedTime}) } func getNetworks(c *gin.Context) { diff --git a/src/api/networks/crud.go b/src/api/networks/crud.go index e74ae5d..9c5dc86 100644 --- a/src/api/networks/crud.go +++ b/src/api/networks/crud.go @@ -7,7 +7,7 @@ import ( const DB_WRITTING_TIME_IN_SECONDS = 10 type Network struct { - Name string + Name string `json:"name" binding:"required"` Status string } @@ -16,24 +16,23 @@ var networksTable = []Network{ {Name: "network_02", Status: "ONLINE"}, } -func create(c *gin.Context) { +func create(c *gin.Context) (int, string){ var newNetwork Network - c.BindJSON(&newNetwork) - if newNetwork.Name == "" { - return + if err := c.BindJSON(&newNetwork); err != nil { + return 400, "Error: " + err.Error() } /* CPU demanding CRUD operation starts */ time.Sleep(DB_WRITTING_TIME_IN_SECONDS * time.Second) newNetwork.Status = "ONLINE" networksTable = append(networksTable, newNetwork) + return 200, "Network created" } -func createAsync(c *gin.Context) { +func createAsync(c *gin.Context) (int, string){ var newNetwork Network - c.BindJSON(&newNetwork) - if newNetwork.Name == "" { - return + if err := c.BindJSON(&newNetwork); err != nil { + return 400, "Error" + err.Error() } newNetwork.Status = "PREPARING" networksTable = append(networksTable, newNetwork) @@ -45,14 +44,13 @@ func createAsync(c *gin.Context) { if network.Name == newNetwork.Name { networksTable = append(networksTable[:i], networksTable[i+1:]...) // Deletes placeholder network // Creating the real one as "ONLINE" - var modifNetwork Network - modifNetwork.Name = newNetwork.Name - modifNetwork.Status = "ONLINE" + modifNetwork := Network{newNetwork.Name, "ONLINE"} networksTable = append(networksTable, modifNetwork) return } } } () + return 200, "Network creation triggered" } func readAll() []Network{