Skip to content

Challenge Solved "RealImage/challenge2016" #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
/bin
18 changes: 18 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

rm -rf ./bin
PORT=8080

go build -o ./bin/qube

docker kill qube

docker build -t qube_img .

docker run -d -it --rm --name qube \
-p $PORT:$PORT \
-e PORT=8080 \
-e ENV=production \
qube_img

echo "container 'qube' is now up and running..."
75 changes: 75 additions & 0 deletions controllers/distributer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package controllers

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/roh4nyh/qube_challenge_2016/models"
"github.com/roh4nyh/qube_challenge_2016/service"
)

var DistributorCollectionMap map[string]models.Distributor

func GetDistributors(c *gin.Context) {
collection := service.GetDistributors()
c.JSON(http.StatusOK, collection)
}

func GetDistributor(c *gin.Context) {
distributorID := c.Param("distributor_id")
distributor := service.GetDistributor(distributorID)
c.JSON(http.StatusOK, distributor)
}

func AddDistributor(c *gin.Context) {
var newDistributor models.NewDistributorCmd

if err := c.BindJSON(&newDistributor); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

result, err := service.AddDistributor(newDistributor)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusCreated, result)
}

func CheckDistributorPermission(c *gin.Context) {
distributorID := c.Param("distributor_id")
var checkDistributorPermissionCmd models.CheckDistributorPermissionCmd

if err := c.BindJSON(&checkDistributorPermissionCmd); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

result, err := service.CheckDistributorPermission(distributorID, checkDistributorPermissionCmd.Locations)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusOK, result)
}

func UpdateDistributor(c *gin.Context) {
distributorID := c.Param("distributor_id")
var updateDistributor models.UpdateDistributorCmd

if err := c.BindJSON(&updateDistributor); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

result, err := service.UpdateDistributor(distributorID, updateDistributor)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusOK, result)
}
71 changes: 71 additions & 0 deletions curl.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# check health
curl --location --request GET 'http://localhost:8080/health' \
--header 'Content-Type: application/json'

###

# get distributors
curl --location --request GET 'http://localhost:8080/distributors/' \
--header 'Content-Type: application/json' \

###

# add distributor 1
curl --location --request POST 'http://localhost:8080/distributors/add' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "DISTRIBUTOR1",
"includes": ["IN", "US"],
"excludes": ["KA,IN", "CENAI,TN,IN"],
"parent_distributor_id": ""
}'

###

# add distributor 2
curl --location --request POST 'http://localhost:8080/distributors/add' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "DISTRIBUTOR2",
"includes": ["IN"],
"excludes": ["TN,IN"],
"parent_distributor_id": "41d05a85"
}'

###

# add distributor 3
curl --location --request POST 'http://localhost:8080/distributors/add' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "DISTRIBUTOR3",
"includes": ["HBALI,KA,IN"],
"excludes": [],
"parent_distributor_id": "e5f69807"
}'

###

# check distributor permissions against locations
curl --location --request GET 'http://localhost:8080/distributors/28a78bc3/check' \
--header 'Content-Type: application/json' \
--data-raw '{
"locations": ["CENAI,TN,IN", "YELDU,TG,IN", "WAIRN,UP,IN", "HBALI,KA,IN", "HUBLE,RP,DE", "US", "TN,IN"]
}'

###

# get distributor by id
curl --location --request GET 'http://localhost:8080/distributors/2471f887' \
--header 'Content-Type: application/json' \

###

# update distributor
curl --location --request PUT 'http://localhost:8080/distributors/2471f887' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "demo1",
"includes": ["US"],
"excludes": ["HUBLE,RP,DE"]
}'
Loading