Skip to content

Commit

Permalink
feat: Add required validation ballot_box_with_check ballot_box_with_c…
Browse files Browse the repository at this point in the history
…heck ☑️
  • Loading branch information
luislvasquez committed May 30, 2022
1 parent 55d25da commit 57efbb8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
* [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)
11 changes: 6 additions & 5 deletions src/api/networks/controler.go → src/api/networks/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 10 additions & 12 deletions src/api/networks/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
Expand All @@ -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{
Expand Down

0 comments on commit 57efbb8

Please sign in to comment.